Hi Given the following code:
public interface IMyContext
{
string subtype { get; set; }
}
public class MyContext : IMyContext
{
public string subtype { get; set; }
}
public interface IMyExporter
{
string Export();
}
public class MyExporterXML : IMyExporter
{
public string Export()
{
return "";
}
}
public class MyExporterJson : IMyExporter
{
public string Export()
{
return "";
}
}
public class MyExporterFactory
{
private IMyContext context;
public MyExporterFactory(IMyContext context)
{
this.context = context;
}
public IMyExporter Create()
{
switch (context.subtype)
{
case "JSON" :
return new MyExporterJson();
default:
return new MyExporterXML();
}
}
}
public class MyService
{
private IMyContext context;
private IMyExporter exporter;
public MyService(IMyContext context, IMyExporter exporter)
{
this.context = context;
this.exporter = exporter;
}
public string Extractdata()
{
return exporter.Export();
}
}
[TestClass]
public class UnitTest2
{
[TestMethod]
public void TestMethod1()
{
var container = new WindsorContainer();
container.Register(Component.For<IMyContext>().ImplementedBy<MyContext>());
container.Register(Component.For<MyExporterFactory>());
container.Register(Component.For<MyService>());
container.Register(Component.For<IMyExporter>().UsingFactoryMethod(kernel
=> kernel.Resolve<MyExporterFactory>().Create()));
var context = container.Resolve<IMyContext>();
var service = container.Resolve<MyService>();
context.subtype = "JSON";
service.Extractdata();
}
}
Is there a way to have the injected exporter in the MyService resolved
at the time where it's actually used ?? Ie. when running the above
code, the exporter resolved is the MyExporterXML, but I really want's
it to be the MyExporterJson because of the context.subtype = "JSON"
setting. However the exporter is resolved before the subtype is set...
I know Castle::Windsor has something called delegate-based factories,
but I simply can't figure out how to use it....
Any help would be greatly appreciated, TIA
--
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en.