Click here to Skip to main content
Click here to Skip to main content

Refactoring Tips - Tip 4

, 1 Mar 2010
Rate this:
Please Sign up or sign in to vote.
Refactoring Tips - Tip 4Tip 4 Based on one of the design principle - SRP( Single Responsibility Principle) There should be only one reason to change or break a classs.Dont mixup more than one functionality in a class.Bad practice Dont have domain and persistence logic in a...
Refactoring Tips - Tip 4
 
Tip 4


Based on one of the design principle - SRP( Single Responsibility Principle) There should be only one reason to change or break a classs.
 
Dont mixup more than one functionality in a class.
 
Bad practice
 
Dont have domain and persistence logic in a single class.
 
Following class- 'Customer' contains both domain logic and persistent logic,hence there are two reasons to change.(domain and data related) This is against the rules described above(SRP).
 
 
 public class Customer
 {
   //Attributes...
   //Behaviors (CRUD Operations)
   
   public void Create(some param)
   {
     //Writing code as shown below...

     //Creating Data Connection...
     // Opening connection...
     //Firing a Query against the data store
     //Processing the data...
     //Closing connection...
   }
 
   //Some more methods.....like Update,Delete,Read etc...
 }
Good practice
 
Have two sets of classes - one for domain and one for persistence
 
In the following snippet, we are having two set of classes one exclusive to the domain logic(Customer), which in turn gets the data ability through the repository class (which is exclusively to the data).
 
 
public class Customer
 {
   //Attributes...
   //Behaviors (CRUD Operations)
   ICustomerRepository rep;
   public Customer():this(new CustomerRepository())
   {
   }
   public customer(ICustomerRepository re)
   {
     rep = re;
   }
   
   public void Create(some param)
   {
     //To get the Data..
     rep.Create(some param);
   }
   //Some more methods.....like Update,Delete,Read etc...
 }
 
public interface ICustomerRepository 
{
 void Create(some param);
 //Some more methods...
}
public class CustomerRepository : ICustomerRepository 
{
 
 public void Create(some param)
 {
     //Creating Data Connection...
     // Opening connection...
     //Firing a Query against the data store
     //Processing the data...
     //Closing connection...
 }
//Some more methods ...like update,delete,read etc...
}
 
 
I hope this helps!.
 
Regards,
-Vinayak

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Vinayaka Krishna Shenoy
Architect MindTree Ltd
India India
Motivated achiever who guides organizations in applying technology to business settings, provides added value, and creates project deliverables in a timely manner. An experienced Technical Consultant, have successfully led large project teams of more than 20 people from requirements gathering to implementation and support using C#, .NET ,ADO.NET, ADO.NET Entity Framework,ASP.NET,ASP.NET MVC, WCF and SQL Server.

Comments and Discussions

 
GeneralExamples PinmemberRavi LVS28-Feb-10 21:26 
GeneralRe: Examples PinmemberVinayaka Krishna Shenoy1-Mar-10 4:19 
GeneralRe: Examples PinmemberRavi LVS1-Mar-10 15:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 1 Mar 2010
Article Copyright 2010 by Vinayaka Krishna Shenoy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid