2
votes

I want to configure structuremap to create a service using a factory class. The factory itself has a dependency that needs filling. Currently I have the following in my Registry class:

For<IDoStuffWebService>().Singleton().Use(() => 
    new DoStuffWebServiceClientFactory(new ConfigProvider()).Create()
);

Rather than having to hard-code the concrete type DoStuffWebServiceClientFactory and manually fill it's dependency, I want structuremap to get this for me (it implements IDoStuffWebServiceClientFactory). It looks like IContext might help (http://docs.structuremap.net/UsingSessionContext.htm), but I'm struggling to figure out how this fits.

Any help greatly appreciated. Roger.

1

1 Answers

6
votes

To use the structuremap context in a Use method you can use the overload method that have a context as a parameter.

For<IDoStuffWebService>().Singleton().Use(ctx => ctx.GetInstance<IDoStuffWebServiceClientFactory>().Create());