C# 使用多个数据模型、助手和减少代码重复-C

C# 使用多个数据模型、助手和减少代码重复-C,c#,.net,web-services,helper,.net-core-2.0,C#,.net,Web Services,Helper,.net Core 2.0,我正在使用.NETCore2.0为web服务构建模型和助手类。我需要通过传入模型及其对象列表,在许多模型上重复工作。到目前为止,我已经在每个helper类中完全定义了该方法,因为每个类都传递其各自的模型和对象列表。返回类型也是同一模型的对象列表 在任何情况下都可以简单地使用一个方法,但在每个帮助器类中,我必须传入并返回不同的数据模型及其列表以及一些其他参数,这些参数对于所有帮助器类都是相同的 我将用一个简单的例子来解释这个场景。 我有一个名为FullTimeEmployee、PartTimeEm

我正在使用.NETCore2.0为web服务构建模型和助手类。我需要通过传入模型及其对象列表,在许多模型上重复工作。到目前为止,我已经在每个helper类中完全定义了该方法,因为每个类都传递其各自的模型和对象列表。返回类型也是同一模型的对象列表

在任何情况下都可以简单地使用一个方法,但在每个帮助器类中,我必须传入并返回不同的数据模型及其列表以及一些其他参数,这些参数对于所有帮助器类都是相同的

我将用一个简单的例子来解释这个场景。 我有一个名为FullTimeEmployee、PartTimeEmployee、ContractEmployee、PermanentEmployee、LocalEmployee等的模型类

我还有名为FullTimeEmployeesHelper、PartTimeEmployeesHelper、ContractEmployeesHelper、PermanentEmployeesHelper、LocalEmployeesHelper等助手类

在每个helper类中,我必须定义一个方法,该方法接受相应的模型类、相应的雇员列表、一个int和一个字符串。退货类型也是员工列表

例如,FullTimeEmployeesHelper中的方法如下

public static List<FullTimeEmployee> UpdateState(List<FullTimeEmployee> employeesList, FullTimeEmployee employee, int newValue, string propertyToBeUpdated) { }
public static List<PartTimeEmployee> UpdateState(List<PartTimeEmployee> employeesList, PartTimeEmployee employee, int newValue, string propertyToBeUpdated) { }
PartTimeEmployeesHelper中的方法如下

public static List<FullTimeEmployee> UpdateState(List<FullTimeEmployee> employeesList, FullTimeEmployee employee, int newValue, string propertyToBeUpdated) { }
public static List<PartTimeEmployee> UpdateState(List<PartTimeEmployee> employeesList, PartTimeEmployee employee, int newValue, string propertyToBeUpdated) { }
我想定义一个方法并从每个helper类中调用它,因为在该方法中完成的所有工作都是相同的,只是数据模型不同。我尝试过使用IEEmployee接口,但没有成功。使用抽象类Employee也不起作用。我面临的主要问题是,所有数据模型都有不同的属性。甚至没有一个属性是所有数据模型的公共属性。我传入的字符串是要更新的属性的名称。该方法在数据模型中查找属性,并将其更改为作为int传递的newValue


有没有办法减少代码重复并只使用可以从所有助手调用的方法?

您可以使用组合或IEmployee和泛型。我没有看到您希望如何使用更新的属性示例,因此我创建了自己的实现。在下面的代码中,我更新了传递到方法中的employee属性。然后,我将其添加到原始列表中并返回

       public static List<IEmployee> UpdateState<T>(List<T> employeesList, T employee, int newValue, string propertyToBeUpdated) where T : IEmployee
    {
        T myObject = employee;

        PropertyInfo propInfo = myObject.GetType().GetProperty(propertyToBeUpdated);

        propInfo.SetValue(employee, Convert.ChangeType(newValue, propInfo.PropertyType), null);

        employeesList.Insert(employeesList.Count, myObject);

        return ((IEnumerable<IEmployee>)employeesList).ToList();

    }
兼职/全职员工:

    public class PartTimeEmployee : IEmployee
{
    public int PartTimeOnly { get; set; }
}

    public class FullTimeEmployee : IEmployee
{
    public int FullTimeOnly { get; set; }
}
主程序:

        static void Main(string[] args)
    {
        var newList = EmployeeHelper.UpdateState(new List<FullTimeEmployee>() { new FullTimeEmployee() { FirstName = "Ryan1", FullTimeOnly = 1 } }, new FullTimeEmployee() { FirstName = "Ryan2", FullTimeOnly = 2 }, 9, "FullTimeOnly");

        var newList2 = EmployeeHelper.UpdateState(new List<PartTimeEmployee>() { new PartTimeEmployee() { FirstName = "Roy1" } }, new PartTimeEmployee() { FirstName = "Roy", PartTimeOnly = 6 }, 2, "PartTimeOnly");
    }
希望您可以根据自己的需要调整此实现。任何问题都让我知道


其他信息

是否使用反射通过使用字符串名称查找属性?此外,当这5种类型中没有一种共享行为都具有不同的属性时,您是如何尝试为所有这5种类型保留一个抽象类的。。其他的陈述。对我来说似乎是一个问题。你想解决的问题是什么?