C# Structuremap-覆盖注册
是否可以在注册表中注册接口,然后“重新注册”以覆盖第一次注册 即:C# Structuremap-覆盖注册,c#,dependencies,registry,structuremap,code-injection,C#,Dependencies,Registry,Structuremap,Code Injection,是否可以在注册表中注册接口,然后“重新注册”以覆盖第一次注册 即: For().Use(); For().Use(); 在运行时,我想让我的对象工厂在我请求ISomeInterface时返回SomeClassExtension 提前谢谢 好消息,我发现是的。这完全取决于注册表规则添加到对象工厂容器的顺序。因此,如果您像我一样使用多个注册表类,则需要找到一种方法来赋予优先级,以便将它们添加到容器中 换句话说,不要使用以错误顺序获取所有注册表类的.LookForRegistries(),而是尝试查
For().Use();
For().Use();
在运行时,我想让我的对象工厂在我请求ISomeInterface
时返回SomeClassExtension
提前谢谢 好消息,我发现是的。这完全取决于注册表规则添加到对象工厂容器的顺序。因此,如果您像我一样使用多个注册表类,则需要找到一种方法来赋予优先级,以便将它们添加到容器中 换句话说,不要使用以错误顺序获取所有
注册表
类的.LookForRegistries()
,而是尝试查找所有注册表
文件,按所需顺序设置它们,并手动将它们添加到对象工厂容器中:
ObjectFactory.Container.Configure(x => x.AddRegistry(registry));
这样,你就可以完全控制你想要的规则
希望它能有所帮助:)我只是想在SpecFlow测试中覆盖注册表的某些部分时添加解决方案 我在搜索过程中很早就找到了这条线索,但它并没有真正帮助我找到解决方案,所以我希望它能帮助你 我的问题是“StoreRegistry”(由应用程序使用)中的“DataContext”使用“HybridHttpOrthReadLocalScope”,我需要它在测试中是“瞬态的”
The code looked like this:
[Binding]
public class MySpecFlowContext
{
...
[BeforeFeature]
private static void InitializeObjectFactories()
{
ObjectFactory.Initialize(x =>
{
x.AddRegistry<StoreRegistry>();
x.AddRegistry<CommonRegistry>();
});
}
}
代码如下所示:
[有约束力]
公共类MySpecFlowContext
{
...
[之前特写]
私有静态void initializeObjectFactorys()
{
ObjectFactory.Initialize(x=>
{
x、 AddRegistry();
x、 AddRegistry();
});
}
}
要覆盖范围设置,您需要在注册中显式设置它。
并且覆盖需要低于被覆盖的值
The working code looks like this:
[Binding]
public class MySpecFlowContext
{
...
[BeforeFeature]
private static void InitializeObjectFactories()
{
ObjectFactory.Initialize(x =>
{
x.AddRegistry<StoreRegistry>();
x.AddRegistry<CommonRegistry>();
x.AddRegistry<RegistryOverrideForTest>();
});
}
class RegistryOverrideForTest : Registry
{
public RegistryOverrideForTest()
{
//NOTE: type of scope is needed when overriding the registered classes/interfaces, when leaving it empty the scope will be what was registered originally, f ex "HybridHttpOrThreadLocalScoped" in my case.
For<DataContext>()
.Transient()
.Use<DataContext>()
.Ctor<string>("connection").Is(ConnectionBuilder.GetConnectionString());
}
}
}
工作代码如下所示:
[有约束力]
公共类MySpecFlowContext
{
...
[之前特写]
私有静态void initializeObjectFactorys()
{
ObjectFactory.Initialize(x=>
{
x、 AddRegistry();
x、 AddRegistry();
x、 AddRegistry();
});
}
类RegistryOverrideForTest:注册表
{
公共注册表OverrideForTest()
{
//注意:重写已注册的类/接口时需要作用域的类型,将其保留为空时,作用域将是最初注册的,在本例中为“HybridHttpOrthReadLocalScope”。
For()
.Transient()
.Use()
.Ctor(“连接”).Is(ConnectionBuilder.GetConnectionString());
}
}
}
The working code looks like this:
[Binding]
public class MySpecFlowContext
{
...
[BeforeFeature]
private static void InitializeObjectFactories()
{
ObjectFactory.Initialize(x =>
{
x.AddRegistry<StoreRegistry>();
x.AddRegistry<CommonRegistry>();
x.AddRegistry<RegistryOverrideForTest>();
});
}
class RegistryOverrideForTest : Registry
{
public RegistryOverrideForTest()
{
//NOTE: type of scope is needed when overriding the registered classes/interfaces, when leaving it empty the scope will be what was registered originally, f ex "HybridHttpOrThreadLocalScoped" in my case.
For<DataContext>()
.Transient()
.Use<DataContext>()
.Ctor<string>("connection").Is(ConnectionBuilder.GetConnectionString());
}
}
}