你的位置:首页 > ASP.net教程

[ASP.net教程]领域Command


一、项目文件

 

二、代码

1   /// <summary>2   /// 3   /// </summary>4   public interface ICommand5   {6   }

 1   /// <summary> 2   ///  3   /// </summary> 4   public interface ICommandResult 5   { 6     bool Success { get; } 7     int? ReturnKey { get; } 8  9     string ReturnKeys { get; }10 11   }

1   /// <summary>2   /// 3   /// </summary>4   /// <typeparam name="TCommand"></typeparam>5   public interface ICommandHandler<in TCommand> where TCommand : ICommand6   {7     ICommandResult Execute(TCommand command);8   }

1   /// <summary>2   /// 3   /// </summary>4   /// <typeparam name="TCommand"></typeparam>5   public interface IValidationHandler<in TCommand> where TCommand : ICommand6   {7     IEnumerable<ValidationResult> Validate(TCommand command);8   }

 1   /// <summary> 2   ///  3   /// </summary> 4   public class CommandResult : ICommandResult 5   { 6     public CommandResult(bool success, int? returnkey = null, string returnKeys = null) 7     { 8       this.Success = success; 9       this.ReturnKey = returnkey;10       this.ReturnKeys = returnKeys;11     }12 13     public bool Success { get; protected set; }14 15     public int? ReturnKey { get; protected set; }16 17     public string ReturnKeys { get; protected set; }18 19   }

 1   /// <summary> 2   ///  3   /// </summary> 4   public class CommandHandlerNotFoundException : Exception 5   { 6     public CommandHandlerNotFoundException(Type type) 7       : base(string.Format("Command handler not found for command type: {0}", type)) 8     { 9     }10   }

 1   /// <summary> 2   ///  3   /// </summary> 4   public class ValidationHandlerNotFoundException : Exception 5   { 6     public ValidationHandlerNotFoundException(Type type) 7       : base(string.Format("Validation handler not found for command type: {0}", type)) 8     { 9     }10   }

1   /// <summary>2   /// 3   /// </summary>4   public interface ICommandBus5   {6     ICommandResult Submit<TCommand>(TCommand command) where TCommand : ICommand;7     IEnumerable<ValidationResult> Validate<TCommand>(TCommand command) where TCommand : ICommand;8   }

 1   /// <summary> 2   ///  3   /// </summary> 4   public class DefaultCommandBus : ICommandBus 5   { 6     public ICommandResult Submit<TCommand>(TCommand command) where TCommand : ICommand 7     { 8       var handler = (ICommandHandler<TCommand>)DependencyDefaultInstanceResolver.GetInstance(typeof(ICommandHandler<TCommand>)); 9       if (!((handler != null) && handler is ICommandHandler<TCommand>))10       {11         throw new CommandHandlerNotFoundException(typeof(TCommand));12       }  13       return handler.Execute(command);14     }15 16     public IEnumerable<ValidationResult> Validate<TCommand>(TCommand command) where TCommand : ICommand17     {18       var handler = (IValidationHandler<TCommand>)DependencyDefaultInstanceResolver.GetInstance(typeof(IValidationHandler<TCommand>));19       if (!((handler != null) && handler is IValidationHandler<TCommand>))20       {21         throw new ValidationHandlerNotFoundException(typeof(TCommand));22       }23       return handler.Validate(command);24     }25 26   }

 

 1   /// <summary> 2   /// Describes the result of a validation of a potential change through a business service. 3   /// </summary> 4   public class ValidationResult 5   { 6  7     /// <summary> 8     /// Initializes a new instance of the <see cref="ValidationResult"/> class. 9     /// </summary>10     public ValidationResult()11     {12     }13 14     /// <summary>15     /// Initializes a new instance of the <see cref="ValidationResult"/> class.16     /// </summary>17     /// <param name="memeberName">Name of the memeber.</param>18     /// <param name="message">The message.</param>19     public ValidationResult(string memeberName, string message)20     {21       this.MemberName = memeberName;22       this.Message = message;23     }24 25     /// <summary>26     /// Initializes a new instance of the <see cref="ValidationResult"/> class.27     /// </summary>28     /// <param name="message">The message.</param>29     public ValidationResult(string message)30     {31       this.Message = message;32     }33 34     /// <summary>35     /// Gets or sets the name of the member.36     /// </summary>37     /// <value>38     /// The name of the member. May be null for general validation issues.39     /// </value>40     public string MemberName { get; set; }41 42     /// <summary>43     /// Gets or sets the message.44     /// </summary>45     /// <value>46     /// The message.47     /// </value>48     public string Message { get; set; }49 50   }

 

三、使用示例

注册

1       builder.RegisterType<DefaultCommandBus>().As<ICommandBus>().InstancePerLifetimeScope();2 3       var domainCommands = Assembly.Load("Frozen.DomainCommands");4       builder.RegisterAssemblyTypes(domainCommands)5         .AsClosedTypesOf(typeof(ICommandHandler<>)).InstancePerLifetimeScope();6       builder.RegisterAssemblyTypes(domainCommands)7         .AsClosedTypesOf(typeof(IValidationHandler<>)).InstancePerLifetimeScope();

定义Command

 1   /// <summary> 2   /// Command 删除学生 3   /// </summary> 4   public class DeleteStudentCommand : ICommand 5   { 6     /// <summary> 7     /// 学生Id 8     /// </summary> 9     public int StuId { get; set; }10 11   }

定义Handler

 1   /// <summary> 2   ///  3   /// </summary> 4   public class DeleteStudentHandler : ICommandHandler<DeleteStudentCommand> 5   { 6     private readonly IStuEducationRepo _iStuEducationRepo; 7  8     public DeleteStudentHandler(IStuEducationRepo iStuEducationRepo) 9     {10       this._iStuEducationRepo = iStuEducationRepo;11     }12 13     public ICommandResult Execute(DeleteStudentCommand command)14     {15 16 17       return new CommandResult(true);18     }19 20   }

调用

1       var command = new DeleteStudentCommand()2       {3         StuId = 14       };5       var result = _commandBus.Submit(command);

结果:

 

已打包成附件

http://files.cnblogs.com/files/frozenzhang/Frozen.DomainCommands.zip