c# - 向 Entity 类添加版本控制
问题描述
我有一个带有 2 个实体类的旧代码库(假设有 10 个度量)。旧功能的工作方式就像每当您上传 Excel 文件时,它都会解析文件并映射到这些实体类。现在根据新要求,每年我都会向这些实体类添加更多度量,并获得具有 5 个度量的新 Excel 文件。如何根据每年添加版本控制,或者如何每年仅从一个基本实体类映射到实体类。或者我怎样才能在不改变设计的情况下引入这个功能
实体类的例子——
[DataColumnAttribute]
public StatusLkup Insurance { get; set; }
[DataColumnAttribute]
public Code Status { get; set; }
[DataColumnAttribute]
public string test1 { get; set; }
Excel 文件示例
第 1 行 - 保险,州,测试 1 第 2 - 5 行,公司,测试
明年假设再向实体类添加 2 个新度量
[DataColumnAttribute]
public StatusLkup Insurance { get; set; }
[DataColumnAttribute]
public Code Status { get; set; }
[DataColumnAttribute]
public string test1 { get; set; }
[DataColumnAttribute]
public string test2 { get; set; }
[DataColumnAttribute]
public string test3 { get; set; }
明年excel文件
第 1 行 - 测试 1、测试 2 第 2 行 - 12、11
解决方案
由于每一年都是不同的,但基于前一年,人们可以创建Interfaces
将定义(版本)年份的年份。
无论差异如何,人们都可以创建接口,但关键是它对数据进行了版本化。
通过这样做,您将对数据进行版本化,并能够相应地使用/排序它,而无需更改当前代码,只需每年添加新代码。
例子
两个接口,第二个继承第一个:
public interface IYear1
{
int Test1 { get; set;}
}
public interface IYear2 : IYear1
{
int Test2 { get; set;}
}
然后派生的类:
public class Year1 : IYear1
{
public int Test1 { get; set;}
}
public class Year2 : Year1, IYear2
{
public int Test2 { get; set; }
}
然后在代码中我们可以创建/确定正在处理的年份:
var year = new Year2() { Test1 = 1, Test2 = 2};
if (year is IYear2)
Console.WriteLine("Year 2 Found");
if (year is IYear1)
Console.WriteLine("Year 1 Found");
结果是我们确定了两个正在使用的版本:
Year 2 Found
Year 1 Found
推荐阅读
- sql - 使用更新语句删除行
- docker - 从 Gitlab CI/CD 管道执行 docker 命令时权限被拒绝
- python - 找不到 Chrome 驱动程序路径?- 硒 - MacOS
- c - 在 for 循环内和 for 循环外递增
- c# - Nest 6.2.0 自引用循环
- c# - 将数据上传到 SQL 表(实体框架)后,实体按时间排序错误
- oracle - 在 cursor.pl 第 5 行的模式匹配 (m//) 中使用未初始化的值 $_
- c# - 使用 ASP.NET Core 2.1 在控制器上配置输入/输出格式化程序
- java - Eclipse 插件不分透视
- java - Java - Jackson Yaml - 定义键列表的正确方法:对象中的值对