Permalink
Browse files

Some code clean-up

  • Loading branch information...
1 parent 967019a commit 2992fa74b59eb38b3663ba1917507fe4b051b828 Mark Nijhof committed Dec 2, 2009
@@ -8,13 +8,13 @@ public class EventProcessor
{
public Type RegisteredEvent { get; private set; }
public PropertyInfo Property { get; private set; }
- public Action<object, Dictionary<string, object>> EventPropertyProcessor { get; private set; }
+ public Action<object, Dictionary<string, object>> ProcessorEventProperty { get; private set; }
public EventProcessor(Type registeredEvent, PropertyInfo property, Action<object, Dictionary<string, object>> eventPropertyProcessor)
{
RegisteredEvent = registeredEvent;
Property = property;
- EventPropertyProcessor = eventPropertyProcessor;
+ ProcessorEventProperty = eventPropertyProcessor;
}
}
}
@@ -34,8 +34,7 @@ public void Process()
private void ProcessEvent(Type eventType)
{
- var eventProcessors = _eventAccessor.BuildEventProcessors(eventType);
- _eventProcessorCache.RegisterEventProcessors(eventType, eventProcessors);
+ _eventProcessorCache.RegisterEventProcessors(eventType, _eventAccessor.BuildEventProcessors(eventType));
}
}
}
@@ -56,8 +56,7 @@ void IEventProvider.LoadFromHistory(IEnumerable<IDomainEvent> domainEvents)
.ToList()
.ForEach(domainEvent => apply(domainEvent.GetType(), domainEvent));
- Version = domainEvents.Last().EventVersion;
- EventVersion = Version;
+ EventVersion = Version = domainEvents.Last().EventVersion;
}
void IEventProvider.UpdateVersion(int version)
@@ -137,7 +136,7 @@ private void apply(Type eventType, IDomainEvent domainEvent)
eventProcessors
.ToList()
- .ForEach(eventProcessor => eventProcessor.EventPropertyProcessor(domainEvent, _internalState));
+ .ForEach(eventProcessor => eventProcessor.ProcessorEventProperty(domainEvent, _internalState));
}
}
}
@@ -27,37 +27,39 @@ public EventAccessor(EventPropertyLocator eventPropertyLocator)
public IEnumerable<EventProcessor> BuildEventProcessors(Type registeredEvent)
{
- var eventProperties = _eventPropertyLocator.RetrieveDomainEventProperties(registeredEvent);
- return eventProperties.Select(eventProperty => new EventProcessor(registeredEvent, eventProperty, ProcessEventProperty(registeredEvent, eventProperty)));
+ return _eventPropertyLocator
+ .RetrieveDomainEventProperties(registeredEvent)
+ .Select(eventProperty => new EventProcessor(registeredEvent, eventProperty, ProcessEventProperty(registeredEvent, eventProperty)));
}
private Action<object, Dictionary<string, object>> ProcessEventProperty(Type registeredEvent, PropertyInfo eventProperty)
{
- var genericBuildLambdaMethod = _createEventPropertyAccessorLabmdaMethod.MakeGenericMethod(registeredEvent);
+ var eventPropertyAccessor = _createEventPropertyAccessorLabmdaMethod
+ .MakeGenericMethod(registeredEvent)
+ .Invoke(this, new object[] { eventProperty });
- var invoke = genericBuildLambdaMethod.Invoke(this, new object[] { eventProperty });
-
- var makeGenericMethod = _createToInternalStateCopyLambdaMethod.MakeGenericMethod(registeredEvent, eventProperty.PropertyType);
- var internalStateCopyLambda = makeGenericMethod.Invoke(this, new[] { eventProperty, invoke }) as Action<object, Dictionary<string, object>>;
-
- return internalStateCopyLambda;
+ return (Action<object, Dictionary<string, object>>)_createToInternalStateCopyLambdaMethod
+ .MakeGenericMethod(registeredEvent, eventProperty.PropertyType)
+ .Invoke(this, new[] { eventProperty, eventPropertyAccessor });
}
- protected Action<object, Dictionary<string, object>> CreateToInternalStateCopyLambda<TEventType, TPropertyType>(MemberInfo property, object func) where TEventType : class, IDomainEvent
+ protected Action<object, Dictionary<string, object>> CreateToInternalStateCopyLambda<TEventType, TPropertyType>(MemberInfo property, Func<TEventType, TPropertyType> func) where TEventType : class, IDomainEvent
{
return (eventType, internalState) =>
{
if (!internalState.ContainsKey(property.Name))
- internalState.Add(property.Name, new object());
+ internalState.Add(property.Name, null);
- internalState[property.Name] = ((Expression<Func<TEventType, TPropertyType>>)func).Compile().Invoke(eventType as TEventType);
+ internalState[property.Name] = func(eventType as TEventType);
};
}
protected object CreateEventPropertyAccessorLabmda<TEventType>(MemberInfo property)
{
var expression = Expression.Parameter(typeof(TEventType), "x");
- return Expression.Lambda(Expression.MakeMemberAccess(expression, property), new[] { expression });
+ return Expression
+ .Lambda(Expression.MakeMemberAccess(expression, property), new[] { expression })
+ .Compile();
}
}

0 comments on commit 2992fa7

Please sign in to comment.