Alik Levin's

Clarity, Technology, and Solving Problems | PracticeThis.com

Improving Productivity & Readability With Auto-Implemented Properties, & Object Initializers

Improving Productivity & Readability With Auto-Implemented Properties, & Object Initializers

  • Comments 0
 Alik Levin    I have improved readability dramatically by re-implementing my Entities using auto-implemented properties and object initializers. The change is inspired after reading a book LINQ in Action.

Quick Resource Box

Remove Old Fashion Constructor

The original version of my Entities was implemented following the classic approach where you create private fields that are initialized by constructor and the values exposed by public properties. Using auto-implemented properties feature allowed me to dramatically reduce amount of plumbing code.

Before using auto-implemented properties:

    [DataContract] 
    public class AccountInfo 
    { 
        private string m_id = string.Empty; 
        private string m_ownerId = string.Empty; 
        private string m_currency = string.Empty; 
        private double m_balance = double.NaN; 

        public AccountInfo(string id,
                    string ownerId,
                    string currency,
                    double balance)

        [DataMember] 
        public string Id 
        { 
            get { return m_id; } 
            set { m_id = value; } 
        } 
        [DataMember] 
        public string OwnerId 
        { 
            get { return m_ownerId; } 
            set { m_ownerId = value; } 
        } 
        [DataMember] 
        public string Currency 
        { 
            get { return m_currency; } 
            set { m_currency = value; } 
        } 
        [DataMember] 
        public double Balance 
        { 
            get { return m_balance; } 
            set { m_balance = value; } 
        } 
    } 

After using auto-implemented properties:

    public class AccountInfo
    {
        [DataMember]
        public string Id{get; set; }
        [DataMember]
        public string OwnerId{get; set; }
        [DataMember]
        public string Currency{get; set; }
        [DataMember]
        public double Balance {get; set; }

    }

Sweet. 11 lines instead of almost 40!

Changing Business Services 

Now I needed to change my business services class that uses the Entities. This is where object initializer kicks in.

Before object initialzers:

List<AccountInfo> result = null;
result = new List<AccountInfo

    new AccountInfo("ac1","ownr1", "USD", 1001), 
    new AccountInfo("ac2","ownr2", "USD", 1002), 
    new AccountInfo("ac3","ownr3", "USD", 1003), 
    new AccountInfo("ac4","ownr4", "USD", 1004), 
    new AccountInfo("ac5","ownr5", "USD", 1005), 
    new AccountInfo("ac6","ownr6", "USD", 1006),
};

After using object initializer:

In this case I needed to add subtle amount of code, such as Id=, Owner=, Currency=, and Balance=. The number of lines stays the same though:

var result = new List<AccountInfo>
{
    new AccountInfo{Id="ac1",OwnerId="ownr1",Currency ="USD",Balance= 1001},
    new AccountInfo{Id="ac2",OwnerId="ownr2",Currency ="USD",Balance= 1002},
    new AccountInfo{Id="ac3",OwnerId="ownr3",Currency ="USD",Balance= 1003},
    new AccountInfo{Id="ac4",OwnerId="ownr4",Currency ="USD",Balance= 1004},
    new AccountInfo{Id="ac5",OwnerId="ownr5",Currency ="USD",Balance= 1005},
    new AccountInfo{Id="ac6",OwnerId="ownr6",Currency ="USD",Balance= 1006},
};

The interesting part is that Visual Studio supports intellisense when using object initialization feature:

image 

Regression Testing

To make sure I have not broken anything I have ran my Business Services unit test:

image

Ahh, heaven ;)

Conclusion

Auto-implemented properties help removing ton of plumbing code. That does not mean constructors are dead. But in majority of the situations that’s what’s needed and constructors get in a way with ton of unnecessary code that can be avoided with auto-implemented properties easily initialized with object initalizers.

Related Books