- Generic method call, don't want to send in my types
- invoker.Execute(command)
- : CommandBase<TestResult>
- public interface ICommandInvoker
- {
- void Execute<TCommand>(TCommand command) where TCommand : ICommand;
- TResult Execute<TCommand, TResult>(TCommand command) where TCommand : ICommand<TResult>;
- }
- public class CommandInvoker : ICommandInvoker
- {
- ...
- public void Execute<TCommand>(TCommand command) where TCommand : ICommand
- {
- var handler = _container.GetInstance<ICommandHandler<TCommand>>();
- handler.Handle(command);
- _session.SaveChanges();
- }
- public TResult Execute<TCommand, TResult>(TCommand command) where TCommand : ICommand<TResult>
- {
- var handler = _container.GetInstance<ICommandHandler<TCommand>>();
- handler.Handle(command);
- return command.Result;
- }
- }
- public interface ICommand
- {
- bool IsValid { get; }
- }
- public interface ICommand<TResult> : ICommand
- {
- TResult Result { get; }
- }
- public class CommandBase : ICommand
- {
- public bool IsValid
- {
- get { return false; }
- }
- }
- public class CommandBase<TResult> : ICommand<TResult>
- {
- public bool IsValid { get { return false; } }
- public TResult Result { get; set; }
- }
- public interface ICommandHandler<TCommand>
- {
- void Handle(TCommand command);
- }
- public interface ICommandHandlerWithResult<TCommand, TResult> where TCommand : ICommand<TResult>
- {
- void Handle(TCommand command);
- }
- public class TestCommandWithResult : CommandBase<TestResult>
- {
- public string Id { get; set; }
- }
- public class TestResult
- {
- public string Message { get; set; }
- }
- [Test]
- public void CanExcecuteWithResult()
- {
- var command = new TestCommandWithResult { Id = "billy" };
- ObjectFactory.ResetDefaults();
- var mockHandler = new Mock<ICommandHandler<TestCommandWithResult>>();
- var sessionMock = new Mock<ISession>();
- ObjectFactory.Configure(x => x.For<ICommandHandler<TestCommandWithResult>>().Use(mockHandler.Object));
- var invoker = new CommandInvoker(ObjectFactory.Container, sessionMock.Object);
- var result = invoker.Execute<TestCommandWithResult, TestResult>(command);
- mockHandler.Verify(x => x.Handle(command));
- }
- [Test]
- public void CanExcecuteWithResult()
- {
- var command = new TestCommandWithResult { Id = "billy" };
- ObjectFactory.ResetDefaults();
- var mockHandler = new Mock<ICommandHandler<TestCommandWithResult>>();
- var sessionMock = new Mock<ISession>();
- ObjectFactory.Configure(x => x.For<ICommandHandler<TestCommandWithResult>>().Use(mockHandler.Object));
- var invoker = new CommandInvoker(ObjectFactory.Container, sessionMock.Object);
- var result = invoker.Execute(command); // <-- this only calls void version
- mockHandler.Verify(x => x.Handle(command));
- }
- public TResult Execute<TResult>(ICommand<TResult> command)
- {
- Type commandHandlerType = typeof(ICommandHandler<>).MakeGenericType(command.GetType());
- var handler = _container.GetInstance(commandHandlerType);
- handler.Handle(command);
- return command.Result;
- }
- Execute<TComamnd, TResult>(TCommand command)
- TResult Execute<TCommand, TResult>(TCommand command)