When adding the StructureMap-MVC3 package to an ASP.NET MVC application,
an IoC class containing an Initialize method gets added (that gets called by some code in the App_Start folder) containing the following:
public static class IoC
{
public static IContainer Initialize()
{
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
// x.For<IExample>().Use<Example>();
});
return ObjectFactory.Container;
}
}
What is the purpose of the scan.TheCallingAssembly() and scan.WithDefaultConventions() code? I can’t see a good explanation of these methods in the StructureMap documentation.
When using StructureMap in a non-MVC project I’ve found that the whole x.Scan section can be removed without having any impact.
Scanning looks at all the types defined in your Assembly, and applies StructureMap conventions to determine if/how they should be registered in the container.
WithDefaultConventionsmeans: “if while scanning I find an interfaceIExample, and there is a typeExamplethat implementsIExample, then registerExampleas the default type forIExample“.In many cases, you will be able to ask the container for whatever you are looking for (
IExample), and it will return an implementation, without any further configuration.