一、项目结构

二、代码

     /// <summary>
///
/// </summary>
public interface ICommand
{
}
     /// <summary>
///
/// </summary>
public interface ICommandResult
{
bool Success { get; }
int? ReturnKey { get; } string ReturnKeys { get; } }
     /// <summary>
///
/// </summary>
/// <typeparam name="TCommand"></typeparam>
public interface ICommandHandler<in TCommand> where TCommand : ICommand
{
ICommandResult Execute(TCommand command);
}
     /// <summary>
///
/// </summary>
/// <typeparam name="TCommand"></typeparam>
public interface IValidationHandler<in TCommand> where TCommand : ICommand
{
IEnumerable<ValidationResult> Validate(TCommand command);
}
     /// <summary>
///
/// </summary>
public class CommandResult : ICommandResult
{
public CommandResult(bool success, int? returnkey = null, string returnKeys = null)
{
this.Success = success;
this.ReturnKey = returnkey;
this.ReturnKeys = returnKeys;
} public bool Success { get; protected set; } public int? ReturnKey { get; protected set; } public string ReturnKeys { get; protected set; } }
     /// <summary>
///
/// </summary>
public class CommandHandlerNotFoundException : Exception
{
public CommandHandlerNotFoundException(Type type)
: base(string.Format("Command handler not found for command type: {0}", type))
{
}
}
     /// <summary>
///
/// </summary>
public class ValidationHandlerNotFoundException : Exception
{
public ValidationHandlerNotFoundException(Type type)
: base(string.Format("Validation handler not found for command type: {0}", type))
{
}
}
     /// <summary>
///
/// </summary>
public interface ICommandBus
{
ICommandResult Submit<TCommand>(TCommand command) where TCommand : ICommand;
IEnumerable<ValidationResult> Validate<TCommand>(TCommand command) where TCommand : ICommand;
}
     /// <summary>
///
/// </summary>
public class DefaultCommandBus : ICommandBus
{
public ICommandResult Submit<TCommand>(TCommand command) where TCommand : ICommand
{
var handler = (ICommandHandler<TCommand>)DependencyDefaultInstanceResolver.GetInstance(typeof(ICommandHandler<TCommand>));
if (!((handler != null) && handler is ICommandHandler<TCommand>))
{
throw new CommandHandlerNotFoundException(typeof(TCommand));
}
return handler.Execute(command);
} public IEnumerable<ValidationResult> Validate<TCommand>(TCommand command) where TCommand : ICommand
{
var handler = (IValidationHandler<TCommand>)DependencyDefaultInstanceResolver.GetInstance(typeof(IValidationHandler<TCommand>));
if (!((handler != null) && handler is IValidationHandler<TCommand>))
{
throw new ValidationHandlerNotFoundException(typeof(TCommand));
}
return handler.Validate(command);
} }
     /// <summary>
/// Describes the result of a validation of a potential change through a business service.
/// </summary>
public class ValidationResult
{ /// <summary>
/// Initializes a new instance of the <see cref="ValidationResult"/> class.
/// </summary>
public ValidationResult()
{
} /// <summary>
/// Initializes a new instance of the <see cref="ValidationResult"/> class.
/// </summary>
/// <param name="memeberName">Name of the memeber.</param>
/// <param name="message">The message.</param>
public ValidationResult(string memeberName, string message)
{
this.MemberName = memeberName;
this.Message = message;
} /// <summary>
/// Initializes a new instance of the <see cref="ValidationResult"/> class.
/// </summary>
/// <param name="message">The message.</param>
public ValidationResult(string message)
{
this.Message = message;
} /// <summary>
/// Gets or sets the name of the member.
/// </summary>
/// <value>
/// The name of the member. May be null for general validation issues.
/// </value>
public string MemberName { get; set; } /// <summary>
/// Gets or sets the message.
/// </summary>
/// <value>
/// The message.
/// </value>
public string Message { get; set; } }

三、使用示例

注册

             builder.RegisterType<DefaultCommandBus>().As<ICommandBus>().InstancePerLifetimeScope();

             var domainCommands = Assembly.Load("Frozen.DomainCommands");
builder.RegisterAssemblyTypes(domainCommands)
.AsClosedTypesOf(typeof(ICommandHandler<>)).InstancePerLifetimeScope();
builder.RegisterAssemblyTypes(domainCommands)
.AsClosedTypesOf(typeof(IValidationHandler<>)).InstancePerLifetimeScope();

定义Command

     /// <summary>
/// Command 删除学生
/// </summary>
public class DeleteStudentCommand : ICommand
{
/// <summary>
/// 学生Id
/// </summary>
public int StuId { get; set; } }

定义Handler

     /// <summary>
///
/// </summary>
public class DeleteStudentHandler : ICommandHandler<DeleteStudentCommand>
{
private readonly IStuEducationRepo _iStuEducationRepo; public DeleteStudentHandler(IStuEducationRepo iStuEducationRepo)
{
this._iStuEducationRepo = iStuEducationRepo;
} public ICommandResult Execute(DeleteStudentCommand command)
{ return new CommandResult(true);
} }

调用

             var command = new DeleteStudentCommand()
{
StuId =
};
var result = _commandBus.Submit(command);

结果:

附:源码下载

最新文章

  1. navicat 结合快捷键
  2. 零配置Socket TCP消息通讯服务容器EC
  3. js计算两个日期相隔几小时几分钟?
  4. YARN内存使用优化配置
  5. Activity中异步操作showDialog异常解决方法:判断Ay是否结束
  6. AutoMapper 创建嵌套对象映射(原创)
  7. 2、创建File类对象
  8. jenkins构建后操作添加“Publish to Subversion repository”与Eclipse更新提交SVN文件冲突
  9. BZOJ:4659&amp;&amp;BZOJ:2694: Lcm
  10. Navicat for MySQL导出表结构脚本的方法
  11. 09 ExpanableListView 的代码例子
  12. 一文读懂四种常见的XML解析技术
  13. spring为什么推荐使用构造器注入
  14. A1140. Look-and-say Sequence
  15. windows server 2003 安全加固(一)
  16. Android使用内容提供者实现增删改查操作
  17. 9.Libraries and visibility 库和可见性
  18. mysql的TIMESTAMPDIFF
  19. JVM培训之一些GC算法的理论知识
  20. Dijkstra【P2446】 [SDOI2010]大陆争霸

热门文章

  1. 洛谷P1309 瑞士轮
  2. [MEF] 学习之一 入门级的简单Demo(转)
  3. Automating CSS Regression Testing
  4. 在使用windows调用Hadoop 错误 /bin/bash: line 0: fg: no job control一般解决方法
  5. sqlite3添加、修改列名(转)
  6. 关于2B的转义问题
  7. RK3288 制作内核开机logo
  8. Linux 利用busybox制作根文件系统
  9. mysql innodb引擎事务的隔离级别
  10. find: ‘/run/user/1000/gvfs’: Permission denied