Usage of IoC Containers; specifically Windsor

后端 未结 5 1846
孤独总比滥情好
孤独总比滥情好 2020-11-29 04:07

I think the answer to this question is so obivous that noone has bothered writing about this, but its late and I really can\'t get my head around this.

I\'ve been re

相关标签:
5条回答
  • 2020-11-29 04:34

    As the other answers here state there are a lot of choices, and again we're left to ourselves to figure out what is best in our case.

    That said, IMO having a global container that is accessed throughout your application somewhat breaks isolation in that a lot of code now depends on the one global class. Also, for applications that is split out into several assemblies, the global container must be made accessible to all these assemblies.

    With Unity you can actually have a IUnityContainer parameter in your constructor and the container will automatically inject itself into the instance when you resolve the class. This way, for services that needs to resolve other services you pass in the container instead of forcing the class to reference the external class.

    Not sure how other frameworks support this scenario (Windsor will inject IKernel).

    0 讨论(0)
  • 2020-11-29 04:44

    I'm using an implementation of this interface:

    public interface IResolver
    {
        object Resolve(Type type);
        object Resolve(string name);
    
        T Resolve<T>() where T : class;
        T Resolve<T>(string name) where T : class;
    }
    

    Which is actually wrapped in global static class, for example:

    public static class Resolver // : IResolver
    {
        private static IResolver _current;
    
        public static object Resolve(Type type)
        {
            return Current.Resolve(type);
        }
    
        public static object Resolve(string name)
        {
            return Current.Resolve(name);
        }
    
        public static T Resolve<T>() where T : class
        {
            return Current.Resolve<T>();
        }
    
        public static T Resolve<T>(string name) where T : class
        {
            return Current.Resolve<T>(name);
        }
    
        private static IResolver Current
        {
            get
            {
                if (_current == null)
                {
                    _current = new SpringResolver();
                }
    
                return _current;
            }
        }
    }
    

    Also I'm trying to follow the simple rule - use Resolver class as less as possible, instead inject services in objects that need those services.

    0 讨论(0)
  • 2020-11-29 04:46

    Generally you want to keep only one instance for the lifetime of the entire application. What I do most of the time is I initialize the container when the app starts, and then I use typed factories for container-unaware pulling of objects.

    Other popular approach is to wrap the container instance with static class and use that static class to access your (singleton) container. You can find an example of that in Ayende's Rhino.Commons library here. This approach however has severe drawbacks and should be avoided.

    0 讨论(0)
  • 2020-11-29 04:52

    99% of the cases it's one container instance per app. Normally you initialize it in Application_Start (for a web app), like this.

    After that, it's really up to the consumer of the container. For example, some frameworks, like Monorail and ASP.NET MVC allow you to intercept the creation of the instances (the controllers in this case), so you just register the controllers and their dependencies in the container and that's it, whenever you get a request the container takes care of injecting each controller with its dependencies. See for example this ASP.NET MVC controller. In these frameworks, you hardly ever need to call or even reference the container in your classes, which is the recommended usage.

    Other frameworks don't let you get in the creation process easily (like Webforms) so you have to resort to hacks like this one, or pull the required dependencies (that is, explicitly calling the container). To pull dependencies, use a static gateway to the container like this one or the one described by maxnk. Note that by doing this, you're actually using the container as a Service Locator, which doesn't decouple things as well as inversion of control. (see difference here and here)

    Hope this clears your doubts.

    0 讨论(0)
  • 2020-11-29 04:52

    I use this example from Michael Puleio's blog on using a HttpModule to handle the build up of my dependencies using Unity. http://blogs.msdn.com/mpuleio/archive/2008/07/17/proof-of-concept-a-simple-di-solution-for-asp-net-webforms.aspx

    0 讨论(0)
提交回复
热议问题