如何在这个C#脚本中使用类来保存和加载文件?

18 浏览
0 Comments

如何在这个C#脚本中使用类来保存和加载文件?

我如何在这个保存和加载文件的C#脚本中使用类?我正在学习C#,我需要为学校做一个使用C#加载和保存文件的项目。我已经学会了如何使用类,但是当我需要在我的脚本中使用时,我真的感到很困惑:

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog open = new OpenFileDialog();
    if (open.ShowDialog() == DialogResult.OK) ;
    {
        StreamReader read = new StreamReader(File.OpenRead(open.FileName));
        textBox1.Text = read.ReadToEnd();
        read.Dispose(); 
    }
}
private void button2_Click(object sender, EventArgs e)
{
    SaveFileDialog save = new SaveFileDialog();
    if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        StreamWriter write = new StreamWriter(File.Create(save.FileName));
        write.Write(textBox1.Text);
        write.Dispose();
    }
}

0
0 Comments

这个问题的出现的原因是作者想要了解如何在C#脚本中使用类来保存和加载文件。解决方法是将代码重构成三层结构,即UI层、业务层和数据层,并且使用接口来抽象数据层,使得业务层与具体实现解耦。

文章内容如下:

在这个问题中,作者想要了解如何在C#脚本中使用类来保存和加载文件。为了解决这个问题,我们可以将代码重构成三层结构,即UI层、业务层和数据层,并且使用接口来抽象数据层,使得业务层与具体实现解耦。

首先,我们需要将用户界面(UI)与业务逻辑分离。根据这个原则,我们可以将程序逻辑分为三个层次:UI层、业务层和数据层。为了帮助你解决问题,我建议你按照以下方式修改你的代码(这个示例使用了三个层次,但如果你使用两个层次也足够好,你的老师不会怀疑你收到了提示):

// 你的用户界面,应该使用业务层类(而不是数据层)
class YourForm
{
    private readonly YourService _myLogicService;
    public YourForm()
    {
        _myLogicService = new YourService(new YourFilePersistor());
    }
    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog open = new OpenFileDialog();
        if (open.ShowDialog() == DialogResult.OK) ;
        {
            textBox1.Text = _myLogicService.Read(open.FileName);
        }
    }
    private void button2_Click(object sender, EventArgs e)
    {
        var toSave = textBox1.Text;
        SaveFileDialog save = new SaveFileDialog();
        if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            _myLogicService.Write(save.FileName, toSave);
        }
    }
}
// 数据层:这个类依赖于文件系统
// (将来可以使用数据库,但是你不需要改变业务服务,只需要实现接口)
public class YourFilePersistor : IPersistor
{
    public string Read(string filePath)
    {
        return System.IO.File.ReadAllText(filePath); // 或者你的代码
    }
    public void Write(string filePath, string fileContent)
    {
        System.IO.File.WriteAllText(filePath, fileContent); // 或者你的代码
    }
}
public interface IPersistor
{
    string Read(string filePath);
    void Write(string filePath, string fileContent);
}
// 业务层:将应用程序逻辑放在你的领域数据上
// 业务类使用接口来抽象数据层,并且在没有依赖于数据库、远程API等组件的情况下工作
public class YourService
{
    private readonly IPersistor _repository;
    public YourService(IPersistor repository)
    {
        _repository = repository;
    }
    public string Read(string filePath)
    {
        var data = _repository.Read(filePath);
        return data;
    }
    public void Write(string filePath, string fileContent)
    {
        var data = fileContent;
        // 在这里你可以做一些逻辑,比如验证你的数据
        if (string.IsNullOrWhiteSpace(fileContent))
        {
            throw new InvalidOperationException("Data is null");
        }
        // ---
        _repository.Write(filePath, data);
    }
}

通过这样的重构,我们将代码按照职责进行了分离,使得每个类只负责一个任务。同时,我们使用接口来抽象数据层,使得业务层与具体实现解耦,提高代码的可维护性和可测试性。这样的架构也符合SOLID原则和3-Tier层级结构的最佳实践。希望这些改进能够帮助你解决问题。

0