Thursday, November 12, 2015

Aggregate root pattern in C#

Aggregate root are cluster / group of objects that is treated as single unit of data.

I am sure lots of developers are already using this pattern unknowingly, via this short note I would like to inform you formally what you are doing.

Let us try to understand the above definition with an example. Consider the below “Customer” class which has the capability to add multiple “Address” objects to it. In order to achieve the same we have exposed an address collection from the customer class to represent the 1 to many relationships.

The above class structure works perfectly well. You can create object of customer and add multiple addresses object to it.

Customer cust = new Customer();
cust.CustomerName = "Shiv koirala";
cust.DateofBirth = Convert.ToDateTime("12/03/1977");

Address Address1 = new Address();
Address1.Address1 = "India";
Address1.Type = "Home";

Address Address2 = new Address();
Address2.Address1 = "Nepal";
Address2.Type = "Home";

Now let's say we want to implement the following validations:-

“Customer can only have one address of Home type”.

At this moment the address collection is a NAKED LIST COLLECTION which is exposed directly to the client. In other words there are no validations and restrictions on the “Add” method of the list. So you can add whatever and how much ever address objects as you wish.


So how to address this problem in a clean and logical way. If you think logically “Customer” is composed of Addressescollection, so Customer is like a main root.So rather than allowing DIRECT NAKED ACCESS to Addresses list how about accessing the address list from the customer class.In other words centralizing access to address objects from the customer class.

So below are three steps which I have implemented to put a centralize address validation.

Step 1 :- I have made the address list private. So no direct access to the collection is possible.

Step 2 :- Created a “Add” method in the “Customer” class for adding the “Address” object. In this add method we have put the validation that only one “Home” type address can be added.

Step 3 :- Clients who want to enumerate through the address collection for them we have exposed “IEnumerable” interface.

If you analyze the above solution closely the customer is now the root and the address object is manipulated and retrieved via the customer class.

Why did we do this? , because “Customer” and “Address” object is one logical data unit. To maintain integrity of address validation we need to go via the “Customer” class. In the same way loading of data ,updation , deletion etc should all happen via the “Customer” class so that we have proper data integrity maintained.

So when we say load customer from database all the respective address objects should also get loaded.

So when a group of objects which form one logical unit should have centralized root via which the manipulation of the contained object should happen. This kind of arrangement is terms as “Aggregate Root”.

The best way to Learn Design pattern is by doing a project. So if interested you can start from the below youtube video which demonstrates Design pattern by doing a project.

No comments: