强制 StructureMap 在运行时插入填充的实例

Force StructureMap to Plug-In a populated Instance During Runtime

在单元测试期间,我使用模拟数据加载我的工作单元 (UOW) 实例。然后,我将工作单元实例交给正在测试的 class 个实例。

例如:
下面我用默认场景数据加载 UOW...然后...将 UOW 设置到新创建的 WorkfklowProvider(实例)

// ARRANGE
var unitOfWork = Builder.CreateInstance<MeasurementContractsUnitOfWork>();

// Load Data
Builder.DefaultDataScenario.Load(unitOfWork);

// at this point...the UOW is full of testable data

// Set Objects
var workflowProvider = Builder.CreateInstance<WorkflowProvider>();
workflowProvider.UnitOfWork = unitOfWork;

// at this point...I can do my tests

如您所见...使用更小的测试这不是问题。但是,对于具有更多实例的大型测试...它可能会变得非常“繁忙”。

例如:
因此,让我们看一个更大的测试...
看看我如何不得不在任何地方插入 UOW 实例?我希望避免这种情况。

// ARRANGE
var unitOfWork = Builder.CreateInstance<MeasurementContractsUnitOfWork>();

// Load Data
Builder.DefaultDataScenario.Load(unitOfWork);

// Set Objects
var workflowProvider = Builder.CreateInstance<WorkflowProvider>();
var builderATF = Builder.CreateInstance<AuthorizationToFlowDocumentDataSetBuilder>();
var providerATF = Builder.CreateInstance<AuthorizationToFlowDocumentProvider>();

workflowProvider.UnitOfWork = unitOfWork;
builderATF.UserManager.UnitOfWork = unitOfWork;
builderATF.WorkflowProvider = workflowProvider;
builderATF.UserMetaDataComponent.UnitOfWork = unitOfWork;
builderATF.UnitOfWork = unitOfWork;

providerATF.MeterProvider.UnitOfWork = unitOfWork;
providerATF.CommentingProvider.UnitOfWork = unitOfWork;
providerATF.DocumentParticipationProvider.UnitOfWork = unitOfWork;
providerATF.FavoritesManager.UnitOfWork = unitOfWork;
providerATF.UserManager.UnitOfWork = unitOfWork;
providerATF.UnitOfWork = unitOfWork;

问题:
有没有办法告诉 StructureMap 在运行时(在我填充 UOW 之后)将填充的工作单元 (UOW) 实例交还给所有 classes?

明确一点...一旦我填充了初始 UOW 实例...我希望 StructireMap 使用该实例进行所有未来的 CreateInstance 调用

事实证明,您所要做的就是使用 INJECT 方法。

[TestMethod]
public void Experimental_UnitTest()
{
    // STEP 1: Create a container within the scope of your individual UnitTest
    var container = IoC.Initialize();
    var unitOfWork = container.GetInstance<MeasurementContractsUnitOfWork>();

    // STEP 2: Load Your Data
    Builder.DefaultDataScenario.Load(unitOfWork);

    // STEP 3: "Inject" your instance
    container.Inject<MeasurementContractsUnitOfWork>(unitOfWork);

    // From here forward...all Instances created will get a copy of the populated UnitOfWork
    // This includes any PROPERTY objects that StructureMap creates within each instance too
    var builderRTF = container.GetInstance<RequestToFlowDocumentDataSetBuilder>();
    var builderATF = container.GetInstance<AuthorizationToFlowDocumentDataSetBuilder>();

}

对于那些可能想要“查看”Load 方法中发生了什么的人:
如您所见,您正在加载带有测试数据的工作单元...

public void Load(MeasurementContractsUnitOfWork unitOfWork)
{
    // Lookups
    Load_Area(unitOfWork);
    Load_State(unitOfWork);
    Load_County(unitOfWork);

    // Entities
    Load_Users(unitOfWork);
    
    // etc...
}

private void Load_Area(MeasurementContractsUnitOfWork unitOfWork)
{
    var collection = new List<Area>();

    collection.Add(new Area { Id = 1, AreaName = "East Texas", CreateUserId = CREATED_BY_ID, CreateDate = DateTime.Now, UpdateUserId = CREATED_BY_ID, UpdateDate = DateTime.Now, IsActive = true });
    collection.Add(new Area { Id = 2, AreaName = "Intrastate", CreateUserId = CREATED_BY_ID, CreateDate = DateTime.Now, UpdateUserId = CREATED_BY_ID, UpdateDate = DateTime.Now, IsActive = true });
    collection.Add(new Area { Id = 3, AreaName = "Louisana", CreateUserId = CREATED_BY_ID, CreateDate = DateTime.Now, UpdateUserId = CREATED_BY_ID, UpdateDate = DateTime.Now, IsActive = true });
    collection.Add(new Area { Id = 4, AreaName = "Midcon", CreateUserId = CREATED_BY_ID, CreateDate = DateTime.Now, UpdateUserId = CREATED_BY_ID, UpdateDate = DateTime.Now, IsActive = true });
    collection.Add(new Area { Id = 5, AreaName = "Northeast", CreateUserId = CREATED_BY_ID, CreateDate = DateTime.Now, UpdateUserId = CREATED_BY_ID, UpdateDate = DateTime.Now, IsActive = true });
    collection.Add(new Area { Id = 6, AreaName = "North Texas", CreateUserId = CREATED_BY_ID, CreateDate = DateTime.Now, UpdateUserId = CREATED_BY_ID, UpdateDate = DateTime.Now, IsActive = true });
    collection.Add(new Area { Id = 7, AreaName = "South Texas", CreateUserId = CREATED_BY_ID, CreateDate = DateTime.Now, UpdateUserId = CREATED_BY_ID, UpdateDate = DateTime.Now, IsActive = true });
    collection.Add(new Area { Id = 8, AreaName = "West Texas", CreateUserId = CREATED_BY_ID, CreateDate = DateTime.Now, UpdateUserId = CREATED_BY_ID, UpdateDate = DateTime.Now, IsActive = true });

    unitOfWork.Area.CreateList(collection);
}