Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.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
C# Castle.Windsor IoC容器特定配置_C#_Dependency Injection_Inversion Of Control_Castle Windsor_Ioc Container - Fatal编程技术网

C# Castle.Windsor IoC容器特定配置

C# Castle.Windsor IoC容器特定配置,c#,dependency-injection,inversion-of-control,castle-windsor,ioc-container,C#,Dependency Injection,Inversion Of Control,Castle Windsor,Ioc Container,我已选择Castle.Windsor作为我应用程序的IoC容器。这是我的第一次IoC过期,所以我需要一个配置它的建议 应用程序的根类是调度程序。它计划并执行不同类型的“工作”。每个作业都实现了IWorker接口,因此我决定将列表注入调度程序。以后会有很多种“工作”,现在有两种:Cleaner和WriterScheduler需要一个Cleaner实例,所以默认的单身生活方式是可以的。但我还需要根据某个文件夹中XML文件的数量注入Writers的数量。在我的案例中,实现这一点的最佳模式是什么 配置容

我已选择Castle.Windsor作为我应用程序的IoC容器。这是我的第一次IoC过期,所以我需要一个配置它的建议

应用程序的根类是
调度程序
。它计划并执行不同类型的“工作”。每个作业都实现了
IWorker
接口,因此我决定将
列表
注入
调度程序
。以后会有很多种“工作”,现在有两种:
Cleaner
Writer
Scheduler
需要一个
Cleaner
实例,所以默认的单身生活方式是可以的。但我还需要根据某个文件夹中XML文件的数量注入
Writer
s的数量。在我的案例中,实现这一点的最佳模式是什么

配置容器:

var container = new WindsorContainer();
// For IList injecting
container.Kernel.Resolver.AddSubResolver(new ListResolver(container.Kernel, true));
// Registering Scheduler
container.Register(CastleRegistration.Component.For<IScheduler>().ImplementedBy<Scheduler>);
// Registering Workers
container.Register(CastleRegistration.Component.For<IWorker>().ImplementedBy<Writer>());
container.Register(CastleRegistration.Component.For<IWorker>().ImplementedBy<Writer>());
// ... there are multiple Writers depends on XML files count
container.Register(CastleRegistration.Component.For<IWorker>().ImplementedBy<Cleaner>());
// Resolving
var sched = container.Resolve<IScheduler>();
清洁剂:

public class Cleaner : IWorker
{
    public Cleaner()
    {
    }
}
更新: 我需要在每个
解析器中传递一个参数对象(从XML反序列化)。

我应该在容器配置中使用
foreach
循环吗?温莎的打字工厂能帮上忙吗?我需要一些指导方针。

基本上,你想做的是以下几点:

public class WriterFactory : IWriterFactory
{
    public Writer Create(string fileName)
    {
        return new Writer(fileName);
        //if your writers have other dependencies then inject those into the factory via the constructor and use them here
    }
}
那就注册这个工厂

container.Register(CastleRegistration.Component.For<IWriterFactory>().ImplementedBy<WriterFactory>());
container.Register(CastleRegistration.Component.For().ImplementedBy());

然后,在任何需要创建writer的地方,都可以依赖构造函数中的
IWriterFactory

实际上我看不出有什么问题。什么不起作用了?听起来好像你真的想要一个WriterFactory被注入,然后根据需要为你的文件创建Writerdirectory@Sam,看起来这就是我需要的Sam,你能用几行代码给出更复杂的答案吗?在这种情况下,你需要WriterFactory,如@SamHolder所述。在工厂内,您可以存储N个写入程序。如果你像以前一样注册Writer,你就是在Singleton lifestyle上注册,Castle会在你每次申请时为你提供第一次注册。好的,我知道了。工厂注册与此行之间是否存在任何差异:
container.Register(Component.For().AsFactory())?(来自城堡文档)。如果我说我必须尽量避开工厂,我说得对吗?更好的解决方案是当容器“构建”整个对象图时,如果我能做到这一点的话?我不太熟悉Castles特定的IOC容器,所以我不确定是否应该使用这种方法。你不应该避开工厂,当创建的对象依赖于运行时提供的动态数据时(如本例中的文件列表),你需要工厂。@ValeO我认为你的方法更好。始终尝试按容器创建实例。我不知道这会有什么帮助,但我解决了类似的问题,比如:@UfukHacıoğulları您的支付方法解析器只是一个生产支付方法的工厂。。。这种方法在这里不起作用,因为您无法提前知道目录中存在哪些文件。随着新文件的添加,这种情况可能会在运行时发生变化
public class WriterFactory : IWriterFactory
{
    public Writer Create(string fileName)
    {
        return new Writer(fileName);
        //if your writers have other dependencies then inject those into the factory via the constructor and use them here
    }
}
container.Register(CastleRegistration.Component.For<IWriterFactory>().ImplementedBy<WriterFactory>());