领域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 ...
随机推荐
- web.xml配置详解(2)
1 定义头和根元素 部署描述符文件就像所有XML文件一样,必须以一个XML头开始.这个头声明可以使用的XML版本并给出文件的字符编码.DOCYTPE声明必须立即出现在此头之后.这个声明告诉服务器适用的 ...
- BZOJ3277 串 【广义后缀自动机】
Description 字符串是oi界常考的问题.现在给定你n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中 至少k个字符串的子串(注意包括本身). Input 第一行两个整数n, ...
- python面试题(十)
Python中基本数据结构的操作 元组 列表 字典 集合 定义 新增 更改 删除 2.请尽可能列举python列表的成员方法,并给出一下列表操作的答案: (1)a=[1, 2, 3, 4, 5], a ...
- nginx 调试
配置单进程非daemon方式启动 daemon off; master_process off;
- nginx 支持ie 6 等低版本https 的配置
nginx 配置 https 支持ie6 等低版本(主要是加密套件的问题) server { listen 443 ssl; server_name itapiway.demo.com; ssl_ce ...
- git忽略已经提交的文件【转载】
有时候我们添加.gitignore文件之前已经提交过了文件..gitignore只能忽略那些原来没有被track的文件(自添加以后,从未 add 及 commit 过的文件),如果某些文件已经被纳入了 ...
- 关于CSS单位:rem vh vw vmin vmax
rem(root em) 如果你给body设置了font-size字体大小,那么body的任何子元素的1em就是等于body设置的font-size demo: body { font-size: ...
- C#软件安全 反编译 加密与安全等等
我最近开发了一些C#语言的软件,但是由于这是一种解释型语言,也就是会转化成中间件语言,很容易就被反编译解密,包括exe和dll库等等,这时候我们真的需要使用一些技巧来将自己的成果进行加密,加壳等办法. ...
- HotSpot Stop-and-Copy GC
rednaxelafx的Cheney算法的伪代码.如果不用forwarding的话,维护一个旧地址到新地址的映射也可以. 其中重点部分: void Heap::collect() { // The f ...
- 贴一段demo代码,演示channel之间的同步
package main import ( "fmt" "time" ) func deskGoRoutine(index int, userChannel c ...