Castle Windsor: Register components across multiple projects in solution

后端 未结 4 1936
自闭症患者
自闭症患者 2021-01-13 15:48

I would like to use Castle Windsor for dependency injection for my solution consisting of the following projects:

  1. Mvc [ASP.NET MVC 3 Web Appli
4条回答
  •  梦毁少年i
    2021-01-13 16:14

    Create and place Windsor installer classes (IWindsorInstaller interface implementations) in your class libraries, for example:

    public class PostRepositoryInstaller: IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            var allTypesFromBinDir = AllTypes
                .FromAssemblyInDirectory(new AssemblyFilter(HttpRuntime.BinDirectory));
    
            container.Register(allTypesFromBinDir
                .BasedOn()
                .WithService.FromInterface()
                .Configure(c => c.LifeStyle.Transient));
        }
    }
    

    Then in your Global.asax install your dependencies:

    protected virtual void Application_Start()
    {
        new WindsorContainer().Install(FromAssembly.InDirectory(new AssemblyFilter(HttpRuntime.BinDirectory)));
    }
    

提交回复
热议问题