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.
WithDefaultConventions
means: "if while scanning I find an interface IExample
, and there is a type Example
that implements IExample
, then register Example
as the default type for IExample
".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With