Skip to content

Instantly share code, notes, and snippets.

@MarkNijhof
Created August 3, 2010 22:11
Show Gist options
  • Save MarkNijhof/507250 to your computer and use it in GitHub Desktop.
Save MarkNijhof/507250 to your computer and use it in GitHub Desktop.
namespace Fohjin.DDD.Events.Account
{
[Serializable]
public class CashWithdrawnEvent : DomainEvent
{
public decimal Balance { get; private set; }
public decimal Amount { get; private set; }
public CashWithdrawnEvent(decimal balance, decimal amount)
{
Balance = balance;
Amount = amount;
}
}
[Serializable]
public class CashWithdrawnEvent_v2 : DomainEvent
{
public decimal Balance { get; private set; }
public decimal Amount { get; private set; }
public Guid AtmId { get; private set; }
public CashWithdrawnEvent_v2(decimal balance, decimal amount, Guid atmId)
{
Balance = balance;
Amount = amount;
AtmId = atmId;
}
}
}
namespace Test.Fohjin.DDD.Spike
{
public class Spike_test_1 : BaseTestFixture
{
private object ConvertedEvent;
protected override void When()
{
ConvertedEvent = new EventConvertor().Convert(new CashWithdrawnEvent(10.0M, 20.0M));
}
[Then]
public void The_converted_event_is_the_latest_version()
{
ConvertedEvent.WillBeOfType<CashWithdrawnEvent_v4>();
}
[Then]
public void The_converted_event_wil_contain_the_correct_data()
{
ConvertedEvent.As<CashWithdrawnEvent_v4>().Balance.WillBe(10.0M);
ConvertedEvent.As<CashWithdrawnEvent_v4>().Amount.WillBe(20.0M);
ConvertedEvent.As<CashWithdrawnEvent_v4>().AtmId.WillBe(string.Empty);
}
}
public class Spike_test_2 : BaseTestFixture
{
private object ConvertedEvent;
protected override void When()
{
ConvertedEvent = new EventConvertor().Convert(new CashWithdrawnEvent_v2(10.0M, 20.0M, "12345"));
}
[Then]
public void The_converted_event_is_the_latest_version()
{
ConvertedEvent.WillBeOfType<CashWithdrawnEvent_v4>();
}
[Then]
public void The_converted_event_wil_contain_the_correct_data()
{
ConvertedEvent.As<CashWithdrawnEvent_v4>().Balance.WillBe(10.0M);
ConvertedEvent.As<CashWithdrawnEvent_v4>().Amount.WillBe(20.0M);
ConvertedEvent.As<CashWithdrawnEvent_v4>().AtmId.WillBe("12345");
}
}
public class Spike_test_3 : BaseTestFixture
{
private object ConvertedEvent;
protected override void When()
{
ConvertedEvent = new EventConvertor().Convert(new CashWithdrawnEvent_v3(10.0M, 20.0M, "12345"));
}
[Then]
public void The_converted_event_is_the_latest_version()
{
ConvertedEvent.WillBeOfType<CashWithdrawnEvent_v4>();
}
[Then]
public void The_converted_event_wil_contain_the_correct_data()
{
ConvertedEvent.As<CashWithdrawnEvent_v4>().Balance.WillBe(10.0M);
ConvertedEvent.As<CashWithdrawnEvent_v4>().Amount.WillBe(20.0M);
ConvertedEvent.As<CashWithdrawnEvent_v4>().AtmId.WillBe("12345");
}
}
public class Spike_test_4 : BaseTestFixture
{
private object ConvertedEvent;
protected override void When()
{
ConvertedEvent = new EventConvertor().Convert(new CashWithdrawnEvent_v4(10.0M, 20.0M, "12345"));
}
[Then]
public void The_converted_event_is_the_latest_version()
{
ConvertedEvent.WillBeOfType<CashWithdrawnEvent_v4>();
}
[Then]
public void The_converted_event_wil_contain_the_correct_data()
{
ConvertedEvent.As<CashWithdrawnEvent_v4>().Balance.WillBe(10.0M);
ConvertedEvent.As<CashWithdrawnEvent_v4>().Amount.WillBe(20.0M);
ConvertedEvent.As<CashWithdrawnEvent_v4>().AtmId.WillBe("12345");
}
}
}
namespace Test.Fohjin.DDD.Spike
{
public class EventConvertor
{
private readonly Dictionary<Type, Func<object, object>> _convertors;
public EventConvertor()
{
_convertors = new Dictionary<Type, Func<object, object>>();
RegisterEventConvertors();
}
private void RegisterEventConvertors()
{
_convertors.Add(typeof(CashWithdrawnEvent), x => new CashWithdrawnEventConvertor().Convert((CashWithdrawnEvent)x));
_convertors.Add(typeof(CashWithdrawnEvent_v2), x => new CashWithdrawnEvent_v2Convertor().Convert((CashWithdrawnEvent_v2)x));
_convertors.Add(typeof(CashWithdrawnEvent_v3), x => new CashWithdrawnEvent_v3Convertor().Convert((CashWithdrawnEvent_v3)x));
}
public object Convert(object soureEvent)
{
Func<object, object> convertor;
return _convertors.TryGetValue(soureEvent.GetType(), out convertor)
? Convert(convertor(soureEvent))
: soureEvent;
}
}
public interface IEventConvertor<TSourceEvent, TTargetEvent>
where TSourceEvent : IDomainEvent
where TTargetEvent : IDomainEvent
{
TTargetEvent Convert(TSourceEvent sourceEvent);
}
public class CashWithdrawnEventConvertor : IEventConvertor<CashWithdrawnEvent, CashWithdrawnEvent_v4>
{
public CashWithdrawnEvent_v4 Convert(CashWithdrawnEvent sourceEvent)
{
var theEvent = new CashWithdrawnEvent_v4(sourceEvent.Balance, sourceEvent.Amount, string.Empty)
{
AggregateId = sourceEvent.AggregateId
};
(theEvent as IDomainEvent).Version = (sourceEvent as IDomainEvent).Version;
return theEvent;
}
}
public class CashWithdrawnEvent_v2Convertor : IEventConvertor<CashWithdrawnEvent_v2, CashWithdrawnEvent_v3>
{
public CashWithdrawnEvent_v3 Convert(CashWithdrawnEvent_v2 sourceEvent)
{
var theEvent = new CashWithdrawnEvent_v3(sourceEvent.Balance, sourceEvent.Amount, sourceEvent.AtmId)
{
AggregateId = sourceEvent.AggregateId
};
(theEvent as IDomainEvent).Version = (sourceEvent as IDomainEvent).Version;
return theEvent;
}
}
public class CashWithdrawnEvent_v3Convertor : IEventConvertor<CashWithdrawnEvent_v3, CashWithdrawnEvent_v4>
{
public CashWithdrawnEvent_v4 Convert(CashWithdrawnEvent_v3 sourceEvent)
{
var theEvent = new CashWithdrawnEvent_v4(sourceEvent.Balance, sourceEvent.Amount, sourceEvent.AtmId)
{
AggregateId = sourceEvent.AggregateId
};
(theEvent as IDomainEvent).Version = (sourceEvent as IDomainEvent).Version;
return theEvent;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment