- Orchard Command Handler infrastructure

- CommandHandler interface and reflection based default implementation.
- Basic command execution support.
- Unit tests.

--HG--
branch : dev
This commit is contained in:
Suha Can
2010-04-05 16:59:17 -07:00
parent a2e58f9470
commit f589720049
6 changed files with 78 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
using NUnit.Framework;
using Orchard.Commands;
using System;
namespace Orchard.Tests.Commands {
[TestFixture]
public class CommandsTests {
private ICommandHandler _handler;
[SetUp]
public void Init() {
_handler = new StubCommandHandler();
}
[Test]
public void TestFooCommand() {
CommandContext commandContext = new CommandContext { Command = "Foo" };
_handler.Execute(commandContext);
Assert.That(commandContext.Output, Is.EqualTo("Command Foo Executed"));
}
[Test]
public void TestNotExistingCommand() {
CommandContext commandContext = new CommandContext { Command = "NoSuchCommand" };
Assert.Throws<InvalidOperationException>(() => _handler.Execute(commandContext));
}
}
public class StubCommandHandler : DefaultOrchardCommandHandler {
public string Foo() {
return "Command Foo Executed";
}
}
}

View File

@@ -107,6 +107,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Commands\CommandHandlerTests.cs" />
<Compile Include="ContentManagement\ContentQueryTests.cs">
<SubType>Code</SubType>
</Compile>

View File

@@ -0,0 +1,8 @@
namespace Orchard.Commands {
public class CommandContext {
public string Input { get; set; }
public string Output { get; set; }
public string Command { get; set; }
public string[] Arguments { get; set; }
}
}

View File

@@ -0,0 +1,27 @@
using System;
using System.Reflection;
using Orchard.Localization;
namespace Orchard.Commands {
public abstract class DefaultOrchardCommandHandler : ICommandHandler {
protected DefaultOrchardCommandHandler() {
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
#region Implementation of ICommandHandler
public void Execute(CommandContext context) {
MethodInfo methodInfo = GetType().GetMethod(context.Command);
if (methodInfo != null) {
context.Output = (string) methodInfo.Invoke(this, null);
}
else {
throw new InvalidOperationException(T("Command : ") + context.Command + T(" was not found"));
}
}
#endregion
}
}

View File

@@ -0,0 +1,5 @@
namespace Orchard.Commands {
public interface ICommandHandler : IDependency {
void Execute(CommandContext context);
}
}

View File

@@ -129,6 +129,9 @@
<Compile Include="Validation\Argument.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="Commands\CommandContext.cs" />
<Compile Include="Commands\DefaultOrchardCommandHandler.cs" />
<Compile Include="Commands\ICommandHandler.cs" />
<Compile Include="ContentManagement\Aspects\ICommonAspect.cs" />
<Compile Include="ContentManagement\Drivers\IContentItemDriver.cs" />
<Compile Include="ContentManagement\Extenstions\UrlHelperExtensions.cs" />