Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of scan.TheCallingAssembly, scan.WithDefaultConventions in StructureMap-MVC3

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.

like image 640
Richard Ev Avatar asked May 16 '12 12:05

Richard Ev


1 Answers

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.

like image 78
Joshua Flanagan Avatar answered Sep 23 '22 22:09

Joshua Flanagan