

Return "Property Injected: " + this.PiValue2 Return "Property Injected: " + this.PiValue1 Return "Property Injected: " + this.MyPropertyName Return "Constructor Injected: " + this.ConstructorInjected This.ConstructorInjected = myproperty1 + " & " + shape1.Name + " & " + myproperty2 + " & " + shape2.Name Public DIV2Controller(string myproperty1, IShape shape1, string myproperty2, IShape shape2) This.MethodInjected2 = myproperty1 + " & " + shape1.Name + " & " + myproperty2 + " & " + shape2.Name Public void Initialize2(string myproperty1, IShape shape1, string myproperty2, IShape shape2) This.MethodInjected1 = "Default Initialize done" I am covering most of the examples of Dependency Injection in ASP.NET Web API 2 public interface IShape So DI or Dependency Injection means to inject any object another might require. public class BankingSystemĪnd you can setup the library to inject a object into the constructor when it's created. Now let's say you have a class that relies on an ICalculator to be present you could have. IoC libraries can usually be configured to either hold a singleton or create a new instance every time you resolve a type. So now when you want an instance of an ICalculator you just. You would use a library like Unity to register Calculator to be returned when the type ICalculator is requested aka IoC (Inversion of Control) (this example is theoretical, not technically correct). Unity is a library like many others that allows you to get an instance of a requested type without having to create it yourself.

They can do more, but that's the thrust of it - they resolve dependencies for you, so you don't have to (and you don't have to use the "new" keyword throughout your code).įinal step: when you create your M圜lass, you would do this: var m圜lass = ObjectFactory.GetInstance() So what you've done is told the container, "When someone requests the IMyService, give them a copy of the SomeConcreteService." And you've also specified that when someone asks for a M圜lass, they get a concrete M圜lass. StructureMapConfiguration.ForRequestedType().TheDefaultIsConcreteType() Using StructureMap, configuring the container looks like this: StructureMapConfiguration.ForRequestedType().TheDefaultIsConcreteType() When you create the M圜lass with your container, your container will resolve the IMyService dependency for you. So with a constructor-based injection scheme, you just pass the interface to the IMyService dependency into the constructor. With an IoC container you "configure" the container to resolve those dependencies for you. And that is bad for a number of reasons (you've coupled your class to a specific concrete version of the IMyService, you can't unit test it easily, you can't change it easily, etc.) Without IoC, your class that relies on the IMyService has to new-up a concrete version of the service to use. A bit easier to grok, I think, when the IoC stuff is new to you.īasically, if you understand IoC then you understand that what you're doing is inverting the control for when an object gets created. Google StructureMap and try it out instead.
