Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dependency injection 重新使用Castle Windsor安装程序的注册?_Dependency Injection_Inversion Of Control_Castle Windsor_Ioc Container_Castle - Fatal编程技术网

Dependency injection 重新使用Castle Windsor安装程序的注册?

Dependency injection 重新使用Castle Windsor安装程序的注册?,dependency-injection,inversion-of-control,castle-windsor,ioc-container,castle,Dependency Injection,Inversion Of Control,Castle Windsor,Ioc Container,Castle,我们目前有两个web应用程序,一个是面向客户的前端应用程序,另一个是管理后端应用程序。我们注意到,这两个应用程序之间有很多注册是重复的。例如,RavenDb设置。例如,两个应用程序在asp.net global.asax中都有此代码 container.Register( Component.For<IDocumentStore>() .UsingFactoryMethod(x =>

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

        container.Register(
           Component.For<IDocumentStore>()
               .UsingFactoryMethod(x =>
               {
                   var docStore = new DocumentStore { ConnectionStringName = "RavenDB" };
                   docStore.Initialize();
                   return docStore;
               }).LifestyleSingleton()
           );
container.Register(
用于()的组件
.使用FactoryMethod(x=>
{
var docStore=newdocumentstore{ConnectionStringName=“RavenDB”};
初始化();
退货仓库;
}).生活方式单身()
);
我们将这段代码重构成一个安装程序,并将其放在一个名为CastleWindsor.Ravendbinshiller的程序集中,该程序集可以被两个应用程序引用和重用

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()
           );
    }
}
公共类Ravendbins安装程序:IWindsorInstaller
{
public void安装(IWindsorContainer、IConfigurationStore)
{
集装箱。登记(
用于()的组件
.使用FactoryMethod(x=>
{
var docStore=newdocumentstore{ConnectionStringName=“RavenDB”};
初始化();
退货仓库;
}).生活方式单身()
);
}
}
一切正常,但这是在应用程序之间重用注册逻辑的推荐方法吗??

另外,当独立程序集中的安装程序依赖于另一个类时会发生什么情况。这应该如何处理。例如,如果我的ravendb connectionstring不应硬编码,而应附加到ApplicationConfiguration类,该怎么办。如何处理CastleWindsor.RavendBinshiller程序集及其包含的安装程序类的依赖关系

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()
           );
    }
}
公共类Ravendbins安装程序:IWindsorInstaller
{
public void安装(IWindsorContainer、IConfigurationStore)
{
集装箱。登记(
用于()的组件
.使用FactoryMethod((c,y)=>
{

var connectionStringName=c.Resolve().connectionStringName;//如果要对后端和前端的IAApplicationConfiguration使用相同的implementation,则将其放置在CastleWindsor.RavendBinshiller程序集中是有意义的。否则,将不会。
干杯。

就我个人而言,我会把安装程序放在它引用的组件所在的程序集中。你不会解析你没有安装的组件,也不会解析你没有使用的组件,所以分离这两个元素对我来说真的没有意义。两个应用程序都应该引用一个使用RavenDB和包含安装元素

关于您对连接字符串的引用,我建议您在castle安装中添加一个自定义项,该自定义项能够从您需要的任何源解析您的连接字符串:这允许您在安装程序中解析连接字符串,而无需对外部组件进行引用。另一个好处是,如果您没有注册解析连接字符串的方法如果不必处理错误,解析将以连接字符串的名称显式失败

public class ConnectionStringResolver : ISubDependencyResolver
{
    public bool CanResolve(Castle.MicroKernel.Context.CreationContext context, ISubDependencyResolver contextHandlerResolver, Castle.Core.ComponentModel model, Castle.Core.DependencyModel dependency)
    {
        var connectionStringInformation = ConfigurationManager.ConnectionStrings[dependency.DependencyKey];

        return connectionStringInformation != null 
            && !string.IsNullOrEmpty(connectionStringInformation.ConnectionString)
              &&
              TypeDescriptor.GetConverter(dependency.TargetType)
               .CanConvertFrom(typeof(string));
    }

    public object Resolve(Castle.MicroKernel.Context.CreationContext context, ISubDependencyResolver contextHandlerResolver, Castle.Core.ComponentModel model, Castle.Core.DependencyModel dependency)
    {
        return TypeDescriptor
            .GetConverter(dependency.TargetType)
            .ConvertFrom(ConfigurationManager.ConnectionStrings[dependency.DependencyKey].ConnectionString);
    }
}
那么您的安装程序将是:

public void Install(IWindsorContainer container, IConfigurationStore store)
{
    container.Register(
       Component.For<IDocumentStore>()
           .UsingFactoryMethod((c, y) =>
           {
               var connectionStringName = container.Resolve("TheConnectionString");
               var docStore = new DocumentStore { ConnectionStringName = connectionStringName };
               docStore.Initialize();
               return docStore;
           }).LifestyleSingleton()
       );
}
public void安装(IWindsorContainer,IConfigurationStore)
{
集装箱。登记(
用于()的组件
.使用FactoryMethod((c,y)=>
{
var connectionStringName=container.Resolve(“连接字符串”);
var docStore=newdocumentstore{ConnectionStringName=ConnectionStringName};
初始化();
退货仓库;
}).生活方式单身()
);
}

您是否找到了令人满意的问题解决方案?