NoobDeveloper
NoobDeveloper

Reputation: 1887

Scanning with StructureMap

A project i am working on uses StructureMap. We have a code segment using something like this:

Scan(x =>
{
    try
    {
        x.Convention<SqlTypesConvention>();
        x.Assembly("ASSEMBLY NAME HERE");
    }
    catch
    {
    }
});

internal class SqlTypesConvention : IRegistrationConvention
{
    #region IRegistrationConvention Members

    public void Process(Type type, Registry registry)
    {
        string interfaceName = "I" + type.Name + "Data";
        Type interfaceType = type.GetInterface(interfaceName);

        if (type.IsAbstract || !type.IsClass || interfaceType.IsNullRef())
        {
            return;
        }
        registry.AddType(interfaceType, type);
    }

    #endregion IRegistrationConvention Members
}

What does this Scan and Convention code do?

Upvotes: 0

Views: 876

Answers (2)

Karthikeyan VK
Karthikeyan VK

Reputation: 6006

If there are any classes "XXX" that use the interface "IXXX" which starts with "I", then the structure map will scans for all the assembly for "xxxx" class and instantiates automatically. Detail information about how structure map works is available @ how structure map works

Upvotes: 1

Handcraftsman
Handcraftsman

Reputation: 6993

Frequently the interface for class Foo is named IFoo. Thus the convention is to prefix the class name with I.

The convention in your code sample is that for a given class Foo its interface will be named IFooData

StructureMap will use this convention to try to find the Interface relationships for all the Types. So, when a class constructor asks for an IFooData StructureMap will be able to provide an instance of Foo.

Upvotes: 2

Related Questions