Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 组织文件版本迁移代码的最佳方法是什么?_C#_.net_Migration_Versioning_Data Migration - Fatal编程技术网

C# 组织文件版本迁移代码的最佳方法是什么?

C# 组织文件版本迁移代码的最佳方法是什么?,c#,.net,migration,versioning,data-migration,C#,.net,Migration,Versioning,Data Migration,我正在通过API为另一个程序编写插件。为了保存它的当前状态,我的插件通过将项目对象的实例序列化为XML并将其存储在用户字符串(API提供的功能)中,将其存储到文件中。它还存储一个单独的字符串,其中包含当前正在使用的插件的版本号 当用户打开文件时,我的插件会检查版本号。如果当前插件版本与文件中存储的版本不同,则会弹出一条消息,警告用户不同的版本,该文件可能导致插件崩溃 我更愿意提供一组迁移脚本,当用户在较新版本的插件中打开较旧的文件时,这些脚本会自动运行。但我的问题是,它们通常会去哪里?它们是如何

我正在通过API为另一个程序编写插件。为了保存它的当前状态,我的插件通过将项目对象的实例序列化为XML并将其存储在用户字符串(API提供的功能)中,将其存储到文件中。它还存储一个单独的字符串,其中包含当前正在使用的插件的版本号

当用户打开文件时,我的插件会检查版本号。如果当前插件版本与文件中存储的版本不同,则会弹出一条消息,警告用户不同的版本,该文件可能导致插件崩溃

我更愿意提供一组迁移脚本,当用户在较新版本的插件中打开较旧的文件时,这些脚本会自动运行。但我的问题是,它们通常会去哪里?它们是如何组织的

也就是说,我的项目类在不同版本之间发生了显著的变化,如果尝试从旧文件中反序列化项目,那么新的项目类将失败。我不想保留Project类的每个版本的副本,因为它们是我的程序集,但同时必须通过XML进行解析将更加痛苦。假设解析XML是最好的选择,那么有人能建议一种比下面的代码更有组织的方法吗

public string MigrateProject(int fileVersion, int plugInversion, string proj)
{
    if(fileVersion>plugInversion)
    {
       //tell user to upgrade their copy of the plugin
       return null;
    }

    if(fileVersion ==1)
    { 
        string v2 = Migrate1to2(serializedProject);
        string v3 = Migrate2to3(v2);
        string v4 = Migrate3to4(v3);
        return v4;
    }
    else if(fileVersion ==2)
    { 
        string v3 = Migrate2to3(serializedProject);
        string v4 = Migrate3to4(v3);
        return v4;
    }
    else if(fileVersion ==3)
    { 
        string v4 = Migrate3to4(serializedProject);
        return v4;
    }
    else 
    {
         //could not migrate project message
         return null;
    }
}

XmlSerializer是不允许版本的,如果您不包括一个版本字段,您可以在手动进行反序列化时对该字段进行操作

支持SoapFormatter和的版本

您仍然需要处理转换,并且可以通过以下方式轻松剪切代码:

if(fileVersion ==1)
{ 
    serializedProject = Migrate1to2(serializedProject);
    fileVersion = 2;
}
if(fileVersion ==2)
{ 
    serializedProject = Migrate2to3(serializedProject);
    fileVersion = 3;
}
if(fileVersion ==3)
{ 
    serializedProject = Migrate3to4(serializedProject);
    fileVersion = 4
}
else 
{
     //could not migrate project message
     return null;
}

XmlSerializer是不允许版本的,如果您不包括一个版本字段,您可以在手动进行反序列化时对该字段进行操作

支持SoapFormatter和的版本

您仍然需要处理转换,并且可以通过以下方式轻松剪切代码:

if(fileVersion ==1)
{ 
    serializedProject = Migrate1to2(serializedProject);
    fileVersion = 2;
}
if(fileVersion ==2)
{ 
    serializedProject = Migrate2to3(serializedProject);
    fileVersion = 3;
}
if(fileVersion ==3)
{ 
    serializedProject = Migrate3to4(serializedProject);
    fileVersion = 4
}
else 
{
     //could not migrate project message
     return null;
}

将迁移方法存储在如下列表中:

List<Func<string,string>> myMigrateMethods = new List<Func<string,string>>();
myMigrateMethods.Add(Migrate1To2);
myMigrateMethods.Add(Migrate2To3);
myMigrateMethods.Add(Migrate3To4);
List myMigrateMethods=new List();
myMigrateMethods.Add(migrate1到2);
myMigrateMethods.Add(Migrate2To3);
添加(Migrate3To4);
然后从适当的方法开始遍历列表:

public string MigrateProject(int fileVersion, int plugInversion, string proj)
{
    if(fileVersion>plugInversion)
    {
       //tell user to upgrade their copy of the plugin
       return null;
    }

    //user already at max version
    if(fileVersion >= (myMigrateMethods.Length-1)) return null;

    var firstMigrateMethodNeeded = (fileVersion-1); //array is 0-based

    var output = serializedProject;
    for(var i= firstMigrateMethodNeeded; i< myMigrateMethods.Length; i++)
    {
       output = myMigrateMethods[i](output);
    }

    return output;

}
publicstringmigrateproject(int-fileVersion、int-plugInversion、stringproj)
{
如果(文件版本>插件反转)
{
//告诉用户升级他们的插件副本
返回null;
}
//用户已处于最大版本
if(fileVersion>=(myMigrateMethods.Length-1))返回null;
var firstMigrateMethodNeeded=(fileVersion-1);//数组基于0
var输出=序列化项目;
for(var i=firstMigrateMethodNeeded;i
将迁移方法存储在如下列表中:

List<Func<string,string>> myMigrateMethods = new List<Func<string,string>>();
myMigrateMethods.Add(Migrate1To2);
myMigrateMethods.Add(Migrate2To3);
myMigrateMethods.Add(Migrate3To4);
List myMigrateMethods=new List();
myMigrateMethods.Add(migrate1到2);
myMigrateMethods.Add(Migrate2To3);
添加(Migrate3To4);
然后从适当的方法开始遍历列表:

public string MigrateProject(int fileVersion, int plugInversion, string proj)
{
    if(fileVersion>plugInversion)
    {
       //tell user to upgrade their copy of the plugin
       return null;
    }

    //user already at max version
    if(fileVersion >= (myMigrateMethods.Length-1)) return null;

    var firstMigrateMethodNeeded = (fileVersion-1); //array is 0-based

    var output = serializedProject;
    for(var i= firstMigrateMethodNeeded; i< myMigrateMethods.Length; i++)
    {
       output = myMigrateMethods[i](output);
    }

    return output;

}
publicstringmigrateproject(int-fileVersion、int-plugInversion、stringproj)
{
如果(文件版本>插件反转)
{
//告诉用户升级他们的插件副本
返回null;
}
//用户已处于最大版本
if(fileVersion>=(myMigrateMethods.Length-1))返回null;
var firstMigrateMethodNeeded=(fileVersion-1);//数组基于0
var输出=序列化项目;
for(var i=firstMigrateMethodNeeded;i