I presented a session on the DAL 2 at DNN World in Orlando, and a few people asked about “calculated” properties - or properties that do not have any matching column in the database. This is an important feature for any “auto-mapped” repository implementation, so we have added three new Attributes that control the mapping of properties and columns.
The first of the new attributes is the IgnoreColumn attribute. Applying this attribute to a property will mean that the property is ignored when mapping the class to it’s corresponding Database table. Continuing to use the Task class from previous blog posts, lets look at an updated version where there is a new property that shouldn’t be saved to the Database.
Listing 1: The Task class
The new “PropertyNotInDatabase” property does not have a corresponding column in the database, and this would throw an error. This can be made to work by adding the new IgnoreColumn attribute - see Listing 2.
Listing 2: Aplying the IgnoreColumn Attribute
DeclareColumns and IncludeColumn Attribute
An alternative approach is to work the other way - i.e. require the class to specify columns that map to fields in the database. This would be the preferred approach if the class has less “included” columns than “excluded” columns. Or maybe as a developer you feel that your intentions are clearer by explicitly including columns to be mapped.
The DeclareColumns attribute can be applied at the class level, and if applied only properties that have the IncludeColumn attribute applied will be mapped to the database table - see Listing 3
Listing 3: Applying the DeclareColumns and IncludeColumns Attributes
So this concludes this introductory series of articles on the new DAL 2. Please continue to send your feedback as you use it in your module development, so we can continue to improve it.