mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
- 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:
34
src/Orchard.Tests/Commands/CommandHandlerTests.cs
Normal file
34
src/Orchard.Tests/Commands/CommandHandlerTests.cs
Normal 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";
|
||||
}
|
||||
}
|
||||
}
|
@@ -107,6 +107,7 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Commands\CommandHandlerTests.cs" />
|
||||
<Compile Include="ContentManagement\ContentQueryTests.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
|
8
src/Orchard/Commands/CommandContext.cs
Normal file
8
src/Orchard/Commands/CommandContext.cs
Normal 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; }
|
||||
}
|
||||
}
|
27
src/Orchard/Commands/DefaultOrchardCommandHandler.cs
Normal file
27
src/Orchard/Commands/DefaultOrchardCommandHandler.cs
Normal 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
|
||||
}
|
||||
}
|
5
src/Orchard/Commands/ICommandHandler.cs
Normal file
5
src/Orchard/Commands/ICommandHandler.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
namespace Orchard.Commands {
|
||||
public interface ICommandHandler : IDependency {
|
||||
void Execute(CommandContext context);
|
||||
}
|
||||
}
|
@@ -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" />
|
||||
|
Reference in New Issue
Block a user