Getting Class Information
If you have an entity instance, you can get the metadata for that entity using the EntityInfo extension method.
Getting the metadata from an entity instance |
using Mindscape.LightSpeed.MetaData; // bring extension method into scope |
Remember that the Entityinfo represents the metadata for the type of the entity: it is not specific to the entity instance.
You can also get the metadata for an entity type without having an entity instance by using the EntityInfo.FromType static method:
Getting the metadata from an entity type |
var classInfo = EntityInfo.FromType(typeof(Order)); |
Getting Class Settings
EntityInfo has a number of properties relating to the entity class, primarily around storage policies. See the API documentation for information about these properties.
You can also determine which other classes are part of the same single or class inheritance hierarchy as the entity class, using the InheritanceHierarchy property. (Concrete inheritance is not considered because concrete inheritance is not polymorphic and is not used in database mapping.)
Getting Field and Association Information
EntityInfo exposes the fields of the entity in several different ways.
The Fields collection lists all fields defined on the entity class – both ‘value’ fields and associations. To get just the value fields, use the ValueFields collection; to get just the associations, use the Associations collection.
Each of these collections has a corresponding collection with the “Flattened” prefix – FlattenedFields, FlattenedValueFields and FlattenedAssociations. In single or class table inheritance scenarios, these contain all fields of the relevant type from across the inheritance hierarchy. (In the absence of discriminated inheritance, they are the same as the non-flattened equivalents.)
The FieldInfo and AssociationInfo classes have a number of properties relating to the field or association definition. For example, you can get the data type using the FieldType property, or the cardinality of association using the AssociationType property. Again, see the API documentation for information about these properties.
Fields and Properties
The LightSpeed metamodel represents fields, not properties, because fields are what LightSpeed works with. Normally there is a one-to-one correspondence between fields and properties, but if you are hand-coding entities, or have overridden the default designer behaviour using the Generation option, then this may not be the case. In this case, remember that the field collections represent fields, not properties.
You will also notice that FieldInfo has three name properties: FieldName, PropertyName and HumanName. FieldName returns the name of the actual backing field which LightSpeed uses for persistence. PropertyName returns the name of the CLR property which application code uses to get or set that field. As noted above, in hand-coded cases there may not be such a property. Finally, HumanName returns the field name tidied up and split up into words for end user display.
For example, if you create a property named OrderReference in the designer, the metadata name properties will be as follows:
· FieldName: _orderReference
· PropertyName: OrderReference
· HumanName: Order Reference
Getting Validation Information
ValidationInfo exposes information about a validation that is being applied to a field on an entity. To get this information you can enumerate the Validations collection on a FieldInfo.
Each ValidationInfo instance provides a reference to the validation name which you can use for comparison purposes, the .NET type of the validation class if you want to use that for comparison or for any further work within your code and then a dictionary of name/object pairs which contain the actual data associated with the validation instance.
For example for a length validation you will find a LengthValidationRule entry with Minimum and Maximum entries in the dictionary which hold integer values as set on the validation instance.