Domain Components Basics
- 3 minutes to read
Important
Domain Component (DC) interfaces are abstractions above eXpress Persistent Objects (XPO) classes. Notwithstanding its benefits, this abstraction imposes certain technical limitations. DC is only suitable for certain usage scenarios. The Blazor User Interface is not supported for DC. DC is in maintenance mode and we do not recommend its use in new software projects. We recommend that you migrate DC interfaces to pure XPO classes.
With Domain Components, you can define interfaces instead of regular business objects inherited from XPO classes. These interfaces will declare the required properties or data fields. The way this data is to be processed (the Domain Logic) is then defined by the creation of special classes that determine how interface members behave when an object is constructed, when properties are changed, etc. Actual business classes are automatically implemented by XAF at runtime, based on the logic and interfaces provided. You can package interfaces and domain logic in an assembly, and use it as a domain library. If you then create a new XAF application, you can reference the domain library and reuse the domain components. Since interfaces support multiple inheritances, the required business objects can be combined into new domain components. With interfaces, you can make your domain model independent of implementation.
Domain Component Interfaces
The following snippet illustrates a typical Domain Component definition.
[DomainComponent]
public interface IPerson {
string LastName { get; set; }
string FirstName { get; set; }
string FullName { get; }
void Copy(IPerson target);
}
As you can see in the code above, the interface decorated with the DomainComponentAttribute is considered to be a Domain Component. This interface must expose properties of the business class to automatically be generated when the application runs. You can use attributes applicable to regular business classes and their properties. For instance, you can decorate the LastName property with the RuleRequiredFieldAttribute, and the interface itself with the NavigationItemAttribute. The FullName property is declared as read-only. Thus, it is required that the logic of its calculation is defined. Additionally, the Copy method implementation is required. The required logic should be implemented in a Domain Logic class associated with the Domain Component via the DomainLogicAttribute attribute.
Register Domain Components
To specify what classes should be generated, register the required domain components with the application. Edit the Module.cs file and invoke the ITypesInfo.RegisterEntity method in the ModuleBase.Setup method override.
using DevExpress.Persistent.BaseImpl;
// ...
public override void Setup(XafApplication application) {
base.Setup(application);
XafTypesInfo.Instance.RegisterEntity("Person", typeof(IPerson));
}
Based on the code above, the Person class derived from the DCBaseObject class will be generated. The generated class will expose properties and utilize the Domain Logic of the IPerson Domain Component.
Note
To use custom base class, pass the baseClass parameter to the RegisterEntity method.