如何减少我的冗余代码,创建通用方法?

时间:2019-07-19 22:50:27

标签: c# delegates func

我有一个插入和更新实体的代码,但是逻辑非常相似:实例化一个控制器,验证请求,如果出错,则返回失败,否则执行该操作。


        /// <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);

        public BaseResponse Execute(BaseRequest request, Validator funcValidator, Executor funcExecutor)
        {
            var result = new BaseResponse();

            result.Messages = funcValidator(request);

            if (result.Messages != null && result.Messages.Any())
            {
                result.Status = "failed";
            }
            else
            {
                result = funcExecutor(request);
            }

            return result;
        }
    }

我想减少另一个更通用的Add方法和Update方法。

2 个答案:

答案 0 :(得分:1)

using System;
using System.Collections.Generic;
using System.Linq;

class Test
{
    private object classmates;

public PersonResponse Add(PersonInsertRequest item)
{
    var controller = new PersonController(classmates);
    return ValidateAndExecute(() => controller.ValidateInsert(item),
    () => controller.ExecuteInsert(item));
}

public PersonResponse Update(PersonUpdateRequest item)
{
    var controller = new PersonController(classmates);
    return ValidateAndExecute(() => controller.ValidateUpdate(item),
    () => controller.ExecuteUpdate(item));
}

private PersonResponse ValidateAndExecute(Func<IEnumerable<string>> validator,
Func<PersonResponse> execute)
{
    var result = new PersonResponse();

    result.Messages = validator();

    if (result.Messages != null && result.Messages.Any())
    {
        result.Status = "failed";
    }
    else
    {
        result = execute();
    }

    return result;
}

}

答案 1 :(得分:0)

使用我的基类中的通用方法,我可以重构我的方法

/// <summary>
        /// Method Add with similar code in Update method
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public PersonResponse Add(PersonInsertRequest item)
        {

            var controller = new PersonController(classmates);

            Validator myValidator = request => controller.ValidateInsert((PersonInsertRequest)request);
            Executor myExecutor = request => controller.ExecuteInsert((PersonInsertRequest)request);

            var result = Execute(item, myValidator, myExecutor);

            return result as PersonResponse;
        }

        /// <summary>
        /// Method Update with similar code in Add method
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public PersonResponse Update(PersonUpdateRequest item)
        {
            var controller = new PersonController(classmates);

            Validator myValidator = request => controller.ValidateUpdate((PersonUpdateRequest)request);
            Executor myExecutor = request => controller.ExecuteUpdate((PersonUpdateRequest)request);

            var result = Execute(item, myValidator, myExecutor);

            return result as PersonResponse;
        }