Skip to content

Instantly share code, notes, and snippets.

@CarstenKoenig
Created June 5, 2013 14:09
Show Gist options
  • Save CarstenKoenig/5714144 to your computer and use it in GitHub Desktop.
Save CarstenKoenig/5714144 to your computer and use it in GitHub Desktop.
Fragen bezüglich Aggregate.cs
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