当前,我们有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连接字符串不应该被硬编码并且应该附加到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()
);
}
}
最佳答案
如果要为后端和前端对应用程序配置使用相同的实现,则将其放置在CastleWindsor.RavenDbInstaller程序集中是有意义的。否则不行。
干杯。
关于dependency-injection - 重用CaSTLe Windsor安装程序的注册?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28152312/