Skip to content

Instantly share code, notes, and snippets.

@thefringeninja
Created September 26, 2012 18:14
Show Gist options
  • Save thefringeninja/3789614 to your computer and use it in GitHub Desktop.
Save thefringeninja/3789614 to your computer and use it in GitHub Desktop.
Method Dependencies w/ Cross Cutting Concerns
// j oliver's event store will dispatch commits when you build it up, BEFORE you had a chance to register anything.
// So it needs to be lazily evaluated. I don't want to see Lazy<T> in my repo implementation or the application layer.
// Don't care if I see it in registration.
// this really confusing code allows us to do something like
// [Logged]
// public void Handle(TestMessage message, IRepository<TestAggregate> repository) {}
//
// var repository = new Lazy<IRepository<TestAggregate>>(() => new EventStoreRepository(Wireup...Build()));
// bus.Register<TestMessage, IRepository<TestAggregate>>(applicationLayer.Handle, () => repository.Value));
public static class BusRegistrationExtensions
{
public static void Register<T, TParam1>(this IBus bus, Action<T, TParam1> action, Func<TParam1> d1)
{
Action<T> application = m => action(m, d1());
bus.Register(application.ApplyAspects(action.Method.FindAspects()));
}
public static void Register<T, TParam1, TParam2>(this IBus bus, Action<T, TParam1, TParam2> action, Func<TParam1> d1, Func<TParam2> d2)
{
Action<T> application = m => action(m, d1(), d2());
bus.Register(application.ApplyAspects(action.Method.FindAspects()));
}
public static void Register<T, TParam1, TParam2, TParam3>(this IBus bus, Action<T, TParam1, TParam2, TParam3> action, Func<TParam1> d1, Func<TParam2> d2, Func<TParam3> d3)
{
Action<T> application = m => action(m, d1(), d2(), d3());
bus.Register(application.ApplyAspects(action.Method.FindAspects()));
}
public static void Register<T, TParam1, TParam2, TParam3, TParam4>(this IBus bus, Action<T, TParam1, TParam2, TParam3, TParam4> action, Func<TParam1> d1, Func<TParam2> d2, Func<TParam3> d3, Func<TParam4> d4)
{
Action<T> application = m => action(m, d1(), d2(), d3(), d4());
bus.Register(application.ApplyAspects(action.Method.FindAspects()));
}
public static void Register<T, TParam1, TParam2, TParam3, TParam4, TParam5>(this IBus bus, Action<T, TParam1, TParam2, TParam3, TParam4, TParam5> action, Func<TParam1> d1, Func<TParam2> d2, Func<TParam3> d3, Func<TParam4> d4, Func<TParam5> d5)
{
Action<T> application = m => action(m, d1(), d2(), d3(), d4(), d5());
bus.Register(application.ApplyAspects(action.Method.FindAspects()));
}
public static Action<T> ApplyAspects<T>(this Action<T> action)
{
return action.ApplyAspects(action.Method.FindAspects());
}
public static Action<T> ApplyAspects<T>(this Action<T> action, IEnumerable<ICutAcross> concerns)
{
return concerns
.Aggregate(action, (current, concern) => concern.Apply(current));
}
public static IEnumerable<ICutAcross> FindAspects(this MethodInfo methodInfo)
{
var concerns = methodInfo.GetCustomAttributes(false).OfType<ICutAcross>();
if (methodInfo.DeclaringType != null)
{
concerns = concerns.Union(methodInfo.DeclaringType.GetCustomAttributes(false).OfType<ICutAcross>());
}
return concerns;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment