Skip to content

Instantly share code, notes, and snippets.

@tarwn
Created February 22, 2013 13:28
Show Gist options
  • Save tarwn/5013368 to your computer and use it in GitHub Desktop.
Save tarwn/5013368 to your computer and use it in GitHub Desktop.
This file has the original Bad C# CommandExecutor, a good one, and a good one using the same names as you used for the VB version. Original tests are at bottom, with extra set for the VBish CommandExecutor
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ChrissieGist5012518
{
public class BadCommandExecuter : ICommandExecuter
{
public void Execute<TCommand>() where TCommand : class, ICommand, new()
{
var command = new TCommand();
command.Execute();
}
//same terms as the C# version
public void Execute<TCommandWithParameter, TParameterType>(TParameterType parameter)
where TCommandWithParameter : class, ICommandWithParameter<object>, new() // <-- that object should be TParameterType
{
var command = new TCommandWithParameter();
command.ExecuteWitParameter(parameter);
}
}
public class CommandExecuter : ICommandExecuter
{
public void Execute<TCommand>() where TCommand : class, ICommand, new()
{
var command = new TCommand();
command.Execute();
}
//same terms as the C# version
public void Execute<TCommandWithParameter, TParameterType>(TParameterType parameter)
where TCommandWithParameter : class, ICommandWithParameter<TParameterType>, new()
{
var command = new TCommandWithParameter();
command.ExecuteWitParameter(parameter);
}
}
// used parameter names for the generics that matched your VB code
public class CommandExecuterVBish : ICommandExecuter
{
public void Execute<TCommand>() where TCommand : class, ICommand, new()
{
var command = new TCommand();
command.Execute();
}
// same terms as the VB version
public void Execute<TCommand, TParameter>(TParameter parameter)
where TCommand : ICommandWithParameter<TParameter>, new()
{
var command = new TCommand();
command.ExecuteWitParameter(parameter);
}
}
#region Interfaces
public interface ICommandExecuter
{
}
public interface ICommand
{
void Execute();
}
public interface ICommandWithParameter
{
void Execute();
}
public interface ICommandWithParameter<T>
{
void ExecuteWitParameter(T Parameter);
}
#endregion
[TestFixture]
public class BadTests
{
[Test]
public void IfExecuteExecutesCommandWithParameter()
{
var executer = new BadCommandExecuter();
Assert.Throws<System.NotImplementedException>(() => executer.Execute<TestCommandWithParameter, String>("test"));
}
private class TestCommandWithParameter : ICommandWithParameter<object>
{
public void ExecuteWitParameter(object parameter)
{
throw new System.NotImplementedException();
}
}
}
[TestFixture]
public class WhatIWantTests
{
[Test]
public void IfExecuteExecutesCommandWithParameter_VBStyleParamNames()
{
var executer = new CommandExecuterVBish();
Assert.Throws<System.NotImplementedException>(() => executer.Execute<TestCommandWithParameter, string>("test"));
}
[Test]
public void IfExecuteExecutesCommandWithParameter()
{
var executer = new CommandExecuter();
Assert.Throws<System.NotImplementedException>(() => executer.Execute<TestCommandWithParameter, string>("test"));
}
private class TestCommandWithParameter : ICommandWithParameter<string>
{
public void ExecuteWitParameter(string parameter)
{
throw new System.NotImplementedException();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment