Each property has a type, such as string, integer, or boolean, that mimics the type from the Column
in the Table (nvarchar, integer).
That type is often not specific enough for developing
a user interface or validation rules. For example, a double can be used as a currency,
percentage, duration, longitude, etc. DateTime can be used as a Date, Time, both
or Duration. The DataTypeAttribute lets you redefine the type and provide more specific
validation and formatting rules.
The BLD user interface layer uses DataTypeAttributes to select a Field Template, which
describes the user interface for a given type. Instead of just getting a textbox that validates
decimals, it may get a CurrencyTextBox or PercentageTextBox with right validators.
Not every DataField (property in the Entity class) needs a DataTypeAttribute. Use
the DataTypeAttribute when the DataField must be described more precisely than the
database’s datatype.
To apply a DataTypeAttribute, open an Entity class file and go to the property that
needs its DataType to be clarified. Then add the attribute to that property. If
you are using an Entity Metadata class, (applies to LINQ to SQL and Entity Framework
users), the work should be done in the Entity Metadata class. You will have to add
a property declaration first, giving it the same name as the Entity's property.
However, its type can be 'object' as this is merely a placeholder.
Here is the CategoryMetadata class with two of its properties defined and the appropriate
DataTypeAttributes assigned.
<span class='SCKeyword'>public</span> <span class='SCKeyword'>class</span> CategoryMetadata
{
[DESDA.DataType(DataType.MultilineText)]
<span class='SCKeyword'>public</span> <span class='SCKeyword'>object</span> Description { <span class='SCKeyword'>get</span>; <span class='SCKeyword'>set</span>; }
[DESDA.DbImageDataType(BadFormatErrorMessage=<span class='SCString'>"Bad format"</span>,
ErrorMessage=<span class='SCString'>"Must be {EXTENSION}"</span>, SupportedFileTypes=<span class='SCString'>"jpg"</span>)]
<span class='SCKeyword'>public</span> <span class='SCKeyword'>object</span> Picture { <span class='SCKeyword'>get</span>; <span class='SCKeyword'>set</span>; }
}
Here are the DataTypeAttribute classes available in BLD.
Attribute Class
|
Usage
|
DESDA.ABARoutingNumberAttribute
|
Use when the string contains an ABA Routing Number.
|
DESDA.AnniversaryDataTypeAttribute
|
For date oriented DataFields containing an anniversary (date without the year).
|
DESDA.CreditCardNumberAttribute
|
Use when the string contains a credit card number.
|
DESDA.CurrencyDataTypeAttribute
|
For floating point DataFields that contain a currency value.
|
DESDA.DataTypeAttribute
|
When none of the other DataTypeAttributes are the right choice. It has numerous
options in the System.ComponentModel.DataAnnotations.DataType type.
BLD extends that list with its own enumerated type, PeterBlum.DES.DataAnnotations.ExtendedDataType.
|
DESDA.DateDataTypeAttribute
|
For date DataFields.
|
DESDA.DbImageDataTypeAttribute
|
For binary DataFields that contain an image, including gif, jpg, png, tiff, and
bmp.
|
DESDA.DecimalDataTypeAttribute
|
For floating point DataFields. If your DataField is already a floating point type,
this is not required, but has extra properties for your business rules.
|
DESDA.DecimalMeasurementDataTypeAttribute
|
For floating point DataFields whose value reflects a measurement, like gallon, millimeter,
and degrees.
|
DESDA.DurationDataTypeAttribute
|
For time, integer, or floating point DataFields containing a time duration. Integer
and floating point values can represent a number of hours, minutes, seconds, and
more.
|
DESDA.EmailAddressDataTypeAttribute
|
For text DataFields containing email addresses.
|
DESDA.EnumeratedAttribute
|
For integer DataFields that have a fixed list of values and associated text for
each value, much like an enumerated type.
|
DESDA.EnumeratedStringAttribute
|
For text DataFields that have a fixed list of values and associated text for each
value, much like an enumerated type.
|
DESDA.IntegerDataTypeAttribute
|
For integer DataFields. If your DataField is already an integer type, this is not
required, but has extra properties for your business rules.
|
DESDA.IntegerMeasurementDataTypeAttribute
|
For integer DataFields whose value reflects a measurement, like gallon, millimeter,
and degrees.
|
DESDA.MonthYearDataTypeAttribute
|
For date oriented DataFields containing a MonthYear (date without the day of the
month).
|
DESDA.PercentDataTypeAttribute
|
For floating point DataFields that contain a percent value.
|
DESDA.ListOfValuesDataTypeAttribute
|
A more powerful version of the DESDA.EnumeratedAttribute that allows you
to gather data from various resources and display that data in different ways.
For example, a Country DataField could get its data from an XML file and display
either the full name or abbreviated name.
|
DESDA.TimeOfDayDataTypeAttribute
|
For date, time, integer, or floating point DataFields containing a time of day (time
from 0 to 23:59:59).
|
DESDA.UrlAsImageDataTypeAttribute
|
For text DataFields containing a URL to an image.
|
DESDA.UrlDataTypeAttribute
|
For text DataFields containing URLs.
|
The Source Code Browser shows completed DataAnnotations. The DataTypeAttributes
have been highlighted.
In the next topic, you'll learn about ValidationAttributes,
the rules that create validation.
Open the Source Code Browser (C# only)