A factory method is useful when you need fine-grained control, when you need an instance. However, you should not directly depend on the container itself, but rather introduce the factory method as a dependency. Here is an example to illustrate this:
public class SomeController { private Func<ISomeService> _serviceFactory; public SomeController(Func<ISomeService> serviceFactory) { _serviceFactory = serviceFactory; } public void DoSomeWork() { var service = _serviceFactory(); .... } }
The StructureMap registration code will look something like this:
var container = new Container(cfg => cfg.For<ISomeService>().Use(() => new SomeServiceImpl()) );
Peter Lillevold
source share