从另一个项目覆盖StructureMap注册表
问题描述:
我有Unit Test
项目&想要为Unit of Work
类中的存储库实现存根实现。我会知道如何使用其他 IoC技术,但对StructureMap来说是新的。从另一个项目覆盖StructureMap注册表
- 我如何重新定义&代用品的定义从我的单元测试项目
- 是什么样子里面?
我很难在此找到好的文档。
默认容器:
下面是业务注册,我希望重写的一部分。
public ContainerRegistry()
{
Scan(...);
// --------
// UNIT OF WORK
// This is the DEFAULT implementation
For(typeof(IRepository<>)).Use(typeof(GenericRepository<>));
For<IUnitOfWork>().Use<MeasurementContractsUnitOfWork>();
}
单元测试:
在单元测试项目,我想国际奥委会,而不是使用:
// This is the STUB Implementation
For(typeof(IRepository<>)).Use(typeof(GenericRepositoryStub<>));
目标:
目的是简单地使用容器,正常情况下,但自动获取STUB自动填充。
var container = IoC.Initialize();
var uow = container.GetInstance<IUnitOfWork>()
答
我不确定这是否是正确的做法,但这是我如何实现我的目标。如果有人给出了一个更好的答案,我将迎来他们是正确的:
我创建单独IOC Initializer
& ContainerRegistry
类Unit Test
项目中,像这样......
初始化类:
public static class IoC
{
public static IContainer Initialize()
{
var container = new Container();
// NOTE: If you have many Registries to consider, you can add them (order matters)
// The Business Registry
container.Configure(x => x.AddRegistry<Business.DependencyResolution.ContainerRegistry>());
// The UnitTest Projects Registry (order matters)
container.Configure(x => x.AddRegistry<ContainerRegistry>());
return container;
}
}
REGISTRY CLASS:
public class ContainerRegistry : Registry
{
public ContainerRegistry()
{
// The Override
For(typeof(IRepository<>)).Use(typeof(GenericRepositoryIdentifiableStub<>));
}
}