Using Extension Methods - Very Helpful For Scrum (Agile) Teams!

Background

 
You may already know how Extension Methods (one of the advance topics of C#) is defined. Some of you may be using it in your projects.
 
However, many of you may not be able to appreciate the scenarios in which the extension methods capability provides excellent leverage of code manageability and coordination among teams.
 
Practical project execution using Agile methology in real world has many challenges. That is further manifolded by the following scenarios,
  1. Teams are spread across geogrpahy, having different or almost opposite timezones.
  2. Teams are inducted progressively, where former teams are more experienced and knowledgeable on the domain and processes and later teams are still learning.
  3. Teams guided by different code reviewers
  4. Developers have extreme range of experience - from 12 months to 12 years, who have to churn out backlogs during sprints at high velocity.
Allow me to elaborate a scenario graphically. (Do let me know with your comments if you have worked in projects of similar nature!)
 
Team A defined an Entity Class Department in a earlier sprint, which another Team B, need to extend in later sprint based on the feature they are working on.
 
Approach 1
 
Change the original class defined by Team A and add the properties.
 
Impact
 
The functions and API's that refer the earlier class, will need to undergo changes. This will increase the scope of work for Team B It will increase the effort for testing as regression test suites needs to run on all previous implementations.
 
The situation will complicate if there are external or dependent API's on this one as the changes must cascade there as well.
 
Approach 2
 
Do not touch the original class and add extension methods that is required by Team B to add their functionality.
 
Impact
 
Minimal, since the original class was not touched, all implementation related to it, will not get affected.
Using Extension Methods - Very helpful for Scrum (Agile) Teams! 
Thus, Extension Methods comes very handy in such situations.
 
Allow me to jump to the code part, straight. I will inherit from the former class and define my new class as follows. You may add the Save() method in this class or seperate based on your design pattern. It is included here for illustration purpose only.
  1. public class Person {  
  2.     public long PersonID {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string FirstName {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string LastName {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public DateTime DOB {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public string EntityCode {  
  19.         get;  
  20.         set;  
  21.     }  
  22. }  
  23. //This Entity class was written by Team A     
  24. public class Department {  
  25.     public long DepmtID {  
  26.         get;  
  27.         set;  
  28.     }  
  29.     public string DepmtName {  
  30.         get;  
  31.         set;  
  32.     }  
  33.     public string DepmtSubject {  
  34.         get;  
  35.         set;  
  36.     }  
  37. }  
  38. //This Entity Class is now extended by Team B     
  39. public class ExtendedDepartment: Department {  
  40.     //Define a constructor that would still work with the base properties only    
  41.     public ExtendedDepartment() {  
  42.         this.DepmtID = base.DepmtID;  
  43.         this.DepmtName = base.DepmtName;  
  44.         this.DepmtSubject = base.DepmtSubject;  
  45.     }  
  46.     //additional attribute added    
  47.     public Person DepmtHead {  
  48.         get;  
  49.         set;  
  50.     }  
  51.     public void Save() {  
  52.         Console.WriteLine("Superindent successfully added to the Department.");  
  53.         //write the code to persist the information into database.    
  54.     }  
  55. }  
Now we will add the extension methods. Again, for additional illustration, I have added two new Extention methods to the Person class (though it is not required for the Department class to work
 
Method 1
 
Creating the Full Name from the First Name and Last Name of the Person. This could be required for display to the user only and not required to be persisted.
 
Method 2
 
Find the Age of the Person given his DOB.
 
Method 3
 
Extension method to add the Department Head to the existing Department details. The Department Head is of type Person
  1. public static class EntityExtensions  
  2. {  
  3.     public static string FullName(this Person p)  
  4.     {  
  5.         return $"{p.FirstName} {p.LastName}";  
  6.     }  
  7.   
  8.     public static int Age(this Person p)  
  9.     {  
  10.         // Save today's date.  
  11.         var today = DateTime.Today;  
  12.         // Calculate the age.  
  13.         var age = today.Year - p.DOB.Year;  
  14.         // Go back to the year in which the person was born in case of a leap year  
  15.         if (p.DOB.Date > today.AddYears(-age)) age--;  
  16.         return age;  
  17.     }  
  18.   
  19.     public static void AddDepartmentHead(this ExtendedDepartment d, Person h)  
  20.     {  
  21.         d.DepmtHead = h;  
  22.     }  
  23. }  
The extension method are included in the client method by importing the namespace only under which the Extension class is defined.
 
Thus, in the calling method, following is to be implemented.
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.   
  6.         //Demo   
  7.         DemoOfExtensions();  
  8.   
  9.         Console.ReadLine();  
  10.     }  
  11.   
  12.     static void DemoOfExtensions()  
  13.     {  
  14.         //Define the Person type of superintendent.   
  15.         Person superitendent = new Person()  
  16.         {  
  17.             PersonID = 190987,  
  18.             FirstName = "Ram",  
  19.             LastName = "Kumar",  
  20.             DOB = Convert.ToDateTime("12-Apr-1980"),  
  21.             EntityCode = "T"  
  22.         };  
  23.   
  24.         //This is the method that Team B is now defined and using it to extend their functionality  
  25.         //i.e. adding a Superintendent.  
  26.         ExtendedDepartment d = new ExtendedDepartment()  
  27.         {  
  28.             DepmtID = 14,  
  29.             DepmtName = "Electrical and Electronics",  
  30.             DepmtSubject = "Electronics, Electrical, Advance Networking"  
  31.         };  
  32.   
  33.         //We have added two extensions method to Person class in order to display additional processed information  
  34.         Console.WriteLine("Welcome to Professor " + superitendent.FullName() + " !");  
  35.         Console.WriteLine("He is currently " + superitendent.Age() + " yrs. old.");  
  36.           
  37.         //let's use the extension method to add the superintendent  
  38.         d.AddDepartmentHead(superitendent);  
  39.   
  40.         //let's check if the extended department class is holding the properties value of the base  
  41.         Console.WriteLine("From base: "+ d.DepmtID + ", " + d.DepmtName + ", " + d.DepmtSubject);  
  42.   
  43.         //some method to persist the changes into database  
  44.         d.Save();  
  45.   
  46.     }  
  47. }  
 The output on the console is,
  1. Welcome to Professor Ram Kumar !  
  2. He is currently 41 yrs. old.  
  3. From base: 14, Electrical and Electronics, Electronics, Electrical, Advance Networking  
  4. Superindent successfully added to the Department.  

Conclusion 

 
Use the Extension Methods when you do not want to touch the initial class and its related implementation. If that class is not sealed, you can use the above-mentioned approach very effectively.
 
Do share your feedback as always. Until then, stay safe and stay blessed.
 
Thank You !