Last active
April 16, 2020 07:34
-
-
Save emanuelefirmani/e4014b5b21d2e8f27a3248dca2e04da4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection; | |
using Autofac; | |
using Autofac.Features.OwnedInstances; | |
using FluentAssertions; | |
using KPMG.ITS.ERP.ETL.Customer; | |
using KPMG.ITS.ERP.ETL.DB; | |
using KPMG.ITS.ERP.ETL.UpdatingLog; | |
using Xunit; | |
namespace KPMG.ITS.ERP.ETL.Test.IoCTests | |
{ | |
public class UnitOfWorkTest | |
{ | |
[Fact] | |
public void should_resolve_UnitOfWork() | |
{ | |
var builder = new ContainerBuilder(); | |
builder.RegisterModule<TestModule>(); | |
var container = builder.Build(); | |
var caller = container.Resolve<Caller>(); | |
var cuDbContext1 = GetEtlDbContext<CustomerUpdater>(caller.Owned1.Value, "_customerUpdater", "_dbContext"); | |
var uluDbContext1 = GetEtlDbContext<UpdatingLogUpdater>(caller.Owned1.Value, "_updatingLogUpdater", "_context"); | |
var cuDbContext2 = GetEtlDbContext<CustomerUpdater>(caller.Owned2.Value, "_customerUpdater", "_dbContext"); | |
var uluDbContext2 = GetEtlDbContext<UpdatingLogUpdater>(caller.Owned2.Value, "_updatingLogUpdater", "_context"); | |
uluDbContext1.GetHashCode().Should().Be(cuDbContext1.GetHashCode()); | |
uluDbContext2.GetHashCode().Should().Be(cuDbContext2.GetHashCode()); | |
cuDbContext1.GetHashCode().Should().NotBe(cuDbContext2.GetHashCode()); | |
} | |
private static EtlDbContext GetEtlDbContext<T>(Updater updater, string firstPropertyName, string secondPropertyName) | |
{ | |
var fieldInfo = typeof(Updater).GetField(firstPropertyName, BindingFlags.NonPublic | BindingFlags.Instance); | |
var tValue = (T)fieldInfo.GetValue(updater); | |
var contextFieldInfo = typeof(T).GetField(secondPropertyName, BindingFlags.NonPublic | BindingFlags.Instance); | |
var dbContext = contextFieldInfo.GetValue(tValue) as EtlDbContext; | |
return dbContext; | |
} | |
} | |
public class Caller : IDisposable | |
{ | |
public Owned<Updater> Owned1 { get; } | |
public Owned<Updater> Owned2 { get; } | |
public Caller(Func<Owned<Updater>> updaterFactory) | |
{ | |
Owned1 = updaterFactory(); | |
Owned2 = updaterFactory(); | |
} | |
public void Dispose() | |
{ | |
Owned1?.Dispose(); | |
Owned2?.Dispose(); | |
} | |
} | |
public class TestModule : IoCModule | |
{ | |
protected override void Load(ContainerBuilder builder) | |
{ | |
base.Load(builder); | |
builder.RegisterType<Caller>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment