Using SPF Attributes

 

Project Page

Download

Manual

Overview
Hello SPF
Attributes
Constraints
Triggers
Criteria
Child Collections

Although SPF can use reflection and infer details of how to store an entity, sometimes it is either desirable or necessary to override this behaviour. This is done through the use of the attributes on the class and fields of the entity. A full listing of this example can be found here.

Class Attributes

The SpfTypeStorage attribute can be used to tell SPF what table the entity maps to. This can be used if the name of the entity conflicts with a keyword in the data store or if you have naming standard for tables, e.g. they must start with 'tb' and be plural. The following example shows how an entity called 'User' is mapped to a table called 'tbUsers':

  [SpfTypeStorage(TableName = "tbUsers")]
  public class User : SpfEntity {

Field Attributes

Data Type

If no data type attribute is specifed then SPF uses the type of the field to infer an in built data type attribute. The table below shows the types supported:

Type Attribute
bool SpfBoolDataTypeAttribute()
int SpfIntDataTypeAttribute()
double SpfDoubleDataTypeAttribute()
Guid SpfGuidDataTypeAttribute()
string SpfStringDataTypeAttribute(4000, true)
DateTime SpfDateTimeDataTypeAttribute()

If we wish to override the default behaviour then we simply add an attribute to the field. For example, to define username string field with a maximum length of 16 we would code the following:

    [SpfStringDataType(16)]
    public string Username;

The default behaviour is for all strings to allow null and have a maximum length of 4000 (the largest nvarchar SQL Server allows). The above code ensures that username will be not be null or contain more that 16 characters. The following code specifies that nulls are allowed but the address line cannot be longer than 255 characters:

    [SpfStringDataType(256, true)]
    public string AddressLine2;

SPF supports enumerated types by default. It uses reflection to extract the base type and the valid values and so can take care of all the validation.

To specify custom data types, an custom data type attribute must be used. These attributes are created in the process of building custom data types.

Storage

By default, SPF loads and saves all public fields on an entity using a column matching the name of the field. As with the other default behaviour, this is overridden via attributes.

For example, say we wish to have a field representing the confirmation of a user's password so that we can validate the data entry. This value should not be loaded from or saved to the store. which the following code specifies:

    [SpfFieldStorage(IsLoaded = false, IsSaved = false)]
    public string PasswordConfirmation;

If we wish to specify the name of the column a field maps to then this can be done as follows:

    [SpfFieldStorage("UserTitle")] 
    public UserTitle Title;

The above code specifies that the field is mapped to a column called 'UserTitle'.

Summary

We have seen how to override the default behaviour of SPF to specify the table and column names used in storing the entity and how to specify the data type of a field. We also saw how we can tell SPF whether or not a field value should be loaded or saved from the store.