我有一个插入和更新实体的代码,但逻辑非常相似:实例一个控制器,验证请求,如果错误,返回失败,否则执行操作。
/// <summary>
/// Method Add with similar code in Update method
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public PersonResponse Add(PersonInsertRequest item)
{
var result = new PersonResponse();
var controller = new PersonController(classmates);
result.Messages = controller.ValidateInsert(item);
if (result.Messages != null && result.Messages.Any())
{
result.Status = "failed";
}
else
{
result = controller.ExecuteInsert(item);
}
return result;
}
/// <summary>
/// Method Update with similar code in Add method
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public PersonResponse Update(PersonUpdateRequest item)
{
var result = new PersonResponse();
var controller = new PersonController(classmates);
result.Messages = controller.ValidateUpdate(item);
if (result.Messages != null && result.Messages.Any())
{
result.Status = "failed";
}
else
{
result = controller.ExecuteUpdate(item);
}
return result;
}
- 我尝试使用委托,但无法使用它们,因为我无法将委托分配给controller.ValidateInsert(item);
public class BaseLogic
{
public delegate List<Message> Validator(BaseRequest request);
public delegate BaseResponse Executor(BaseRequest request);
我想减少另一个更通用的 Add 方法和 Update 方法。
拉莫斯之舞
杨魅力
相关分类