领域Command
2016-04-05 16:44
459 查看
一、项目结构
二、代码
/// <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 = 1 }; var result = _commandBus.Submit(command);
结果:
附:源码下载
相关文章推荐
- shiro
- 纽扣游戏---博弈(腾讯2017实习生题目)
- NLTK vs Sklearn vs Gensim
- 第六周书面项目2-变量的变化(1)
- CALayer的mask属性简介
- kafka安装
- Charles使用介绍--MAC上优秀的抓包工具
- java 多线程相关知识
- swift实现ios类似微信输入框跟随键盘弹出的效果
- 【转】eclipse android 设置及修改生成apk的签名文件 -- custom debug keystore
- GitHub使用方法(初级)
- HTML data属性简介以及低版本浏览器兼容算法
- SSD和HDD的区别
- zoj 3689 Digging 贪心+01背包
- 打开系统图库
- 关于JS中的apply,call,bind的深入解析
- Linux NFS服务器的安装与配置
- Windows 内置 .NET Framework 版本号
- Arcgis server 10.2 配置Oracle监听使用SQL操作ST_geometry
- 自定义view之圆点progressBar