-4

How do I put classes in this C# script which saves and loads files. I am currently in process of learning C#. I have to make a project for school with C# which loads and saves files with use of classes. I learned how to work with classes, but I am really getting confused when I have to make use of this in my script:

  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();
        }
    }
  • Why bother? look at `System.File`. Specifically, `ReadAllText` and `WriteAllText` methods. – Zohar Peled Nov 27 '17 at 08:57
  • if you want to save/load classes as a file. you can look into the XMLReader. look into this: [link](https://stackoverflow.com/a/13266536/8579225) – Arno Nov 27 '17 at 08:58
  • You *are* using classes in that code - `OpenFileDialog`, `StreamReader`, `File`. etc are all classes. Further all the code you include here must be inside a class of its own (I'm guessing it might be called `Form1`). But is the requirement that you must create new classes to load a file into a text box/save from a text box into a file? – Dylan Nicholson Nov 27 '17 at 09:02

1 Answers1

1

I think the main point of your exercise is to separate responsabilities.

Every class should accomplish one task only.

According to this principle it is good practice to logically split your programs in three levels (UI, Business and Data layer).

There's a lot to discuss on this so i suggest you to have a look to SOLID principles and 3-Tier level architectures

To help you I suggest you to change your code as follows (this example uses three levels but if you use two levels it is good enough and your teacher won't suspect you received a hint)

   // You User Interface, should use Business level classes (not data)
    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);
            }
        }
    }

    // Data Layer: this class has dependencies from file system 
    // (in future a database can be used but you will not change your business service, just implement the interface)
    public class YourFilePersistor : IPersistor
    {
        public string Read(string filePath)
        {
            return System.IO.File.ReadAllText(filePath); // Or your code
        }

        public void Write(string filePath, string fileContent)
        {
            System.IO.File.WriteAllText(filePath, fileContent); // Or your code
        }
    }

    public interface IPersistor
    {
        string Read(string filePath);

        void Write(string filePath, string fileContent);
    }



    // Service is your "Business Level" for put your application logic on your domain data  
    // Business classes uses intefaces to abstract data layer and work without dependencies from components like database, remote apis and so on...
    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;
            // here you could do some logic i.e. to validate your data  
            if (string.IsNullOrWhiteSpace(fileContent))
            {
                throw new InvalidOperationException("Data is null");
            }
            // ---

            _repository.Write(filePath, data);
        }
    }
Grappachu
  • 1,259
  • 13
  • 21