领域Command
一、项目结构

二、代码
/// <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的更多相关文章
- 搭建一个Web API项目(DDD)
传送阵:写在最后 一.创建一个能跑的起来的Web API项目 1.建一个空的 ASP.NET Web应用 (为什么不直接添加一个Web API项目呢,那样会有些多余的内容(如js.css.Areas等 ...
- 关于领域驱动设计(DDD)中聚合设计的一些思考
关于DDD的理论知识总结,可参考这篇文章. DDD社区官网上一篇关于聚合设计的几个原则的简单讨论: 文章地址:http://dddcommunity.org/library/vernon_2011/, ...
- ENode框架单台机器在处理Command时的设计思路
设计目标 尽量快的处理命令和事件,保证吞吐量: 处理完一个命令后不需要等待命令产生的事件持久化完成就能处理下一个命令,从而保证领域内的业务逻辑处理不依赖于持久化IO,实现真正的in-memory: 保 ...
- 一缕阳光:DDD(领域驱动设计)应对具体业务场景,如何聚焦 Domain Model(领域模型)?
写在前面 阅读目录: 问题根源是什么? <领域驱动设计-软件核心复杂性应对之道>分层概念 Repository(仓储)职责所在? Domain Model(领域模型)重新设计 Domain ...
- 领域驱动设计系列 (六):CQRS
CQRS是Command Query Responsibility Seperation(命令查询职责分离)的缩写. 世上很多事情都比较复杂,但是我们只要进行一些简单的分类后,那么事情就简单了很多,比 ...
- IDDD 实现领域驱动设计-一个简单的 CQRS 示例
上一篇:<IDDD 实现领域驱动设计-CQRS(命令查询职责分离)和 EDA(事件驱动架构)> 学习架构知识,需要有一些功底和经验,要不然你会和我一样吃力,CQRS.EDA.ES.Saga ...
- IDDD 实现领域驱动设计-CQRS(命令查询职责分离)和 EDA(事件驱动架构)
上一篇:<IDDD 实现领域驱动设计-SOA.REST 和六边形架构> 阅读目录: CQRS-命令查询职责分离 EDA-事件驱动架构 Domin Event-领域事件 Long-Runni ...
- IDDD 实现领域驱动设计-架构之经典分层
上一篇:<IDDD 实现领域驱动设计-上下文映射图及其相关概念> 在<实现领域驱动设计>书中,分层的概念作者讲述的很少,也就几页的内容,但对于我来说,有很多的感触需要诉说.之前 ...
- Command and Query Responsibility Segregation (CQRS) Pattern 命令和查询职责分离(CQRS)模式
Segregate operations that read data from operations that update data by using separate interfaces. T ...
随机推荐
- 1153 Decode Registration Card of PAT (25 分)
A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, nam ...
- java集成WebSocket向所有用户发送消息
package com.reading.controller.library; import org.springframework.stereotype.Controller; import org ...
- String.format()格式化日期(2)
在以前的开发中,日期格式化一直使用的是SimpleDateFormat进行格式化.今天发现String.format也可以格式化.当 然,两种方式的优劣没有进行深入分析. 1. 日期格式化 (2018 ...
- 【转】linux内核态和用户态的区别
原文网址:http://www.mike.org.cn/articles/linux-kernel-mode-and-user-mode-distinction/ 内核态与用户态是操作系统的两种运行级 ...
- 【转】理解Linux 配置文件
原文网址:http://www.mike.org.cn/articles/understanding-linux-configuration-files-linux/ 介绍每个 Linux 程序都是一 ...
- hadoop之 HDFS fs 命令总结
版本:Hadoop 2.7.4 -- 查看hadoop fs帮助信息[root@hadp-master sbin]# hadoop fsUsage: hadoop fs [generic option ...
- ECMALL模板解析机制.MVC架构分析及文件目录说明.二次开发指南手册(转)
ECMALL模板解析语法与机制 http://www.nowamagic.net/architecture/archt_TemplateSyntaxAndAnalysis.php ECMALL模块开发 ...
- Unit05: WEB项目的开发模式 、转发 和 Unit09: EL、JSTL
Unit05: WEB项目的开发模式 .转发 和 Unit09: EL.JSTL dao package dao; import java.io.Serializable; import jav ...
- 模拟Linux修改实际、有效和保存设置标识
就是模拟setuid seteuid setreuid setresuid,感觉代码比书上大段的文字好记,就写成代码形式了. // setuid.cc: 模拟<unistd.h>中的设置用 ...
- 写java代码有感。。。构造方法最好带着,
(一) 小结:具体我最大的担心,害怕就是方法调用的时候,new对象之后,赋值,是在new后面的括号里实现,还是在 对象.方法名()这样的.当然带参数的构造方法,调用时本身就直接调用,普通方法,选后者. ...