asp.net-mvc - 温莎城堡 : Register components across multiple projects in solution

标签 asp.net-mvc asp.net-mvc-3 dependency-injection castle-windsor

我想使用 CaSTLe Windsor 为我的由以下项目组成的解决方案进行依赖注入(inject):

  • MVC [ASP.NET MVC 3 Web 应用程序]:表示层(取决于业务和模型)
  • 商务 【类库】:业务层(依赖DataAccess和Models)
  • 数据访问 [类库]:数据访问层(取决于模型)
  • 型号 【类库】:模型层

  • 在业务层有一个类叫做 PostService实现IPostService管理博客文章。 PostsController Mvc 项目的依赖于 IPostService .但是,PostService (对应的具体实现)本身依赖于IPostRepository .

    我在哪里配置 CaSTLe Windsor 以返回 PostRepository 的实例解决IPostRepository ? Mvc 项目不知道 DataAccess 项目。因此,我无法在 global.asax 或 Mvc 中的其他地方配置组件绑定(bind)。

    [更新] 依赖图

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

    Project dependencies

    最佳答案

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

    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));
        }
    }
    

    然后在您的 Global.asax 中安装您的依赖项:
    protected virtual void Application_Start()
    {
        new WindsorContainer().Install(FromAssembly.InDirectory(new AssemblyFilter(HttpRuntime.BinDirectory)));
    }
    

    关于asp.net-mvc - 温莎城堡 : Register components across multiple projects in solution,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6244416/

    相关文章:

    c# - 使用 AspNet.Identity UserManager 时检测更改

    c# - 统一: Will get a StackOverflowException if I try to get a singleton that is registered in multiple mappings

    javascript - AngularJS:将服务注入(inject)指令?

    asp.net - 使用自定义成员资格提供程序进行依赖注入(inject)

    asp.net-mvc - 捕获路由错误

    asp.net-mvc - MVC 4中的脚手架 Controller ,错误: "Error: Unable to retrieve metadata for ' Model'.的解决方案参数 'connectionstring'不能为空

    javascript - 在 Javascript 中使用时,服务器端代码会自动对字符进行编码

    html - ASP.NET MVC - 浏览器不断记住不同模型的输入

    asp.net-mvc-3 - 我正在运行此错误时,mvc3中缺少程序集引用

    asp.net-mvc-3 - 如何在 MVC Razor View 中使用具有多个输入的内联模板