Created
December 23, 2012 14:17
-
-
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).
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
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