c# - Castle.Windsor IoC-container specific configuration -
i have chosen castle.windsor ioc container app. first ioc expirience need advice configuring it.
the root class of app scheduler. plans , performs different kinds of "jobs". each job implements iworker interface, decided inject list<iworker> scheduler. there many kind of "jobs" later, there two: cleaner , writer. scheduler needs single instance of cleaner, default singleton lifestyle ok. need inject number of writers depends on count of xml files in folder. optimal pattern achieve in case?
configuring container:
var container = new windsorcontainer(); // 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 multiple writers depends on xml files count container.register(castleregistration.component.for<iworker>().implementedby<cleaner>()); // resolving var sched = container.resolve<ischeduler>(); scheduler:
public class scheduler : ischeduler { public ilist<iworker> workers { get; set; } public scheduler(ilist<iworker> workers) { workers = workers; } } writer:
public class writer : iworker { public string source { get; set; } public writer() { } public writer(string source) { source = source; } } cleaner:
public class cleaner : iworker { public cleaner() { } } update: need pass parameter object (deserialized xml) in each of parsers. should use foreach loop in container configuration? can windsor's typed factory here? need guideline.
basically want along these lines:
public class writerfactory : iwriterfactory { public writer create(string filename) { return new writer(filename); //if writers have other dependencies inject factory via constructor , use them here } } then register factory
container.register(castleregistration.component.for<iwriterfactory>().implementedby<writerfactory>()); then anywhere need create writer take dependency on iwriterfactory in constructor
Comments
Post a Comment