4 Real Time Use of Partial Classes and Partial Method


Recently I was researching partial classes and the real-time use them. Many of the articles found on Google talked about the concept of partial classes and partial methods, but very few highlighted what scenarios to use them in.
In this article, we will first start with the fundamentals of partial classes and methods and then discuss real-time use.
I have also created a video on partial classes and shown the real-time use of it.

Fundamentals of partial classes

A partial class allows a single class to be divided into two separate physical files. During compile time these files get compiled into a single class.  For instance, you can see in the following figure we have the customer class divided into two different files "customer1.cs" and "customer2.cs".
During compilation, these files get compiled into a single class internally. So when you create an object of the customer class you will be able to see methods lying in both the physical files. For instance, you can see the "Add" method belongs to "customer1.cs" and the "Delete" method belongs to "customer2.cs", but when the customer object is created we can see both "Add" and "Delete" methods.

Fundamentals of partial methods

There is one more important concept in partial classes called partial methods. Partial methods help us to define a method definition in one of the physical files and we can implement that method in the other physical files as shown in the following figure.
In the following figure, you can see we have defined the "Validate" method in "Customer1.cs" and this validate method is implemented in "Customer2.cs". Please note the partial keywords attached to both of these methods.
Use number 1: ASP.NET auto-generated code
The biggest use of partial classes is in technologies where there is code generation. The Microsoft team themselves use partial classes in ASP.NET, LINQ, and EF code generation. For instance, when we look at ASP.NET there are two parts, one is the auto-generated code of a page and the other is the code you write.
The custom logic is written in the ".aspx.cs" file while the auto-generated logic is in the ".aspx.designer.cs" file as shown in the following figure:
As a developer, you would like the auto-generated code to do his work i.e. generate code when you drag and drop a button the ASP.NET designer., as in:
The following is how the code snippet of the auto-generated code looks like:
  1. public partial class WebForm1 {  
  3.     /// <summary>  
  4.     /// form1 control.  
  5.     /// </summary>  
  6.     /// <remarks>  
  7.     /// Auto-generated field.  
  8.     /// To modify move field declaration from designer file to code-behind file.  
  9.     /// </remarks>  
  10.     protected global::System.Web.UI.HtmlControls.HtmlForm form1;  
  12.     /// <summary>  
  13.     /// Button1 control.  
  14.     /// </summary>  
  15.     /// <remarks>  
  16.     /// Auto-generated field.  
  17.     /// To modify move field declaration from designer file to code-behind file.  
  18.     /// </remarks>  
  19.     protected global::System.Web.UI.WebControls.Button Button1;  
  21.     /// <summary>  
  22.     /// Label1 control.  
  23.     /// </summary>  
  24.     /// <remarks>  
  25.     /// Auto-generated field.  
  26.     /// To modify move field declaration from designer file to code-behind file.  
  27.     /// </remarks>  
  28.     protected global::System.Web.UI.WebControls.Label Label1;  

At the same time, you would also like to customize the code in some other file so that the auto-generated part is not disturbed. For the same ASP.NET provides the ".aspx.cs" file which is a partial class wherein you can put your own custom logic.
  1. public partial class WebForm1: System.Web.UI.Page {  
  2.     protected void Page_Load(object sender, EventArgs e) {  
  3.         // Your custom logic  
  4.     }  

This is only possible when the class is split into two physical files but united via the partial keyword. So if you see any ASP.NET behind code class files it's marked by the word partial.
So by using the partial keyword in ASP.NET the Microsoft team has made VS and developer work side by side thus not fiddling with each other's code and thus increasing productivity.
Use number 2: LINQ and Entity framework
LINQ and EF also use partial classes and methods heavily because of the auto-generation nature of these technologies. So when you drag tables in these frameworks they create auto-generated classes as shown in the following figure.
In the following figure you can see how the auto-generated code has partial classes and partial method:
The partial methods later can be extended for including custom logic. For instance, you can see in the following code for the preceding auto-generated class "tblCustomer" we have used partial methods to override the "OnCustomerCodeChanged" event to ensure that the customer code is not more than 8 length.
  1. public partial class tblCustomer {  
  2.     partial void OnCustomerCodeChanged() {  
  3.         if (_CustomerCode.Length > 8) {  
  4.             throw new Exception("Customer code can not be greater than 8");  
  5.         }  
  6.     }  

So by using partial classes and partial methods, LINQ and EF framework keep auto-generating classes and by using partial methods we can customize the class with our own logic.
Use number 3:- Better maintenance by compacting large classes 
The other important use of partial classes is for better maintenance of the project. If you have large classes with many methods as shown in the following figure, it's a bit of a pain to maintain those classes.
By using partial classes you can split them into physical files as shown in the following figure, thus improving your project and making it easier to maintain.
Use number 4:- Multiple people working on the same class
The last and final real time I see of partial classes is when we want two developers to work in the same class simultaneously. I agree this can be a very rare use as there are better options like using a version control software like TFS or Subversion, but in case you want something quick and local this option is not bad at all.
You can also watch my 500 videos on various technologies like .NET, C#, Silverlight, Azure, VSTS, WCF, WPF, WWF, Share Point, design patterns, UML, and much more.

Recommended Ebook

.NET Interview Questions and Answer: Practical Implementation

Download Now!
Similar Articles