一、项目结构

二、代码

     /// <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);

结果:

附:源码下载

领域Command的更多相关文章

  1. 搭建一个Web API项目(DDD)

    传送阵:写在最后 一.创建一个能跑的起来的Web API项目 1.建一个空的 ASP.NET Web应用 (为什么不直接添加一个Web API项目呢,那样会有些多余的内容(如js.css.Areas等 ...

  2. 关于领域驱动设计(DDD)中聚合设计的一些思考

    关于DDD的理论知识总结,可参考这篇文章. DDD社区官网上一篇关于聚合设计的几个原则的简单讨论: 文章地址:http://dddcommunity.org/library/vernon_2011/, ...

  3. ENode框架单台机器在处理Command时的设计思路

    设计目标 尽量快的处理命令和事件,保证吞吐量: 处理完一个命令后不需要等待命令产生的事件持久化完成就能处理下一个命令,从而保证领域内的业务逻辑处理不依赖于持久化IO,实现真正的in-memory: 保 ...

  4. 一缕阳光:DDD(领域驱动设计)应对具体业务场景,如何聚焦 Domain Model(领域模型)?

    写在前面 阅读目录: 问题根源是什么? <领域驱动设计-软件核心复杂性应对之道>分层概念 Repository(仓储)职责所在? Domain Model(领域模型)重新设计 Domain ...

  5. 领域驱动设计系列 (六):CQRS

    CQRS是Command Query Responsibility Seperation(命令查询职责分离)的缩写. 世上很多事情都比较复杂,但是我们只要进行一些简单的分类后,那么事情就简单了很多,比 ...

  6. IDDD 实现领域驱动设计-一个简单的 CQRS 示例

    上一篇:<IDDD 实现领域驱动设计-CQRS(命令查询职责分离)和 EDA(事件驱动架构)> 学习架构知识,需要有一些功底和经验,要不然你会和我一样吃力,CQRS.EDA.ES.Saga ...

  7. IDDD 实现领域驱动设计-CQRS(命令查询职责分离)和 EDA(事件驱动架构)

    上一篇:<IDDD 实现领域驱动设计-SOA.REST 和六边形架构> 阅读目录: CQRS-命令查询职责分离 EDA-事件驱动架构 Domin Event-领域事件 Long-Runni ...

  8. IDDD 实现领域驱动设计-架构之经典分层

    上一篇:<IDDD 实现领域驱动设计-上下文映射图及其相关概念> 在<实现领域驱动设计>书中,分层的概念作者讲述的很少,也就几页的内容,但对于我来说,有很多的感触需要诉说.之前 ...

  9. Command and Query Responsibility Segregation (CQRS) Pattern 命令和查询职责分离(CQRS)模式

    Segregate operations that read data from operations that update data by using separate interfaces. T ...

随机推荐

  1. 每天一个linux命令:【转载】mkdir命令

    linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录. 1.命令格式: mkdir [选项] 目录... 2.命令 ...

  2. Django mysql 字符集问题

    http://www.cnblogs.com/discuss/articles/1862248.html http://www.cnblogs.com/moinmoin/archive/2011/02 ...

  3. Python学习-类

    类是对象的模板或蓝图,类是对象的抽象化,对象是类的实例化 在python中,一个对象的特征也称为属性(attribute),它所具有的的行为也称为方法(method) 对象 = 属性(特征)+方法(行 ...

  4. springboot: 集成jdbc

    1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...

  5. web 前端 html

    1,什么是web 在网络中,大量的数据需要有一个载体,而很多人都能够访问这个载体,利用浏览器的这个窗口链接一个有一个载体,这个载体就是网站也就是web的前身. 1,web标准:结构标准,表现标准,行为 ...

  6. cookie添加中文encoding/decoding

    添加时编码,读取是解码,否则编译不过 Cookie cookie=new Cookie("姓名","黄嘎兵"); respose.addCookie(cooki ...

  7. Linnx 服务器中mysql 无法正常访问问题

    本机连接远程Linnx服务器不通 1. 检测防火墙 -- 保证防火墙关闭 查看到iptables服务的当前状态:service iptables status. 但是即使服务运行了,防火墙也不一定起作 ...

  8. java代码-------继承的方法----重写还是重载

    总结:是自己不听讲吧,不懂啊 感觉父类的方法,子类可以重载,只要参数个数不同,重载与返回值没有关系 重写绝对是可以的.但答案是只能重写啊 package com.s.x; public class T ...

  9. 第十章 hbase默认配置说明

    hbase.rootdir:这个目录是region  server的共享目录,用来持久化Hbase.URL需要是'完全正确'的,还要包含文件系统的scheme.例如,要表示hdfs中的 '/hbase ...

  10. MySQL升级指南

    一 .MySQL升级 1.官方升级策略 注意 升级过程中必须使用具有管理权限的MySQL帐户来执行SQL语句. 1.升级方法 逻辑升级: 涉及使用 mysqldump从旧的MySQL版本导出现有数据 ...