You can extend existing business classes without modifying their source code. For instance, when you work with an assembly that contains persistent classes.
This example modifies business classes declared in a separate project as follows:
- Adds an attribute (DefaultClassOptionsAttribute)
- Creates a simple persistent property (
NewIntField
) - Creates reference and collection properties linked by an association (one-to-many relationship between
PersistentObject1
andPersistentObject2
classes)
-
Populate the AdditionalExportedTypes property with types from external storage to add them to the application.
this.AdditionalExportedTypes.Add(typeof(MyXPOClassLibrary.PersistentObject1)); this.AdditionalExportedTypes.Add(typeof(MyXPOClassLibrary.PersistentObject2));
-
Modify the added types as follows:
- Call the AddAttribute method to add an attribute to an existing class.
Note that by design you cannot dynamically add or remove theOptimisticLocking
orDeferredDeletion
attribute.ITypeInfo typeInfo1 = typesInfo.FindTypeInfo(typeof(PersistentObject1)); typeInfo1.AddAttribute(new DevExpress.Persistent.Base.DefaultClassOptionsAttribute());
- Call the CreateMember method to create a new simple persistent property.
IMemberInfo memberInfo0 = typeInfo1.FindMember("NewIntField"); if (memberInfo0 == null) { typeInfo1.CreateMember("NewIntField", typeof(int)); }
- Use both AddAttribute and CreateMember methods to create new reference and collection properties linked by an association.
ITypeInfo typeInfo2 = typesInfo.FindTypeInfo(typeof(PersistentObject2)); IMemberInfo memberInfo1 = typeInfo1.FindMember("PersistentObject2s"); IMemberInfo memberInfo2 = typeInfo2.FindMember("PersistentObject1"); if (memberInfo1 == null) { memberInfo1 = typeInfo1.CreateMember("PersistentObject2s", typeof(DevExpress.Xpo.XPCollection<PersistentObject2>)); memberInfo1.AddAttribute(new DevExpress.Xpo.AssociationAttribute("PersistentObject1-PersistentObject2s", typeof(PersistentObject2)), true); memberInfo1.AddAttribute(new DevExpress.Xpo.AggregatedAttribute(), true); } if (memberInfo2 == null) { memberInfo2 = typeInfo2.CreateMember("PersistentObject1", typeof(PersistentObject1)); memberInfo2.AddAttribute(new DevExpress.Xpo.AssociationAttribute("PersistentObject1-PersistentObject2s", typeof(PersistentObject1)), true); }
- Call the AddAttribute method to add an attribute to an existing class.
-
Call the RefreshInfo(Type) method to refresh metadata for the modified types.
typesInfo.RefreshInfo(typeof(PersistentObject1)); typesInfo.RefreshInfo(typeof(PersistentObject2));
- Ways to Add a Business Class
- Use Metadata to Customize Business Classes Dynamically
- Access Business Object Metadata
- How to create business classes at runtime based on predefined configurations or allow user to define custom members via the application UI
- How to define a custom member for a domain component (DC) at runtime?
(you will be redirected to DevExpress.com to submit your response)