This post is a guide to configuring Subsonic with Structuremap to ensure the easiest TDD experience possible.
The first thing I did was generate the code using the SubSonic Linq Templates.
Next, it’s just a matter of telling StructureMap all about how to make an instance of SubSonicRepository<T>.
Here’s the goo that you need to make it all wire up:
public static void ConfigureStructureMap()
{
ObjectFactory.Initialize(x =>
{
x.UseDefaultStructureMapConfigFile = false;
x.Scan(scan =>
{
scan.WithDefaultConventions();
scan.TheCallingAssembly();
scan.AssemblyContainingType<IRepository>(); //Scans the Subsonic.Core assembly
});
x.ForRequestedType<IQuerySurface>().TheDefault.Is.ConstructedBy(c => new SomeDB()); //Gen'd by SubSonic
x.ForRequestedType<IDataProvider>().TheDefault.Is.ConstructedBy(c => ProviderFactory.GetProvider("A_Name_Of_A_Connection_In_Config"));
x.ForRequestedType(typeof(IRepository<>)).TheDefaultIsConcreteType(typeof(SubSonicRepository<>));
}
);
}
- IRepository<T> and SubSonicRepository<T> are both shipped in the SubSonic.Core DLL, so there’s no real work to do there.
- SomeDB() is a class generated by the Linq Templates mentioned above.
- The string passed to ProviderFactory.GetProvider(“”) is just the name of a connection that you define in your .config file. You’ll see all this in the examples you downloaded when getting the bits.
- The last line of the config tells StructureMap to map the open generic type to a concrete generic type. For instance, when you request IRepository<Customer> StructureMap will give you a Repository<Customer>.
Now you can use constructor injection or ObjectFactory.GetInstance<T>() to get a concrete ready-to-go SubSonic repository.