gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:39:02 25 4
gpt4 key购买 nike

我想使用 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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com