When designing business class, a common task is to initialize the object. The next content, we will initialize the different types of properties.
For example, the implementation of the contacts (Contact) business class, after you create an object, the object is initialized with default values.
Initialize the default object property values in the official document, found there are two ways (the code basically the same): one is inherited XPO framework AfterConstruction () method (I almost use this method); the other is to use Entity Framework ( Entity Framework), OnCreated implement the method of the interface IXafEntityObject.
Note: See the official Demo Website URL reference.
Simple Properties
All persistent classes are derived from the basic PersistentBase class. PersistentBase.AfterConstruction method can be used as a method to initialize the business object. After you create an object called only once. Because this method is specifically designed for the initialization, there is no need to check the current state of the object when the object attributes assigned. The following code demonstrates:
1 public class Contact : Person { 2 //... 3 public override void AfterConstruction() { 4 base.AfterConstruction(); 5 6 FirstName = "Sam"; 7 TitleOfCourtesy = TitleOfCourtesy.Mr; 8 } 9 }
Reference properties
Reference properties are initialized AfterConstruction method. And simple property is different, need access to existing objects. Thus, using the method FindObject. Specific code as follows:
1 public class Contact : Person { 2 //... 3 public override void AfterConstruction() { 4 base.AfterConstruction(); 5 Address1 = new Address(Session); 6 Address1.Country = Session.FindObject<Country>(CriteriaOperator.Parse("Name = 'USA'")); 7 if(Address1.Country == null) { 8 Address1.Country = new Country(Session); 9 Address1.Country.Name = "USA"; 10 Address1.Country.Save(); 11 } 12 Manager = Session.FindObject<Contact>(CriteriaOperator.Parse( 13 "FirstName = 'John' && LastName = 'Doe'")); 14 } 15 }
Collection property
XPCollection.Add method using a set of properties. The following code demonstrates how to use the predefined phone number filled collection.
1 public class Contact : Person { 2 //... 3 public override void AfterConstruction() { 4 base.AfterConstruction(); 5 6 PhoneNumber phone1 = Session.FindObject<PhoneNumber>(CriteriaOperator.Parse( 7 "Number = '555-0101'")); 8 PhoneNumber phone2 = Session.FindObject<PhoneNumber>(CriteriaOperator.Parse( 9 "Number = '555-0102'")); 10 PhoneNumbers.Add(phone1); 11 PhoneNumbers.Add(phone2); 12 } 13 }
Computed Property
slightly. When changes associated property, property change automatically calculated
The newly created objects through new activities (Action) initialization
slightly. See https://documentation.devexpress.com/eXpressAppFramework/112912/Task-Based-Help/Actions/How-to-Initialize-an-Object-Created-Using-the-New-Action
Initializing sub-objects with attribute values obtained from the main objects
Property can set default values in the reference setter properties of the master object as child objects.
1 public class ChildObject : BaseObject { 2 // ... 3 public MasterObject MasterObject { 4 get { return masterObject; } 5 set { 6 bool modified = SetPropertyValue("MasterObject", ref masterObject, value) ; 7 if (!IsLoading && !IsSaving && value != null && modified) { 8 this.SomeProperty = value.DefaultForChildren; 9 } 10 } 11 } 12 }
Reference Site
[1] Related Bowen - default attribute values initialized business object business object initialization (II) - Entity Framework (the Entity Framework): https://www.cnblogs.com/luyj00436/p/11489185.html
[3] official website Project example: http://www.devexpress.com/example=E2053
[4] Default initialize the object attribute value: https://documentation.devexpress.com/eXpressAppFramework/113712/Task-Based-Help/Business-Model-Design/Entity-Framework-EF/How-to-Initialize-Business-Objects -with-Default-Property-Values- in-Entity-Framework
[5] A brief tutorial initialization properties: https://documentation.devexpress.com/eXpressAppFramework/112834/Getting-Started/Comprehensive-Tutorial-MainDemo-Application/Business-Model-Design/Business-Model-Design-with-XPO / Initialize-a-Property-After -Creating-an-Object-XPO
[6] create objects initialized by a new activity (Action): https://documentation.devexpress.com/eXpressAppFramework/112912/Task-Based-Help/Actions/How-to-Initialize-an-Object-Created-Using-the -New-Action