Created
June 5, 2013 14:09
-
-
Save CarstenKoenig/5714144 to your computer and use it in GitHub Desktop.
Fragen bezüglich Aggregate.cs
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
protected void ApplyChanges<T>(T changeEvent) where T : class | |
{ | |
var id = IdFrom((dynamic)changeEvent); | |
if (id == Guid.Empty) | |
{ | |
try { id = Id; } | |
catch (NullReferenceException) | |
{ | |
throw new ArgumentException( | |
"Unable to find a valid Id for Aggregate. Ensure the Event or Aggregate contains an Id."); | |
} | |
} | |
Changes.Add(new AggregateEventBag<T>((Guid)id){EventData = changeEvent}); | |
} | |
private static Guid IdFrom(object changeEvent) | |
{ | |
var propertyInfos = changeEvent.GetType().GetProperties(); | |
try | |
{ | |
var identifier = propertyInfos.SingleOrDefault(pi => | |
pi.Name.Equals("id") | |
| pi.Name.Equals("ID") | |
| pi.Name.Equals("Id") | |
| pi.Name.Equals("iD")); | |
return identifier != null | |
? (Guid)identifier.GetValue(changeEvent) | |
: Guid.Empty; | |
} | |
catch (Exception) | |
{ | |
return Guid.Empty; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment