1

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?

jlembke
  • 13,217
  • 11
  • 42
  • 56
Javier Ros
  • 3,511
  • 2
  • 21
  • 41
  • In your example, you are specifying `Singleton()` in the first version of `BasicConvention.Process`, but not in the second version. This doesn't affect the question overall, but it does make for different Lifecycle values (Transient vs. Singleton) in the `WhatDoIHave()` results. – hunch_hunch Aug 20 '14 at 19:15
  • Yes, doesn't affect the question. – Javier Ros Aug 21 '14 at 10:13

2 Answers2

0

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.

hunch_hunch
  • 2,283
  • 1
  • 21
  • 26
0

The accepted answer is not entirely correct. They are not the same. Which is surprisingly is indicated at the end of an answer. Use will set default instance for plugin (type) and AddType will not. So you will not be able to use Structure map as service locator with AddType. You can have this error No default Instance is registered and cannot be automatically determined for type <type>

Dmitriy
  • 638
  • 1
  • 6
  • 12