您的位置:首页 > 其它

领域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);


结果:



附:源码下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: