Initialize Business Object Properties (XPO)
- 2 minutes to read
This lesson explains how to initialize properties in newly created business objects.
For this purpose, you will add the Priority property to the DemoTask class created in the Set a Many-to-Many Relationship (XPO) lesson. Then you will override the AfterConstruction method to initialize the new property.
Note
Before you proceed, take a moment to review the previous lessons:
Step-by-Step Instructions
Add the Priority property to the DemoTask class and declare the Priority enumeration, as shown below:
public class DemoTask : Task { // ... private Priority priority; public Priority Priority { get { return priority; } set { SetPropertyValue(nameof(Priority), ref priority, value); } } //... } public enum Priority { Low, Normal, High }
To initialize the newly added Priority property when a DemoTask object is created, override the AfterConstruction method, as shown below:
[DefaultClassOptions] [ModelDefault("Caption", "Task")] public class DemoTask : Task { //... public override void AfterConstruction() { base.AfterConstruction(); Priority = Priority.Normal; } //... }
Run the application.
Create a new DemoTask object. In the Task detail view, the Priority property is set to
Priority.Normal
, as declared in the code above.Note that XAF generates a combo box for the Priority property. The combo box items are the enumeration values declared in the step 2.
Detailed Explanation
Initialize Property Values
The AfterConstruction method is executed when a new business object is created. In this tutorial, you override this method to set the Priority property to Priority.Normal
when a new DemoTask object is created.
Next Lesson
Implement Dependent Reference Properties (XPO)