使用接口将保存数据写入文件

Using interface for writing save data to a file

好像我偶然发现了一个与设计相关的问题,但无法解决问题如下...我有两个类 使用不同的 结构 存储和处理 它们在内存中的数据。还有另一个 class 用于 存储数据 基本上在名为 File_Data_Handler[=35= 的 文件 上] 它本身 实现了一个接口 IDController 用于对文件进行读取、写入、搜索、删除操作。现在我面临的真正问题是如何在接口和classes 用于基于不同结构写入数据,同时保留与数据写入机制相关的代码在File_Data_Handler 相同的。任何帮助将不胜感激,在此先感谢。这是代码接口

public interface IDInterface
{

    /*Read data from the source*/
    object ReadData(params object[] _args);

    /*Write data from the source*/
    void WriteData(params object [] _args);

    /*Remove data from the source*/
    bool RemoveData(params object[] _args);

    /*Remove all data from the source*/
    bool RemoveAll(params object[] _args);

}

文件数据编写器代码...

public class File_Data_Handler : IDInterface
{

    #region Attribute/Properties

    /*Private Variables*/
    private XmlWriter _writer = null;   //Content writer for class.
    private string _fileLocation="";    //Content storage location.
    private string _fileName = "";  //Content file name.

    #endregion

    #region Compiler Implicit Callbacks

    /*Constructor*/
    public File_Data_Handler(string _fileLocation,string _fileName)
    {

        /*Instance Initialization*/
        this._fileLocation = _fileLocation;
        this._fileName = _fileName;

        /*Make file ready*/
        FileExistanceFlag(_fileName,_fileLocation);

    }

    #endregion

    #region Base overriden Methods

    /*Read data from the source*/
    public object ReadData(params object[] _args)
    {
        return "";
    }

    /*Write data from the source*/
    public void WriteData(params object[] _args)
    {

    }

    /*Remove data from the source*/
    public bool RemoveData(params object[] _args)
    {
        return true;
    }

    /*Remove all data from the source*/
    public bool RemoveAll(params object[] _args)
    {
        return true;
    }

    #endregion

    #region Processing Methods

    /*Check file existance*/
    private void FileExistanceFlag(string _fileName,string _fileLocation)
    {

        if (!File.Exists(_fileLocation + _fileName))
        {
            File.Create(_fileLocation + _fileName);
        }

    }

    #endregion


}

类 包含需要写入的结构

    public classA
{
 public structA
  {
   public int id;
   public string name;
   public string path;
  }
}

public classB
{
 public structB
  {
   public int id;
   public string name;
  }
}

现在我如何通过接口映射这些不同的结构,以便将其写入文件。任何对此设计问题的优雅解决方案将不胜感激。

我不确定你到底在问什么,但我相信下面的例子会详细说明如何实现你的目标。

public interface IDataProcessor<TConfiguration>
{
     IEnumerable<TEntity> Read<TEntity>(TConfiguration configuration);
}

相当简单的代码,您有两个泛型,一个用于您希望构建的对象,另一个用于关联配置或交互以完成您的任务。

public class TextFileProcessor : IDataProcessor<TextFileConfiguration>
{
     private readonly TextFileConfiguration configuration;

     public TextFileProcessor(TextFileConfiguration configuration) => this.configuration = configuration;

     public IEnumerable<TEntity> Read<TEntity>(configuration)
     {
         // Map, based on data from configuration and create object.
         // More...
     }
}

以上可以模拟文本文件的处理器。而在下面,将模拟不同的类型。

public class ExcelFileProcessor : IDataProcessor<ExcelFileConfiguration>
{
    private readonly ExcelFileConfiguration configuration;

    public ExcelFileProcessor(ExcelFileConfiguration configuration) => this.configuration = configuration;

    public IEnumerable<TEntity> Read<TEntity>(configuration)
    {
         // Map based on data from configuration and create object.
         // More...
    }
}

所以这两个类继承了接口,但是实现是完全分开的。通过使用通用的配置对象,它提供了额外的灵活性。我相信这就是您要寻找的概念。