StructureMap约定-注册表之间的区别.AddType和Registry.For(). use()



简短的问题。registry.AddType(pluginType, type);registry.For(pluginType).Use(type);存在一定的差异?

代码:

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

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

使用WhatDoIHave()我可以看到相同的:

使用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)
===============================================================================================================================================================================================================================================================================
使用的()

.Use ():

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

唯一的区别是在描述…

有人吗?

registry.AddType(pluginType, type)的调用基本上是将ForUse链接在一起的简写,如registry.For(pluginType).Use(type)

调用registry.AddType(pluginType, type)将导致直接调用Registry.alter.set,并同时指定插件类型和具体类型。

调用registry.For(pluginType).Use(type)ForUse连接在一起。调用For返回一个新的GenericFamilyExpression(构造函数调用Registry.alter.set获取接口类型),调用Use最终调用Registry.alter.set,使具体类型成为插件家族的默认类型。

请参阅Registry.cs和GenericFamilyExpression.cs的源代码,以及StructureMap源中的其他类。

接受的答案并不完全正确。它们不一样。令人惊讶的是,这是在答案的最后指出的。Use将为插件(类型)设置默认实例,而AddType不会。因此,您将不能使用structmap作为AddType的服务定位器。你可以有这个错误No default Instance is registered and cannot be automatically determined for type <type>

最新更新