Language preference:

Phase 1 / Introducing DataAnnotation attributes and the Entity Metadata class

Your primary tool to define business rules is through DataAnnotation attributes. DataAnnotation attributes are objects, based on System.Attribute, and thus can be attached to class and property definitions as metadata.

BLD supplies numerous DataAnnotation attributes (which you will see in Phase 2), expanding upon those shipped with .net in the System.ComponentModel.DataAnnotations namespace. BLD’s start with the “DESDA.”

Suppose you want to add a range business rule to a DataField. You will add DESDA.RangeAttribute to the property definition of that DataField, specifying in its Minimum and Maximum properties.

[DESDA.Range(0, 10)]
public int PropertyName { get; set; }

However, this reveals a big problem: you cannot modify the DAL’s Entity class where each property resides! (This is actually a good problem to have because DAL’s Entity class is in the wrong layer for your business rules.)

To solve this problem, you will create a new class, called the Entity Metadata class, where you define identical property names and apply DataAnnotation attributes to them. This new class is connected to BLL’s Entity class by the MetadataTypeAttribute on the BLL’s Entity class.

using DESDA=PeterBlum.DES.DataAnnotations;
… 
[MetadataType(typeof(CategoryMetadata))]
[EntityDAOType(typeof(CategoryEntityDAO))]
public partial class Category  
{
 // business logic methods go here.
}

[DESDA.DisplayName("Categories")]
public class CategoryMetadata
{
  [DESDA.Required()]
  [DESDA.DisplayName("Name")]
  public object CategoryName { get; set; }

  [DESDA.DataType(DataType.MultilineText)]
  [DESDA.StringLength(2000)]
  public object Description { get; set; }
}

As you look in the above code, notice how each property is of type Object instead of the original property type in the DAL Entity class. That’s because these properties are merely placeholders for attributes. They need to have identical property names, but their type is never evaluated.

Hint If you prefer your fixed business rules be stored in a database, XML file or some other mechanism, you can use the GatherDataFieldAttributes event handler on the DataContextDescriptor and write code that delivers DataAnnotation attributes from your storage source. The Entity Metadata class can be omitted when you do this.

BLD’s library of DataAnnotation attributes is quite rich, but its unlikely it has all of your business rules covered. You can create your own Attribute classes to handle your special cases.

The next topic introduces the classes that handle CRUD operations.