structuremapstructuremap3

StructureMap Convention - Differences between Registy.AddType and Registry.For().Use()


Short question. Exists some diference between registry.AddType(pluginType, type); and registry.For(pluginType).Use(type); ?

Code:

public class BasicConvention : ConfigurableRegistrationConvention
{
    public override void Process(Type type, Registry registry)
    {
            if (something)
                registry.For(pluginType).Use(type).Singleton();
        }
    }
}

and

public class BasicConvention : ConfigurableRegistrationConvention
{
    public override void Process(Type type, Registry registry)
    {
            if (something)
                registry.AddType(pluginType, type);
        }
    }
}

Using WhatDoIHave() I can see the same:

Using AddType:

===============================================================================================================================================================================================================================================================================
PluginType                  Namespace                          Lifecycle     Description                                                                                                                                               Name                                    
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ISession                    Paf.Application.Session            Transient     Paf.Application.Session ('Paf.Application.Session, Paf.Modules.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null')                              Paf.Application.Session,... (Default)
===============================================================================================================================================================================================================================================================================

Using For().Use():

===============================================================================================================================================================================================================================================================================
PluginType                  Namespace                          Lifecycle     Description                                                                                                                                               Name                                    
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ISession                    Paf.Application.Session            Singleton     Paf.Application.Session                                                                                                                                (Default)                               
===============================================================================================================================================================================================================================================================================

The only difference is in the description ...

Somebody?


Solution

  • The call to registry.AddType(pluginType, type) is basically shorthand for chaining together For and Use as in registry.For(pluginType).Use(type).

    Calling registry.AddType(pluginType, type) causes a direct call to Registry.alter.set with the plugin type and the concrete type specified together.

    Calling registry.For(pluginType).Use(type) chains together For and Use. The call to For returns a new GenericFamilyExpression (the constructor calls Registry.alter.set for the interface type), and the call to Use ends up calling Registry.alter.set to make the concrete type the default for the plugin family.

    See the source code for Registry.cs and GenericFamilyExpression.cs, and other classes in the StructureMap source.