Skip to content

Instantly share code, notes, and snippets.

@JefClaes
Created December 23, 2012 14:17
Show Gist options
  • Save JefClaes/4363612 to your computer and use it in GitHub Desktop.
Save JefClaes/4363612 to your computer and use it in GitHub Desktop.
Attempt to not have to separate command and command data to get some DI going. Most commands don't have dependencies which need to be injected (RavenDB in-memory store).
public class CommandHandler : ICommandHandler
{
private IKernel _kernel;
public CommandHandler() { }
public CommandHandler(IKernel kernel)
{
_kernel = kernel;
}
public void Execute(Command command)
{
var method = command.GetType().GetMethod("Inject");
if (method != null)
{
var parameters = method.GetParameters();
var paramterInstances = new List<object>();
foreach (var parameter in parameters)
{
var type = parameter.ParameterType;
var instance = _kernel.Get(type);
paramterInstances.Add(instance);
}
method.Invoke(command, paramterInstances.ToArray());
}
var store = DocumentStore.Get();
using (var session = store.OpenSession())
{
command.Session = session;
command.Execute();
session.SaveChanges();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment