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

Tagged as

Refactoring Tips - Tip 5

, 6 Mar 2010
Rate this:
Please Sign up or sign in to vote.
Refactoring Tips - Tip 5Tip 5Always Programme for interface/abstract than concrete classes, Which will gives the option for extension, loose coupling and plugin.This will also be in line with one of the design principle - LSP(Liskov's substitution Principle) - in simple word it...
Refactoring Tips - Tip 5
 
Tip 5
 
Always Programme for interface/abstract than concrete classes, Which will gives the option for extension, loose coupling and plugin.
 
This will also be in line with one of the design principle - LSP(Liskov's substitution Principle) - in simple word it summarises that, Every subclass should be substitutable for a base class.
 
For eg: if i have to write a data persistent class.
 
Bad practice
 

 public class SqlRepository
 {
  void Crete(parameters)
  {
    //Logic to get the data...
  }
  void Delete(id)
  {
   //Logic to delete the data based on the id
  }
  //Some more specific methods 
 }
 
Good practice
 
 public interface IRepository
 {
   void Create(object);
   void Delete(id);
   //Some more methods...
 }
 
 public class SqlRepository :IRepository
 {
   void Crete(parameters)
   {
    //Logic to get the data...
   }
   void Delete(id)
  {
   //Logic to delete the data based on the id
  }
 
  //Some more specific methods

 }
 
This arrangement gives us the flexibility that tomorrow, if i want to write a repostory for some other store then also i can write without much impact with the exisiting clients.
 
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)

Share

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

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 6 Mar 2010
Article Copyright 2010 by Vinayaka Krishna Shenoy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid