Programming, Programming Stuffs

What Is Dependency Injection?

viagra purchase usa http://fisflug.is/?yrus=come-giocare-con-le-azione-binarie&4cb=ba Dependency Injection

get link Dependency Injection (DI) is a pattern to hold dependencies between class and also to create loosely coupled classes. Before explaining, what are loosely coupled let’s understand tightly coupled classes. For example, there are two classes ClassA and ClassB defined as bonding between them.

can you buy Lyrica over the counter See the code as ClassA associated with ClassB

rencontre ovni public class ClassA { public ClassB ObjB {get; set;} } public class ClassB { }

http://www.romagnamotorsport.it/?binarnewe=opzioni-binarie-60-rsi&c05=02 So ClassB accessible or connected to ClassA and this phenomena is called tightly coupled classes.

get link For loosely coupled classes defined as below

follow link public class ClassA { public IClassB ObjB { get; set; } } public interface IClassB { } public class ClassB : IClassB { }

http://captainaugust.com/?koooas=opzioni-digitali-su-iphone&e7a=5b Here you can see there is one additional interface added instead of concrete ClassB so ClassA not directly associated with ClassB and this type of structure between two classes called loosely coupled.

We can see above code and an interface is there, but every interface needs an instance of an implemented class as a reference and using that instance you can communicate with implementing a class and its members. Suppose ClassA constructor is responsible for providing an instance to interface IClassB as below

public class ClassA
{
  private IClass2 ObjB;
  public ClassA()
{
  ObjB = new ClassB();
}
  public IClassB ClassObjB{ get; set; }
}

public interface IClassB
{
}

public class ClassB : IClassB
{
}

You find here in the constructor of Class A still depends on Class B so this is not a loosely coupled so to avoid such tightly coupled, use a third class to eliminate such dependency and make ClassA as a loosely coupled class. So the third class introduces as Dependency Injection, to inject dependency in ClassA so now that class depends on DI to provide an instance of that particular dependent class. Here you can see Dependency Injection (DI) provided to ClassA and inject IClassB interface.

public class ClassA
{
 public readonly IClassB ObjB;

 public ClassA():this(DependencyFactory.Resolve<IClassB>())
 {

 }

 public ClassA(IClassB classB)
 {
  ObjB = classB;
 }
}

see url Why I use Dependency Injection?

Dependency Injection is useful when you have to implement lots of classes like error handling, unit testing or event handling in one class without doing any code for instance and use all classes like services.
So services as interfaces and also without knowing what is the implemented class behind and members of that class. So interface just provide information like an injection of dependency to get that service of the implementing class. Even you can get the benefit of common classes(services) provide to class and get the benefit of re-usability.

Leave a Reply

Your email address will not be published. Required fields are marked *