Skip to main content
.NET Framework 4.5.2+

How to: Initialize Business Objects with Default Property Values in Entity Framework 6

  • 5 minutes to read

When designing business classes, a common task is to ensure that a newly created business object is initialized with default property values. This topic explains how different types of properties can be initialized. As an example, a Contact business class will be implemented. After a Contact object is created, its properties will be initialized with default values.

InitializeObjects_Win

Note

Simple Property

You can support an IXafEntityObject interface in your business classes. This interface declares the IXafEntityObject.OnCreated method intended for object initialization. The OnCreated method is called only once for an object - after the object is created. Whenever you need to initialize an object, place the initialization code into the OnCreated method body. The following code snippet demonstrates how simple value properties can be initialized.

public class Contact : Person, IXafEntityObject {
    //...
    void IXafEntityObject.OnCreated() {
        FirstName = "Sam";
        TitleOfCourtesy = TitleOfCourtesy.Mr;
    }
    void IXafEntityObject.OnLoaded() { }
    void IXafEntityObject.OnSaving() { }
}

To see another example of initializing a simple property, refer to the Initialize a Property After Creating an Object (EF 6) tutorial lesson.

Reference Property

Initialization of reference properties differs from initialization of simple properties, primarily in that you may need to obtain a reference to an existing object. For this purpose, use the IObjectSpace.FirstOrDefault method of the object’s Object Space. To access the Object Space from the business object code, you should support the IObjectSpaceLink interface. The following code snippet demonstrates how to initialize reference properties with new and existing objects.

public class Contact : Person, IXafEntityObject, IObjectSpaceLink {
    //...
    void IXafEntityObject.OnCreated() {
        // ...
        Address1 = objectSpace.CreateObject<Address>();
        Address1.Country = objectSpace.FirstOrDefault<Country>(country => country.Name == "USA");
        if (Address1.Country == null) {
            Address1.Country = objectSpace.CreateObject<Country>();
            Address1.Country.Name = "USA";
        }
        Manager = objectSpace.FirstOrDefault<Contact>(contact => contact.FirstName == "John" && contact.LastName == "Doe");
    }
    void IXafEntityObject.OnLoaded() { }
    void IXafEntityObject.OnSaving() { }
    private IObjectSpace objectSpace;
    IObjectSpace IObjectSpaceLink.ObjectSpace {
        get { return objectSpace; }
        set { objectSpace = value; }
    }
}

Collection Property

The following code snippet demonstrates how to populate the Phones collection with predefined phone numbers.

public class Contact : Person, IXafEntityObject, IObjectSpaceLink {
    //...
    void IXafEntityObject.OnCreated() {
        // ...
        PhoneNumber phone1 = objectSpace.FirstOrDefault<PhoneNumber>(number => number.Number == "555-0101");
        PhoneNumber phone2 = objectSpace.FirstOrDefault<PhoneNumber>(number => number.Number == "555-0102");
        PhoneNumbers.Add(phone1);
        PhoneNumbers.Add(phone2);
    }
    void IXafEntityObject.OnLoaded() { }
    void IXafEntityObject.OnSaving() { }
    private IObjectSpace objectSpace;
    IObjectSpace IObjectSpaceLink.ObjectSpace {
        get { return objectSpace; }
        set { objectSpace = value; }
    }
}

Calculated Property

A calculated property value is automatically updated when the associated property values are changed. To learn how to implement a regular calculated property, refer to the Make a Property Calculable tutorial lesson. To learn how to implement a calculated property based on property values of the objects contained in a child object collection. Refer to the How to: Calculate a Property Value Based on Values from a Detail Collection help topic.

Initialize an Object Created via the New Action

In certain scenarios, you may need to initialize only objects created specifically via the New Action. To learn how to do this, refer to the How to: Initialize an Object Created Using the New Action help topic.

Initialize a Property of a Child Object with a Value Taken from a Master Object

You can set the default value for the child object’s property within the setter of a property that refers to the master object.

public class ChildObject {
    // ...
    public MasterObject MasterObject {
        get { return masterObject; }
        set {
            if (master == value) return;
            masterObject = value;
            if (value != null) {
                this.SomeProperty = value.DefaultForChildren;
            }
        }
    }
}

Since the reference property of a child object will not be initialized until committing changes, it is necessary to use a ViewController to initialize the child object depending on the master object (see How to: Initialize an Object Created Using the New Action).

See Also