Generates Property Helper Structure

Topic updated 12-6-2007

The Generates Property Helper Structure option can be useful if you want to Extend the BO-Layer by adding custom source code. This template option is turned off by default. Turning it on is recommended if you want to use queries that use Filtering and Sorting. The generated Property Helper Structure provides static fields that can be used in stead of literal strings for these queries. This will help you writing error-free code.

 

Writing Queries with the Property Helper Structure

The generated Property Helper Structures are mainly useful when using the GetAll methods of the Factory Class to retrieve data using filtering and/or sorting. Without the use of a Property Helper Structure, creating the ICriteria for a query that uses filtering will look like this:

   ICriteria _criteria = _factory.GetBasicCriteria();

   _criteria.Add(Expression.Lt("Description", "Category Two"));

 

Using the Property Helper Structure, we can code it like this:

   ICriteria _criteria = _factory.GetBasicCriteria();

   _criteria.Add(Expression.Lt(CategoriesProperties.Description, "Category Two"));

          

 

The code that uses the Property Helper Structure has the advantage that we don't need to worry about spelling. When the GetAll method is called with the ICriteria instance _criteria, the NHibernate Framework is called to perform the actual query. This requires the spelling of the properties referenced by strings in the ICriteria to be exactly right. By using the Property Helper Structure, a spelling error will be caught at compile time, not at runtime.

 

The Property Helper Structure

With the Generates Property Helper Structure option turned on for a Table Template, a C# struct is generated with public fields that exactly match the columns of the underlying entity. Using the static fields from this structure will help detect errors in your code at compile time, preventing them to be hidden until runtime errors occur.

Example of a Property Helper Structure:

public struct ProductsProperties

{

       public const string ProductID = "ProductID";

       public const string Description = "Description";

       public const string Stock = "Stock";

       public const string UnitPrice = "UnitPrice";

       public const string OrderLines = "OrderLines";

       public const string IsValid = "IsValid";

}

 

Note that the static fields contain and return a string. This avoids any casting errors that for example could be caused if an enum type was used.