Skip to content

Extend existing business classes without modifying their source code: add attributes, create simple, reference, and collection properties.

License

Notifications You must be signed in to change notification settings

DevExpress-Examples/xaf-customize-xpo-business-model-at-runtime

Repository files navigation

XAF - Customize an XPO Business Model at Runtime

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 and PersistentObject2 classes)

Run application

Implementation Details

  1. 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));
  2. 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 the OptimisticLocking or DeferredDeletion 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);
      }
  3. Call the RefreshInfo(Type) method to refresh metadata for the modified types.

    typesInfo.RefreshInfo(typeof(PersistentObject1));
    typesInfo.RefreshInfo(typeof(PersistentObject2));

Files to Review

Documentation

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

About

Extend existing business classes without modifying their source code: add attributes, create simple, reference, and collection properties.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 4

  •  
  •  
  •  
  •