【问题标题】:Working with multiple data models, helpers and reducing code repetition - C#使用多个数据模型、助手并减少代码重复 - C#
【发布时间】:2018-09-05 11:10:29
【问题描述】:

我正在使用 .Net Core 2.0 为 Web 服务构建模型和帮助程序类。我需要通过传入模型及其对象列表来对多个模型做一些重复的工作。到目前为止,我已经在每个辅助类中完全定义了该方法,因为每个类都传递了其各自的模型和对象列表。返回类型也是相同模型的对象列表。

是否有可能使用一种方法来简单地实现这一点,但是在每个帮助器类中,我必须传入并取回不同的数据模型及其列表以及其他一些参数,这些参数对于所有帮助器都是相同的类。

我会用一个简单的例子来解释这个场景。 我有名为 FullTimeEmployeePartTimeEmployeeContractEmployeePermanentEmployeeLocalEmployee 的模型类等等……

我还有名为 FullTimeEmployeesHelperPartTimeEmployeesHelperContractEmployeesHelperPermanentEmployeesHelperLocalEmployeesHelper 等等……

在每个帮助器类中,我都定义了一个方法,该方法接受相应的模型类、相应员工的列表、一个 int 和一个字符串。返回类型也是员工列表。

例如 FullTimeEmployeesHelper 中的方法看起来像

public static List<FullTimeEmployee> UpdateState(List<FullTimeEmployee> employeesList, FullTimeEmployee employee, int newValue, string propertyToBeUpdated) { }

PartTimeEmployeesHelper 中的方法看起来像

public static List<PartTimeEmployee> UpdateState(List<PartTimeEmployee> employeesList, PartTimeEmployee employee, int newValue, string propertyToBeUpdated) { }

我想定义一个方法并从每个帮助类中调用它,因为方法内部完成的所有工作都是相同的,只是数据模型不同。我试过使用 IEmployee 接口,但没有用。使用抽象类 Employee 也不起作用。我面临的主要问题是所有数据模型都有不同的属性。甚至没有一个属性对所有数据模型都是通用的。我传入的字符串是要更新的属性的名称。该方法在数据模型中查找属性并将其更改为作为 int 传递的 newValue。

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

【问题讨论】:

  • 您是否使用反射通过字符串名称查找属性?此外,当所有 5 种类型都不共享行为(都具有不同的属性)时,您如何尝试为所有 5 种类型保留一个抽象类?
  • 没有。仅使用 switch case 和一些数量非常小的助手,if .. else.. 语句。
  • 对我来说似乎是XYPropblem。您要解决的问题是什么?

标签: c# .net web-services helper .net-core-2.0


【解决方案1】:

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

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

    }

IEmployee 类:

    public class IEmployee
{

    public string FirstName { get; set; }
}

兼职/全职员工:

    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");
    }

希望您可以根据自己的需要调整此实现。有任何问题请告诉我。

其他信息Covariance and Contravariance in Generics

【讨论】:

    猜你喜欢
    • 2016-01-12
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    • 2013-01-28
    • 1970-01-01
    相关资源
    最近更新 更多