Skip to main content
.NET 6.0+

CreateInstanceAttribute Class

Specifies that a Domain Component‘s target method will create Domain Component instances.

Namespace: DevExpress.ExpressApp.DC

Assembly: DevExpress.ExpressApp.v23.2.dll

NuGet Package: DevExpress.ExpressApp

Declaration

[AttributeUsage(AttributeTargets.Method)]
public class CreateInstanceAttribute :
    Attribute

Remarks

When implementing a Domain Component, you can define a method that will create Domain Component instances. For this purpose, the method must be parameterless, have the required return type, and be decorated with the CreateInstanceAttribute.

[DomainComponent]
public interface IAddress {
    string PrimaryAddress { get; set; }
    string SecondaryAddress { get; set; }
}
[DomainComponent]
public interface IPerson {
    IAddress Address { get; set; }
    [CreateInstance]
    IAddress CreateAddress();
}
[DomainLogic(typeof(IPerson))]
public class PersonLogic {
    public static void AfterConstruction(IPerson person) {
        if(person.Address == null) {
            person.Address = person.CreateAddress();
        }
    }
}

You can override the type of Domain Components created by a method, by using the CreateInstanceAttribute constructor taking a Type parameter.

[DomainComponent]
public interface IExtendedAddress : IAddress { }
[DomainComponent]
public interface IPerson {
    IAddress Address { get; set; }
    [CreateInstance(typeof(IExtendedAddress))]
    IAddress CreateAddress();
}

If an application has several registered entities implementing the interface, specified as the CreateInstanceAttribute.InstanceType, the CreateInstanceAttribute will not be able to determine what type of Domain Components must be created by the method, and an exception will be thrown. In this instance, you need to define the logic for the method, and explicitly specify what type must be instantiated.

[DomainComponent]
public interface ICustomAddress : IAddress { }
[DomainLogic(typeof(IPerson))]
public class AdditionalPersonLogic {
    public static IAddress CreateAddress(IPerson source, IObjectSpace os) {
        return os.CreateObject<ICustomAddress>();
    }
}

Inheritance

Object
Attribute
CreateInstanceAttribute
See Also