gpt4 book ai didi

dependency-injection - 重用 CaSTLe Windsor 安装程序的注册?

转载 作者:行者123 更新时间:2023-12-04 00:16:47 24 4
gpt4 key购买 nike

我们目前有 2 个 Web 应用程序,一个是面向客户的前端应用程序,另一个是管理后端应用程序。我们注意到,两个应用程序之间有很多重复的注册。例如,RavenDb 设置。例如,两个应用程序在 asp.net global.asax 中都有此代码

        container.Register(
Component.For<IDocumentStore>()
.UsingFactoryMethod(x =>
{
var docStore = new DocumentStore { ConnectionStringName = "RavenDB" };
docStore.Initialize();
return docStore;
}).LifestyleSingleton()
);

我们将这段代码重构为一个安装程序,并将其放置在一个名为 CaSTLeWindsor.RavenDbInstaller 的程序集中,两个应用程序都可以引用和重用该程序集。

public class RavenDbInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
Component.For<IDocumentStore>()
.UsingFactoryMethod(x =>
{
var docStore = new DocumentStore { ConnectionStringName = "RavenDB" };
docStore.Initialize();
return docStore;
}).LifestyleSingleton()
);
}
}

一切都很好,但这是在应用程序之间重用注册逻辑的推荐方法吗?

此外,当单独程序集中的安装程序依赖于另一个类时会发生什么情况。这个应该怎么处理。例如,如果我的 ravendb connectionstring 不应该被硬编码并且应该附加到 ApplicationConfiguration 类会怎样。我如何处理我的 CaSTLeWindsor.RavenDbInstaller 程序集及其包含的安装程序类的这种依赖性?

public class RavenDbInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
Component.For<IDocumentStore>()
.UsingFactoryMethod((c, y) =>
{
var connectionStringName = c.Resolve<IApplicationConfiguration>().ConnectionStringName; // <---- How do i deal with this dependency?
var docStore = new DocumentStore { ConnectionStringName = connectionStringName };
docStore.Initialize();
return docStore;
}).LifestyleSingleton()
);
}
}

最佳答案

如果您想为后端和前端使用相同的 IApplicationConfiguration 实现,那么将它放在 CaSTLeWindsor.RavenDbInstaller 程序集中是有意义的。否则不会。干杯。

关于dependency-injection - 重用 CaSTLe Windsor 安装程序的注册?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28152312/

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