Do you have a wrapper for your IoC?

I have been using StructureMap for over a year now. And all this time I had a wrapper class called IoC, which looked like this:

class IoC {
    public static T GetInstance<T>()
    {
        return (T)GetInstance(typeof(T));
    }
    public static IEnumerable<T> GetAllInstances<T>()
    {
        return ObjectFactory.GetAllInstances<T>();
    }

    public static IEnumerable GetAllInstances(Type type)
    {
        return ObjectFactory.GetAllInstances(type);
    }

    public static object GetInstance(Type type)
    {
        return ObjectFactory.GetInstance(type);
    }

    public static void Inject<T>(T obj)
    {
        ObjectFactory.Inject(obj);
    }
}

I added a wrapper, suggesting that I might need to change the IoC container at some point in the line. At the moment, I think this is bad. One of the reasons: I cannot use ObjectFactory in my code to do other interesting things, I have to use this shell. Another thing: our code should not be really independent of the DependencyInjection container.

What are the advantages / disadvantages of using this approach?

+3
source share
3

Common Service Locator. DI , IoC. Simple Service Locator; DI, Common Service Locator.

, , DI. ( ) , , , , (: GetInstance). ASP.NET MVC ControllerFactory. ASP.NET WebForms PageHandlerFactory.

, ​​, . , , .

, , , , IoC, - . :

  • .
  • , .
  • .
  • , .
+6

, . - , , IOC. MVC. , , . http://mvcturbine.codeplex.com/

+1

, , IoC, .

, , IoC. , , IoC, , IoC.

+1
source

Source: https://habr.com/ru/post/1776629/


All Articles