Association, Aggregation, and Composition in C#
When we talk about real word object, the most complicated thing is relationship. In this post, we will try to understand three important concepts of Object Oriented Programming (Association, Aggregation, and Composition) in general, and C# in particular. I talked about inheritance in my second post, which is one of the relationship between object in Object Oriented Programming.
The five sentences below, describe different relationship between objects.
1) A manager is a type of employee.
2) A manager has a swipe card to enter the company premises.
3) A manager has many workers under him.
4) The salary of a manager depends on project success.
5) A project success depends on a manager.
In the five sentences above, we observe many relationships. Let go through of these relationships and try to understand what each of them means.
The first kind of relationship we can see in the sentence “A manager is a type of employee” is a parent-child relationship. This is an inheritance relationship. The inheritance relationship is identified by the words “is a”. We discussed about inheritance. For more information, please take a look at my second post on this link: https://medium.com/@ibrahimyengue/complex-data-structure-and-some-advance-concept-in-c-3f865a51a19c#.zgwq4duy2
What is an association relationship in C#?
We talk about association between two objects when each one of them can use the other one, but also each one of them can exist without the other one. There is no dependency between them.
If we take a look at the sentence “A manager has a swipe card to enter the company premises”, we can see the relationship “has a”. That means a manager can exist without his swipe card, and his swipe card can also be assigned to another employee. The manager also can have another swipe cared. The relationship between the manager and the swipe card is an association relationship since they are not dependent on each other. Let see how it works with a code.

The manager class contains a method logon, which takes a SwipeCard object and calls the Swipe method from the SwipeCard class. The manager class contains also a get method, which returns the manager name.

The SwipeCard class contains a Swipe method, which takes a manager object, and also a method that return the made of the card.
Now let see the relation between the manager and the SwipeCard classes.

In the screenshot above, Manager and SwipeCard are instantiated and each one is calling a method from to it class. That means each class in independent in the relationship. However if a manager object calls logon method, it will need a SwipeCard object as shown below.

The same happens when a SwipeCard object calls the Swipe method, it needs to pass a Manager object as shown below.

What is an aggregation relationship in C#?
We talk about aggregation between two object when one of them is an owner of the other one.
Looking at the sentence “A manager has many workers under him”, we can see that manager is the owner of a group of employees in his organization, and those employees are not going to report to another entity in the organization. The relationship between the manager and the employees are an aggregation relationship. Let see how this works with a code.

The worker class contains only a method returning the worker name for simplicity.

In this relationship, the Manager class is an owner of employees. That ownership is shown in the manager class by the list of workers. This kind of relationship is an aggregation relationship.
What is a composition relationship in C#?
We talk about composition between two objects when they are dependent each other during they life time. Let take a look at these sentences:
· The salary of a manager depends on project success.
· A project success depends on a manager.
This kind of relationship is a composition relationship since each one of the two objects depends on the other object. Let see how it works with a code.
In the screenshot below, we have a manager class containing a method to check if a project is a success or not. To do that, the manager class has an attribute “project” which I made public for simplicity. This attribute is used to access the project class, and check how successful is a project.

The following screenshot is the project class, which contains a set method. This method check the Boolean IsSuccess and make a decision to increase or to decrease the salary.

Now let see how dependent are the manager and project classes.

A manager object invoking the HowIsThemanager object must passed a Boolean which is from the project class.
As you can see below, to create a project object, we need to pass a manager object to the constructor.
These two last screen shows how dependent are the two classes. This kind of relationship is a composition relationship.

This brings us to the end of this post. I hope that you have learn from this post the difference between Association, Aggregation, and Composition. Thank you for reading my post. If you have any questions, I will be more than happy to respond to you.
