Purpose of scan.TheCallingAssembly, scan.WithDefaultConventions in StructureMap-MVC3
Asked Answered
P

1

5

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.

Pentosan answered 16/5, 2012 at 12:20 Comment(0)
C
8

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.

Crunode answered 16/5, 2012 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.