Why Use IoC Infrastructure

I read about Inversion of Control infrastructures, and I just play with the question: "Why the hell do I need the infrastructure for this?"

They didnโ€™t understand my question ... the pattern is often used by programmers, but ... a fully functional structure for this?

I need to miss something and that I am asking a question. I have seen many examples on the Internet and I just do not understand. mi mind is locked for this idea.

Just take a look at the Ninject home page example:

public class Samurai { public IWeapon Weapon { get; private set; } public Samurai(IWeapon weapon) { Weapon = weapon; } } public class WarriorModule : NinjectModule { public override void Load() { Bind< IWeapon >.To< Sword >(); } } 

The Samurai class suits me. The "NinjectModule" structure seems unnecessary to me.

I assume that later in the code we will create new instances of "samurai" passing in instances of the "Sword", for example:

 Samurai theWarrior = new Samurai(WarriorModule.GetInstance(IWeapon));//no coupling 

which can be replaced by:

 Samurai theWarrior = new Samurai(new Sword());//still no coupling 

or

 Samurai theWarrior = new Samurai(GetWeaponFromXML());//no coupling yet 

Which part am I missing? Could you tell us about some scenario where the Ioc structure may be needed in my application?

Thanks.

UPDATE AFTER 4 ANSWERS : I really liked all the answers I received from you guys. I just read this dependency-injection-dissection / post where the guy uses it to test Unit and the StackOverflow link you just provided, and yes, I missed -big-big complexity, so let me set myself up to use the IoC infrastructure. Thanks again.

I would vote for your answers, but I just get an orange message that I canโ€™t.

Thanks to the guy who highlighted the code I posted.

+6
ioc-container ninject
source share
4 answers

Your example is pretty straight forward with only two layers, but when you have a real life example, it soon becomes messy:

 var form = new OrderEntryForm( new OrderService( new OrderReposity( new DatabaseGateway( new DataProviderFactory())))) 

An IoC container can do all the wiring for you, making your life a lot easier.

This is an example from a similar question, which concerns all the details: https://stackoverflow.com/questions/45191/ioc-explain-and-more-important-when-to-use-it/45197#45197

+4
source share

This is a problem with code samples. They are either complex, and you have not explained your point of view, or they are trivial, and then seem meaningless.

What WarriorModule does is bind a particular type to an interface, and therefore, when another class needs to implement this interface, it automatically receives it through the IoC container. A class with a dependency is independent of a particular type, and therefore has a lower grip and higher testability. I think you already knew that.

In replacement scenarios, the samurai class is not associated with the sword class, but the call code is still there. You can click on another level, but now this code will have a dependency, and your intermediate class will now have to distribute all the dependencies.

The IoC container does this for all mappings, placing it in only a few places (module classes). The rest of your code is free so as not to care, and not be dependent on specific types.

Hope this helps.

+6
source share

I recently started using Unity for IoC and I have no doubt that this makes my life easier.

I will just paste my code verbatim and hope you find it useful. Someone more experienced can give you more.

In addition, I found it helpful to read an article about DI Fowler.

 static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); using (IUnityContainer container = new UnityContainer()) { container .RegisterType<ILogger, StatSyncherFormView>(new myLife()) // GUI .RegisterType<IStatSyncherView, StatSyncherFormView>() // Services .RegisterType<ISalesForceWebServices, SalesForceWebServices1>() .RegisterInstance(new SalesForceWebServices1( "XXX", "XXX", "https://login.salesforce.com/services/Soap/c/19.0/0DF70000000CfHq", "https://na5-api.salesforce.com/services/Soap/s/19.0")) .RegisterType<ISalesForceService, SalesForceService1>() .RegisterType<IRestApiCall, RestApiCall1>() .RegisterType<IDirectTrackService, DirectTrackService1>() .RegisterType<IDAgentsService, DAgentsService1>() .RegisterType<IQuickBooksService, QuickBooksService1>(); StatSyncherPresenter p = container.Resolve<StatSyncherPresenter>(); Application.Run((Form)p.view); } } 

One of the things that I find most useful is:

 public class AnyClassCreatedByContainer { [Dependency] public ILogger Logger { get; set; } } 
+1
source share

Although Samurai theWarrior = new Samurai(new Sword()); there is still no connection, if you want all the samurai in your application to be <insert favourite weapon here> , you would need to change the entire call code to switch to a new weapon, whereas with IoC you would change this in one place , and all your samurai will use new weapons.

+1
source share

All Articles