Castle Windsor:在解决方案中注册多个项目的组件

Mar*_*ulz 3 asp.net-mvc dependency-injection castle-windsor asp.net-mvc-3

我想使用Castle Windsor作为我的解决方案的依赖注入,包括以下项目:

  1. Mvc [ASP.NET MVC 3 Web应用程序]:表示层(取决于业务模型)
  2. 业务 [类库]:业务层(取决于DataAccess模型)
  3. DataAccess [类库]:数据访问层(取决于型号)
  4. 模型 [类库]:模型层

在业务层中,有一个名为PostServiceimplementation 的类IPostService来管理博客帖子.在PostsController对中的mvc项目依赖IPostService.但是,PostService(相应的具体实现)本身依赖于IPostRepository.

我在哪里配置Castle Windsor以返回PostRepository要解决的实例IPostRepository?该的mvc项目不知道的数据访问项目.因此,我无法在global.asaxMvc中的其他位置配置组件绑定.


[更新]依赖关系图

现在我找到了解决方案(再次感谢Darin Dimitrov!)我想与您分享当前的依赖关系图.

项目依赖项

Kal*_*ier 9

在类库中创建和放置Windsor安装程序类(IWindsorInstaller接口实现),例如:

public class PostRepositoryInstaller: IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        var allTypesFromBinDir = AllTypes
            .FromAssemblyInDirectory(new AssemblyFilter(HttpRuntime.BinDirectory));

        container.Register(allTypesFromBinDir
            .BasedOn<IPostRepository>()
            .WithService.FromInterface()
            .Configure(c => c.LifeStyle.Transient));
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在您的Global.asax中安装依赖项:

protected virtual void Application_Start()
{
    new WindsorContainer().Install(FromAssembly.InDirectory(new AssemblyFilter(HttpRuntime.BinDirectory)));
}
Run Code Online (Sandbox Code Playgroud)