首页 > 解决方案 > 向 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

标签: c#entity-framework

解决方案


由于每一年都是不同的,但基于前一年,人们可以创建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

推荐阅读