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

How to reduce my redundant code, creating generic methods?

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


        /// <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 方法。

在我的基础中使用我的泛型方法 class,我可以重构我的方法

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

}