StructureMap has an easy to use Registry DSL that can be used in a Registry class. By using (one or more) Registry classes you can group all your IoC plumbing together.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FooBarRegistry : Registry | |
{ | |
public FooBarRegistry() | |
{ | |
For<IFoo>().Use<Foo>(); | |
For<IBar>().Use<Bar>(); | |
} | |
} |
Yesterday I had a situation where I wanted to use some object that was already registered in the IoC container in the registration of another class. This is possible by passing an IContext as a second parameter of the Use() method in the fluent API:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InfrastructureRegistry : Registry | |
{ | |
public InfrastructureRegistry() | |
{ | |
ForSingletonOf<IDocumentStore>().Use("Build the DocumentStore", (IContext context) => | |
{ | |
var options = context.GetInstance<ApplicationOptions>(); | |
var connectionString = options.Data.ConnectionString; | |
return DocumentStore.For(_ => | |
{ | |
_.Connection(connectionString); | |
}); | |
} | |
} | |
} |