Thursday, 22 November 2012

Design Principles : SOLID


Design Principals:
Yes, we know Design Patterns for coding level to resolve the problems while building the applications. as well Design Principals for building types(classes, Interfaces..more efficiently).

These are SOLID Prinicals:

1.      Single Responsibility Principle
2.      Open – Closed Principle
3.      Liscov Substitution Principle
4.      Interface Segregation Principle
5.      Dependency Inversion Principle


Single Responsibility Principle :

Which partitioning the types as per their responabilities.  
To avoid --à tightly coupled types. making the splitting the types as per their responabilities.
We can increase the reusability.

In .net
SqlConnection is responablity is to connect to Sql-server and which contains  Connection relavent
Members. Like connectiontimeout, WorkingStationId……ect.
this type is designed for connection oriented things only. As which reused in another types like

SqlConnection--SqlCommand,SqlDataAdapter types so the same code is reuseable here.

SqlConnection is independent implementation and reused by other types also.

Open – Closed Principle:


Software Entities (Classes, Modules, Functions, etc.) should be open for extension but closed for modifications.
Achived through : Abstraction and Polymorphism.
Abstraction: Abstraction helps us in making our code more extensible while polymorphism helps us in making our code close.
Ex:
DbDataAdapter-à IDataAdapter
public interface IDataAdapter
{
int Fill(DataSet dataSet);  // by default : virtual, public
}
Based on the type of DataAdapteràSqlDataAdapter,OracleDataAdapter…are implements DbDataAdapter.
Base on the type of DataAdapter(SqlDataAdapter,OracleDatapter..)
DbDataAdapter provides the underlaing implementation as per the DataAdapter.
Here
public interface IDataAdapter
{
int Fill(DataSet dataSet);  // by default : virtual, public ,abstract *****
}
Which
Class DbDataAdapter : DataAdapter
{
public override int Fill(DataSet dataSet);
}
SqlDataAdapter : DbDataAdapter
OracleDataAdapter : DbDataAdapter

Here Open for Extenbility-à by declaring as virtual(bydefault interface members).
And close in derived types(SqlDataAdapter,OracleDataAdapter…ect) in dervied types using Override Funcationality.
through Polymorphism(Interface based)this override funcationality is achived.

And which makes colsed from Modification using Polymorphism.

Liskov Substitution Principle:

Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it. (Polymorphism; important part highlighted)

public abstract class DbCommand
protected abstract DbConnection DbConnection { get; set; } //generic
sealed class SqlCommand : DbCommand,
   protected override DbConnection DbConnection { get; set; }
sealed class OracleCommand : DbCommand
protected override DbConnection DbConnection { get; set; }
All abstract members are by default Virtual so we can override in dervide typles like above.

Interface Segregation Principle:

Clients should not be forced to depend upon interfaces that they do not use.
Means -à Every interface is specific purpose. And their derived types specific members only should be defined in that interface.
So define the member relavent to a specific type with relavent members.
EX: IEnumerable---only  Movenext(), Reset(), Current ----relavent to IEnumarable collections.

Dependency Inversion Principle:

to avoid tightly copuled programming between types.

https://csharpsimplified.wordpress.com/2010/10/01/design-principle/

https://mlichtenberg.wordpress.com/category/software-development/


No comments:

Post a Comment