Ioc/DI - Why do I have to reference all layers/assemblies in application's entry point?

前端 未结 4 977
渐次进展
渐次进展 2020-11-21 04:25

(Related to this question, EF4: Why does proxy creation have to be enabled when lazy loading is enabled?).

I\'m new to DI, so bear with me. I understand that the con

4条回答
  •  说谎
    说谎 (楼主)
    2020-11-21 05:22

    If I wasn't using an DI container, I wouldn't have to reference EntityFramework library in my MVC3 app, only my business layer which would reference my DAL/Repo layer.

    You can create a seperate project called "DependencyResolver". In this project you have to reference all your libraries.

    Now the UI Layer doesn't need NHibernate/EF or any other not UI relevant library except of Castle Windsor to be referenced.

    If you want to hide Castle Windsor and DependencyResolver from your UI layer you could write an HttpModule which calls the IoC registry stuff.

    I have only an example for StructureMap:

    public class DependencyRegistrarModule : IHttpModule
    {
        private static bool _dependenciesRegistered;
        private static readonly object Lock = new object();
    
        public void Init(HttpApplication context)
        {
            context.BeginRequest += (sender, args) => EnsureDependenciesRegistered();
        }
    
        public void Dispose() { }
    
        private static void EnsureDependenciesRegistered()
        {
            if (!_dependenciesRegistered)
            {
                lock (Lock)
                {
                    if (!_dependenciesRegistered)
                    {
                        ObjectFactory.ResetDefaults();
    
                        // Register all you dependencies here
                        ObjectFactory.Initialize(x => x.AddRegistry(new DependencyRegistry()));
    
                        new InitiailizeDefaultFactories().Configure();
                        _dependenciesRegistered = true;
                    }
                }
            }
        }
    }
    
    public class InitiailizeDefaultFactories
    {
        public void Configure()
        {
            StructureMapControllerFactory.GetController = type => ObjectFactory.GetInstance(type);
              ...
        }
     }
    

    The DefaultControllerFactory doesn't use the IoC container directly, but it delegates to IoC container methods.

    public class StructureMapControllerFactory : DefaultControllerFactory
    {
        public static Func GetController = type =>
        {
            throw new  InvalidOperationException("The dependency callback for the StructureMapControllerFactory is not configured!");
        };
    
        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
            {
                return base.GetControllerInstance(requestContext, controllerType);
            }
            return GetController(controllerType) as Controller;
        }
    }
    

    The GetController delegate is set in a StructureMap Registry (in Windsor it should be an Installer).

提交回复
热议问题