Click here to Skip to main content
15,895,142 members
Articles / Programming Languages / C#
Tip/Trick

Refactoring Tips - Tip 4

Rate me:
Please Sign up or sign in to vote.
2.88/5 (8 votes)
1 Mar 2010CPOL 10.3K   1   3
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)


Written By
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 Pin
Ravi LVS28-Feb-10 21:26
Ravi LVS28-Feb-10 21:26 
If you provide some example code, then it will be easily understood by others. Sometimes, the terminology may not have been understood by everybody.
GeneralRe: Examples Pin
Vinayaka Krishna Shenoy1-Mar-10 4:19
Vinayaka Krishna Shenoy1-Mar-10 4:19 
GeneralRe: Examples Pin
Ravi LVS1-Mar-10 15:57
Ravi LVS1-Mar-10 15:57 

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

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