Aggregation And Composition In C#

In today's discussion, we will discuss Aggregation and Composition in detail using C#. So, let's get started. Let me go ahead and create simple console App for demo purpose.

  1. namespace CSharpStuffs    
  2. {    
  3.    public class Patient    
  4.     {    
  5.         public string PatientName { getset; }    
  6.         public string AdmissionDate { getset; }    
  7.         public string ReleaseDate { getset; }    
  8.     
  9.         public DoctorInfo DoctorInfo { getset; }    
  10.         public LifeInsurance Insurance { getset; }    
  11.         }    
  12.   }   
  1. namespace CSharpStuffs    
  2. {    
  3.    public class LifeInsurance    
  4.    {    
  5.        public string PolicyName;    
  6.        public string CompanyName;    
  7.    }    
  8. }  
Once, the class gets created, It's time to feed the value via constructor. Hence, the following is the final code for the same.
  1. namespace CSharpStuffs    
  2. {    
  3.    public class Patient    
  4.     {    
  5.         public string PatientName { getset; }    
  6.         public string AdmissionDate { getset; }    
  7.         public string ReleaseDate { getset; }    
  8.     
  9.         public DoctorInfo DoctorInfo { getset; }    
  10.         public LifeInsurance Insurance { getset; }    
  11.     
  12.        public Patient(string patientName,string admissionDate,string releaseDate,string doctorName,string doctorSpeciality, string workingAt)    
  13.        {    
  14.            PatientName = patientName;    
  15.            AdmissionDate = admissionDate;    
  16.            ReleaseDate = releaseDate;    
  17.     
  18.            //Composition comes into picture now    
  19.            //It also means when we construct Patient, we also constructed DoctorInfo    
  20.            //Hence, scope of Doctorinfo is dependent on Patient    
  21.            DoctorInfo = new DoctorInfo    
  22.            {    
  23.                DoctorName = doctorName,    
  24.                DoctorSpeciality = doctorSpeciality,    
  25.                WorkingAt = workingAt    
  26.            };    
  27.     
  28.        }    
  29.     
  30.        //Overrideenn ToString() for convience     
  31.        public override string ToString()    
  32.        {    
  33.            string result = PatientName + " " +    
  34.                            AdmissionDate + " " +    
  35.                            ReleaseDate + " ";    
  36.            //Filling Other objects    
  37.            if (DoctorInfo != null)    
  38.            {    
  39.                result += DoctorInfo.DoctorName + " "    
  40.                          + DoctorInfo.DoctorSpeciality + " "    
  41.                          + DoctorInfo.WorkingAt;    
  42.            }    
  43.            if (Insurance != null)    
  44.            {    
  45.                result += " "+ Insurance.PolicyName + " "    
  46.                          + Insurance.CompanyName;    
  47.            }    
  48.            return result;    
  49.        }    
  50.     }    
  51. }   
You will also notice that here, I have used Composition concept. Composition is tied with actual object which is patient here and hence DoctorInfo also got constructed.

Now, main program looks like the following:
  1. using System;    
  2.     
  3. namespace CSharpStuffs    
  4. {    
  5.     class Program    
  6.     {    
  7.         static void Main(string[] args)    
  8.         {    
  9.             Patient patient = new Patient("Tim","12-08-2015","20-08-2015","Cook","Physician","Fortis");    
  10.             Console.WriteLine(patient.ToString());    
  11.             Console.ReadLine();    
  12.         }    
  13.     }    
  14. }  
And, when I run the program, it will produce the following output.

run

However, I can modify the main program to add more info to this via aggregation as shown below.
  1. using System;    
  2.     
  3. namespace CSharpStuffs    
  4. {    
  5.     class Program    
  6.     {    
  7.         static void Main(string[] args)    
  8.         {    
  9.             Patient patient = new Patient("Tim","12-08-2015","20-08-2015","Cook","Physician","Fortis");    
  10.             Console.WriteLine(patient.ToString());    
  11.                 
  12.     
  13.             //Now, we can add insurance by aggregation    
  14.             patient.Insurance = new LifeInsurance    
  15.             {    
  16.                 CompanyName = "HDFC",    
  17.                 PolicyName = "LIFE-AXA"    
  18.             };    
  19.             Console.WriteLine(patient.ToString());    
  20.             Console.ReadLine();    
  21.         }    
  22.     }    
  23. }   
With the above change in place, it will display the following info.

display

Therefore, in a nutshell, Aggregation is the stuff which can be added at later point of time to the object, but composition gets constructed with object creation itself.

Thanks!

 

Up Next
    Ebook Download
    View all
    Learn
    View all