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,
- Teams are spread across geogrpahy, having different or almost opposite timezones.
- Teams are inducted progressively, where former teams are more experienced and knowledgeable on the domain and processes and later teams are still learning.
- Teams guided by different code reviewers
- Developers have extreme range of experience - from 12 months to 12 years, who have to churn out backlogs during sprints at high velocity.
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.
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.
- public class Person {
- public long PersonID {
- get;
- set;
- }
- public string FirstName {
- get;
- set;
- }
- public string LastName {
- get;
- set;
- }
- public DateTime DOB {
- get;
- set;
- }
- public string EntityCode {
- get;
- set;
- }
- }
- //This Entity class was written by Team A
- public class Department {
- public long DepmtID {
- get;
- set;
- }
- public string DepmtName {
- get;
- set;
- }
- public string DepmtSubject {
- get;
- set;
- }
- }
- //This Entity Class is now extended by Team B
- public class ExtendedDepartment: Department {
- //Define a constructor that would still work with the base properties only
- public ExtendedDepartment() {
- this.DepmtID = base.DepmtID;
- this.DepmtName = base.DepmtName;
- this.DepmtSubject = base.DepmtSubject;
- }
- //additional attribute added
- public Person DepmtHead {
- get;
- set;
- }
- public void Save() {
- Console.WriteLine("Superindent successfully added to the Department.");
- //write the code to persist the information into database.
- }
- }

Join the conversation! Your thoughts help the community grow.