Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 3.18 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Generic method call, don't want to send in my types
  2. invoker.Execute(command)
  3.        
  4. : CommandBase<TestResult>
  5.        
  6. public interface ICommandInvoker
  7. {
  8.     void Execute<TCommand>(TCommand command) where TCommand : ICommand;
  9.     TResult Execute<TCommand, TResult>(TCommand command) where TCommand : ICommand<TResult>;
  10. }
  11.  
  12. public class CommandInvoker : ICommandInvoker
  13. {
  14.     ...
  15.  
  16.     public void Execute<TCommand>(TCommand command) where TCommand : ICommand
  17.     {
  18.         var handler = _container.GetInstance<ICommandHandler<TCommand>>();
  19.         handler.Handle(command);
  20.  
  21.         _session.SaveChanges();
  22.     }
  23.  
  24.     public TResult Execute<TCommand, TResult>(TCommand command) where TCommand : ICommand<TResult>
  25.     {
  26.         var handler = _container.GetInstance<ICommandHandler<TCommand>>();
  27.         handler.Handle(command);
  28.  
  29.         return command.Result;
  30.     }
  31. }
  32.        
  33. public interface ICommand
  34. {
  35.     bool IsValid { get; }
  36. }
  37.  
  38. public interface ICommand<TResult> : ICommand
  39. {
  40.     TResult Result { get; }
  41. }
  42.  
  43. public class CommandBase : ICommand
  44. {
  45.     public bool IsValid
  46.     {
  47.         get { return false; }
  48.     }
  49. }
  50.  
  51. public class CommandBase<TResult> : ICommand<TResult>
  52. {
  53.     public bool IsValid { get { return false; } }
  54.     public TResult Result { get; set; }
  55. }
  56.        
  57. public interface ICommandHandler<TCommand>
  58. {
  59.     void Handle(TCommand command);
  60. }
  61.  
  62. public interface ICommandHandlerWithResult<TCommand, TResult> where TCommand : ICommand<TResult>
  63. {
  64.     void Handle(TCommand command);
  65. }
  66.        
  67. public class TestCommandWithResult : CommandBase<TestResult>
  68. {
  69.     public string Id { get; set; }
  70. }
  71.  
  72. public class TestResult
  73. {
  74.     public string Message { get; set; }
  75. }
  76.        
  77. [Test]
  78. public void CanExcecuteWithResult()
  79. {
  80.     var command = new TestCommandWithResult { Id = "billy" };
  81.  
  82.     ObjectFactory.ResetDefaults();
  83.     var mockHandler = new Mock<ICommandHandler<TestCommandWithResult>>();
  84.     var sessionMock = new Mock<ISession>();
  85.     ObjectFactory.Configure(x => x.For<ICommandHandler<TestCommandWithResult>>().Use(mockHandler.Object));
  86.  
  87.     var invoker = new CommandInvoker(ObjectFactory.Container, sessionMock.Object);
  88.     var result = invoker.Execute<TestCommandWithResult, TestResult>(command);
  89.  
  90.     mockHandler.Verify(x => x.Handle(command));
  91. }
  92.        
  93. [Test]
  94.     public void CanExcecuteWithResult()
  95.     {
  96.         var command = new TestCommandWithResult { Id = "billy" };
  97.  
  98.         ObjectFactory.ResetDefaults();
  99.         var mockHandler = new Mock<ICommandHandler<TestCommandWithResult>>();
  100.         var sessionMock = new Mock<ISession>();
  101.         ObjectFactory.Configure(x => x.For<ICommandHandler<TestCommandWithResult>>().Use(mockHandler.Object));
  102.  
  103.         var invoker = new CommandInvoker(ObjectFactory.Container, sessionMock.Object);
  104.         var result = invoker.Execute(command); // <-- this only calls void version
  105.  
  106.         mockHandler.Verify(x => x.Handle(command));
  107.     }
  108.        
  109. public TResult Execute<TResult>(ICommand<TResult> command)
  110. {
  111.     Type commandHandlerType = typeof(ICommandHandler<>).MakeGenericType(command.GetType());
  112.  
  113.     var handler = _container.GetInstance(commandHandlerType);
  114.     handler.Handle(command);
  115.  
  116.     return command.Result;
  117. }
  118.        
  119. Execute<TComamnd, TResult>(TCommand command)
  120.        
  121. TResult Execute<TCommand, TResult>(TCommand command)