Created
March 6, 2012 14:27
-
-
Save possan/1986557 to your computer and use it in GitHub Desktop.
Minimal command query separation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MakeMyDay | |
{ | |
public interface ICommand { } | |
public interface ICommandHandler<T> where T : ICommand | |
{ | |
void Handle(T cmd); | |
} | |
public interface ICommandValidator<T> where T : ICommand | |
{ | |
CommandValidationResult Validate(T basicsCommand); | |
} | |
public interface IQueryCommand : ICommand { } | |
public interface IQueryResponse { } | |
public interface ICommandDispatcher | |
{ | |
void Dispatch<T>(T command) | |
where T : ICommand; | |
void DispatchAsync<T>(T command) | |
where T : ICommand; | |
CommandValidationResult Validate<T>(T command) | |
where T : ICommand; | |
CommandValidationResult ValidateAndDispatch<T>(T command) | |
where T : ICommand; | |
CommandValidationResult ValidateAndDispatchAsync<T>(T command) | |
where T : ICommand; | |
TR Query<TQ, TR>(TQ query) | |
where TQ : IQueryCommand | |
where TR : IQueryResponse; | |
void QueryAsync<TQ, TR>(TQ query, Action<TR> callback) | |
where TQ : IQueryCommand | |
where TR : IQueryResponse; | |
} | |
public class CommandValidationResult | |
{ | |
public bool Successful { get; private set; } | |
public string ErrorCode { get; private set; } | |
public Exception Exception { get; private set; } | |
private CommandValidationResult(bool successful, string errorcode, Exception exception) | |
{ | |
Successful = successful; | |
ErrorCode = errorcode; | |
Exception = exception; | |
} | |
public static CommandValidationResult Success() { return new CommandValidationResult(true, "", null); } | |
public static CommandValidationResult Fail(string errorcode) { return new CommandValidationResult(false, errorcode, null); } | |
public static CommandValidationResult Fail(string errorcode, Exception e) { return new CommandValidationResult(true, errorcode, e); } | |
public static CommandValidationResult Fail(Exception e) { return new CommandValidationResult(true, "exception", e); } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment