Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneric method call, don't want to send in my types
    text
    copied!<p>I'm experimenting with my Commands and some generic methods. I havn't used generics that much so now my brain is all messed up and hoping for some help.</p> <p>What I want is the last test to pass. And return my TResult without me needing to supply the TCommand, TResult in the call.</p> <pre><code>invoker.Execute(command) </code></pre> <p>Since that command implements </p> <pre><code>: CommandBase&lt;TestResult&gt; </code></pre> <p>I though that the compiler would figure it out.</p> <p>But the compiler only takes me the to the void method.</p> <p>Huge thanks in advance!</p> <p>Edit: The complete code is available at: <a href="http://codepaste.net/7rjg2e" rel="nofollow">http://codepaste.net/7rjg2e</a></p> <p><strong>CommandInvoker</strong></p> <pre><code>public interface ICommandInvoker { void Execute&lt;TCommand&gt;(TCommand command) where TCommand : ICommand; TResult Execute&lt;TCommand, TResult&gt;(TCommand command) where TCommand : ICommand&lt;TResult&gt;; } public class CommandInvoker : ICommandInvoker { ... public void Execute&lt;TCommand&gt;(TCommand command) where TCommand : ICommand { var handler = _container.GetInstance&lt;ICommandHandler&lt;TCommand&gt;&gt;(); handler.Handle(command); _session.SaveChanges(); } public TResult Execute&lt;TCommand, TResult&gt;(TCommand command) where TCommand : ICommand&lt;TResult&gt; { var handler = _container.GetInstance&lt;ICommandHandler&lt;TCommand&gt;&gt;(); handler.Handle(command); return command.Result; } } </code></pre> <p><strong>Commands</strong></p> <pre><code>public interface ICommand { bool IsValid { get; } } public interface ICommand&lt;TResult&gt; : ICommand { TResult Result { get; } } public class CommandBase : ICommand { public bool IsValid { get { return false; } } } public class CommandBase&lt;TResult&gt; : ICommand&lt;TResult&gt; { public bool IsValid { get { return false; } } public TResult Result { get; set; } } </code></pre> <p><strong>CommandHandler</strong></p> <pre><code>public interface ICommandHandler&lt;TCommand&gt; { void Handle(TCommand command); } public interface ICommandHandlerWithResult&lt;TCommand, TResult&gt; where TCommand : ICommand&lt;TResult&gt; { void Handle(TCommand command); } </code></pre> <p><strong>Test classes</strong></p> <pre><code>public class TestCommandWithResult : CommandBase&lt;TestResult&gt; { public string Id { get; set; } } public class TestResult { public string Message { get; set; } } </code></pre> <p><strong>Test thats works</strong></p> <pre><code>[Test] public void CanExcecuteWithResult() { var command = new TestCommandWithResult { Id = "billy" }; ObjectFactory.ResetDefaults(); var mockHandler = new Mock&lt;ICommandHandler&lt;TestCommandWithResult&gt;&gt;(); var sessionMock = new Mock&lt;ISession&gt;(); ObjectFactory.Configure(x =&gt; x.For&lt;ICommandHandler&lt;TestCommandWithResult&gt;&gt;().Use(mockHandler.Object)); var invoker = new CommandInvoker(ObjectFactory.Container, sessionMock.Object); var result = invoker.Execute&lt;TestCommandWithResult, TestResult&gt;(command); mockHandler.Verify(x =&gt; x.Handle(command)); } </code></pre> <p><strong>Test that I want to pass</strong></p> <pre><code> [Test] public void CanExcecuteWithResult() { var command = new TestCommandWithResult { Id = "billy" }; ObjectFactory.ResetDefaults(); var mockHandler = new Mock&lt;ICommandHandler&lt;TestCommandWithResult&gt;&gt;(); var sessionMock = new Mock&lt;ISession&gt;(); ObjectFactory.Configure(x =&gt; x.For&lt;ICommandHandler&lt;TestCommandWithResult&gt;&gt;().Use(mockHandler.Object)); var invoker = new CommandInvoker(ObjectFactory.Container, sessionMock.Object); var result = invoker.Execute(command); // &lt;-- this only calls void version mockHandler.Verify(x =&gt; x.Handle(command)); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload