StructureMap 中的 AddRegistry 和 IncludeRegistry 有什么区别?
what is the difference between AddRegistry and IncludeRegistry in StructureMap?
StructureMap中的Registry
class有一个IncludeRegistry<T>() where T : Registry, new()
方法可以在新建registry实例时使用,如:
class MyRegistry : Registry
{
public MyRegistry ()
{
IncludeRegistry<AnotherRegistry>();
//For<>(...).Use<>(...); ... etc
}
}
继承自Registry
的ConfigurationExpression
class,除此之外还有一个AddRegistry<T>() where T : Registry, new()
方法。
它们之间的(实际)区别是什么?在创建容器时我应该更喜欢哪一个,例如:
var myContainer = new Container(conf =>
{
conf.AddRegistry<RegistryA>();
conf.IncludeRegistry<RegistryB>();
});
我在 StructureMap 文档中找不到任何关于差异的描述,因此非常感谢任何见解。
IncludeRegistry
旨在与注册表一起使用,以导入另一个注册表的内容。
Registry
中方法的 XML documentation 说:
Imports the configuration from another registry into this registry
Here 是文档中的一个示例:
var registry = new Registry();
registry.IncludeRegistry<YellowBlueRegistry>();
AddRegistry
用于配置容器时的配置表达式。它创建(当使用通用重载时)注册表并将配置从它导入容器。
ConfigurationExpression::AddRegistry<T>
的 XML documentation 说:
Creates and adds a Registry object of type T
Here 是文档中的一个示例:
var container2 = new Container(c => { c.AddRegistry<FooBarRegistry>(); });
令人困惑的是 ConfigurationExpression
继承了 Registry
,因此具有这两种方法。
简而言之,对于容器配置(根据文档和测试用例),您应该使用:
var myContainer = new Container(conf =>
{
conf.AddRegistry<RegistryA>();
});
编辑:
Jeremy 在询问时的回复是:
Had to look at the code. It's unlikely that you'd see any difference
in behavior, but AddRegistry
should have been removed for 4. I
recommend you only use IncludeRegistry
StructureMap中的Registry
class有一个IncludeRegistry<T>() where T : Registry, new()
方法可以在新建registry实例时使用,如:
class MyRegistry : Registry
{
public MyRegistry ()
{
IncludeRegistry<AnotherRegistry>();
//For<>(...).Use<>(...); ... etc
}
}
继承自Registry
的ConfigurationExpression
class,除此之外还有一个AddRegistry<T>() where T : Registry, new()
方法。
它们之间的(实际)区别是什么?在创建容器时我应该更喜欢哪一个,例如:
var myContainer = new Container(conf =>
{
conf.AddRegistry<RegistryA>();
conf.IncludeRegistry<RegistryB>();
});
我在 StructureMap 文档中找不到任何关于差异的描述,因此非常感谢任何见解。
IncludeRegistry
旨在与注册表一起使用,以导入另一个注册表的内容。
Registry
中方法的 XML documentation 说:
Imports the configuration from another registry into this registry
Here 是文档中的一个示例:
var registry = new Registry();
registry.IncludeRegistry<YellowBlueRegistry>();
AddRegistry
用于配置容器时的配置表达式。它创建(当使用通用重载时)注册表并将配置从它导入容器。
ConfigurationExpression::AddRegistry<T>
的 XML documentation 说:
Creates and adds a Registry object of type T
Here 是文档中的一个示例:
var container2 = new Container(c => { c.AddRegistry<FooBarRegistry>(); });
令人困惑的是 ConfigurationExpression
继承了 Registry
,因此具有这两种方法。
简而言之,对于容器配置(根据文档和测试用例),您应该使用:
var myContainer = new Container(conf =>
{
conf.AddRegistry<RegistryA>();
});
编辑:
Jeremy 在询问时的回复是:
Had to look at the code. It's unlikely that you'd see any difference in behavior, but
AddRegistry
should have been removed for 4. I recommend you only use IncludeRegistry