Skip to content

Instantly share code, notes, and snippets.

@possan
Created March 6, 2012 14:27
Show Gist options
  • Save possan/1986557 to your computer and use it in GitHub Desktop.
Save possan/1986557 to your computer and use it in GitHub Desktop.
Minimal command query separation
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