Purpose of using interface in c#

+1 vote
asked Jun 7, 2013 in C#.NET by anonymous

So many times i tried to understand about interface, but I could not. I know that the interface is just a skeleton and it will not have any implementation. And we cannot even create instance.At least if we derive from a base class, that will have some methods which can be reused in child class. But when an interface does not have anything except the declarations why should I really use it? How to use it in c#?

Share

1 Answer

+1 vote
answered Jun 8, 2013 by anonymous
selected Jun 8, 2013 by administrator
 
Best answer

Interface is a very important concept in object oriented programming. Even though the interface is not having any definition it help us to implementing following functionality.
Abstraction, Dependency injection, Loose Coupling, Writing Unit Testing, Making sure the required methods are available in child class etc.

To understand interface, first we may need to understand how objects and scope of the objects work.

Abstraction:
For any class if we create object, the instance of the object can be hold by the same type of variable or by its base type.

public interface IClassA
{
   void MethodA();
}

public class ClassA : IClassA
{
  public void MethodA()
 {
    return "Method A";
 }
 public void MethodB()
 {
   return "Method B";
 }
}

In the above code, ClassA is a Class and I ClassA is an interface.
When an object instance of a class is stored in the same type variable then all of the methods and functions can be accessed by instance of that class.

ClassA objA = new ClassA();
using objA instance, we can access both MethodA and MethodB because instance is store in variable of type ClassA (Same Type of the instance)

But when the class instance is stored in the Base Class or BaseType variable, even though the instance is belong to child class, the instance cannot access all of its method instead, it can only access which are exposed by the Base Type.

ICalssA objAAbstracted = new ClassA();
even though the object objAAbstracted is instance of ClassA, we cannot access both MethodB because instance is store in variable of BaseType IClassA, so we can only access which is exposed by IClassA. In our case it is "MethodA"

Dependency Injection, Loose Coupling:
As per the dependency inversion principle, top level modules should not depend on lower level modules directly, instead it should depend on abstraction. So, if BL needs to talk to DAL, then BL should not directly depend on DAL. But, it can depend on the abstraction of DAL.

usually we write the code in our application as below

namespace BL
{
  public class CustomerBL()
  {
    public Customer GetCustomer()
    {
      CustomerDAL customerDAL = new CustomerDAL();
      return customerDAL.GetCustomer();
    }
  }
}

problem with the above code is, in future, if we want to change the customer DAL by another class, then we need to change the code in BL. that mean, BL and DAL both are tightly coupled. So, to make loose coupling between this, we can introduce interface. BL will be expecting DAL object of type interface, that DAL object can be passed by the UI layer (instead of creating DAL object in BL, create it in UI Layer) That interface needs to be implemented by the DAL.

namespace UI
{
  public class CustomerUI
  {
    public void GetCustomer()
    {
      CustomerBL customerBL = new CustomerBL();
      customerBL().GetCustomer(new CustomerDAL());
    }
  }
}

namespace BL
{
  public class CustomerBL
  {
    public Customer GetCustomer(ICustomerDAL customerDAL)
    {
      return customerDAL.GetCustomer();
    }
  }
}

namespace Infrastucture
{
  public interface ICustomerDAL
  {
    Customer GetCustomer();
  }
}

namespace DAL
{
  public class CustomerDAL: ICustomerDAL
  {
    public Customer GetCustomer()
    {
      //code to return customer detail
    }
  }
}

In the above code, BL is not any more depending on the DAL directly. Because now I can send any object which are implementing my interface.
This kind of implementation is going to help me to write unit testing as well for my BL.
Because now, I can create a fake DAL customer class "CustomerDALFake", implement ICustomerDAL interface.
Since I implement interface, I am also making sure that the Fake class must have GetCustomer method. Without that, out code will not even compile.
In my unit test project, I can create instance of the CutomerDALFake class and pass that as a ICustomerDAL instance to me CustomerBL and I can unit teat the GetCustomer() method in CustomerBL.

I hope the above explanation helps you understand about interface.

Your answer

Preview

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015 pinfaq.com
...