Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. [Solved] Opinions on two code layout alternatives?

[Solved] Opinions on two code layout alternatives?

Scheduled Pinned Locked Moved Design and Architecture
discussioncssquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RobertSF
    wrote on last edited by
    #1

    I have an application that reads a directory's files, displays them in a grid, and offers a toolbar with buttons that invoke functions that apply to all the files in the grid. In the following code, reduced to its essentials, when the user clicks the Trim button in the toolbar, toolTrim_Click executes, opening the form that collects the options for the Trim operation, and then invoking the method that trims the pages from the list of files. That method then invokes for each file the method that trims the pages from that particular file. The async part is just to keep the window draggable.

    //Main Form:
    private async void toolTrim_Click(object sender, EventArgs e)
    {
    var form = new TrimOptions();
    DialogResult result = form.ShowDialog();
    if (result == DialogResult.OK)
    {
    await Task.Run(() => filesList.TrimPages(form.trimFirst, form.trimLast, form.ifBlank));
    }
    form.Dispose();
    }

    //filesList Class:
    public void TrimPages(bool trimFirst, bool trimLast, bool ifBlank)
    {
    for (int entry = 0; entry < TotalPDFs; entry++)
    {
    FileEntry thisFile = list[entry];
    thisFile.Trim(trimFirst, trimLast, ifBlank);
    }
    }

    //thisFile Class:
    public void Trim(bool front, bool back, bool ifBlank)
    {
    // code
    }

    What would be the pros and cons of refactoring the above code like this? The difference is that we move opening the options form to the class that manages the list of files instead of keeping it in the main form class.

    //Main Form:
    private async void toolTrim_Click(object sender, EventArgs e)
    {
    await Task.Run(() => filesList.TrimPages());
    }

    //filesList Class:
    public void TrimPages(bool trimFirst, bool trimLast, bool ifBlank)
    {
    var form = new TrimOptions();
    DialogResult result = form.ShowDialog();
    if (result == DialogResult.OK)
    {
    for (int entry = 0; entry < TotalPDFs; entry++)
    {
    PDFEntry thisFile = list[entry];
    }
    }
    }

    //thisFile Class:
    public void Trim(bool front, bool back, bool ifBlank)
    {
    // code
    }

    It seems to me that the class that manages the list of files should be the class that collects the various operation options, but I don't know which is the best practice, or what the implications are of doing it the second way.

    L E 2 Replies Last reply
    0
    • R RobertSF

      I have an application that reads a directory's files, displays them in a grid, and offers a toolbar with buttons that invoke functions that apply to all the files in the grid. In the following code, reduced to its essentials, when the user clicks the Trim button in the toolbar, toolTrim_Click executes, opening the form that collects the options for the Trim operation, and then invoking the method that trims the pages from the list of files. That method then invokes for each file the method that trims the pages from that particular file. The async part is just to keep the window draggable.

      //Main Form:
      private async void toolTrim_Click(object sender, EventArgs e)
      {
      var form = new TrimOptions();
      DialogResult result = form.ShowDialog();
      if (result == DialogResult.OK)
      {
      await Task.Run(() => filesList.TrimPages(form.trimFirst, form.trimLast, form.ifBlank));
      }
      form.Dispose();
      }

      //filesList Class:
      public void TrimPages(bool trimFirst, bool trimLast, bool ifBlank)
      {
      for (int entry = 0; entry < TotalPDFs; entry++)
      {
      FileEntry thisFile = list[entry];
      thisFile.Trim(trimFirst, trimLast, ifBlank);
      }
      }

      //thisFile Class:
      public void Trim(bool front, bool back, bool ifBlank)
      {
      // code
      }

      What would be the pros and cons of refactoring the above code like this? The difference is that we move opening the options form to the class that manages the list of files instead of keeping it in the main form class.

      //Main Form:
      private async void toolTrim_Click(object sender, EventArgs e)
      {
      await Task.Run(() => filesList.TrimPages());
      }

      //filesList Class:
      public void TrimPages(bool trimFirst, bool trimLast, bool ifBlank)
      {
      var form = new TrimOptions();
      DialogResult result = form.ShowDialog();
      if (result == DialogResult.OK)
      {
      for (int entry = 0; entry < TotalPDFs; entry++)
      {
      PDFEntry thisFile = list[entry];
      }
      }
      }

      //thisFile Class:
      public void Trim(bool front, bool back, bool ifBlank)
      {
      // code
      }

      It seems to me that the class that manages the list of files should be the class that collects the various operation options, but I don't know which is the best practice, or what the implications are of doing it the second way.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I see no reason to use a modal dialog. You could use a regular form and encapsulate ALL the related logic in the form. Alternatively, create a static class with a single entry point that wraps the logic and dialog. Component-based design.

      It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

      R 1 Reply Last reply
      0
      • L Lost User

        I see no reason to use a modal dialog. You could use a regular form and encapsulate ALL the related logic in the form. Alternatively, create a static class with a single entry point that wraps the logic and dialog. Component-based design.

        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

        R Offline
        R Offline
        RobertSF
        wrote on last edited by
        #3

        Well, I'm using modal dialogs for the various forms that collect user input because, while any of those forms is open, the user should not be able to focus on any other form. It's like Word's Font dialog. I suppose I could put all related code in each form. Then the Rename Files form would have its own code that renames files, and the Trim Pages code would have its own code that trims pages from the files. But I understood from my Google "research" that forms should just be data input and validation devices. I thought it was better practice to have a class that deals with the collection (files, invoices, parts, whatever) that contains the logic for managing the collection, and separately a class for the individual item within the collect that would contain the logic for operating on the item.

        L 1 Reply Last reply
        0
        • R RobertSF

          Well, I'm using modal dialogs for the various forms that collect user input because, while any of those forms is open, the user should not be able to focus on any other form. It's like Word's Font dialog. I suppose I could put all related code in each form. Then the Rename Files form would have its own code that renames files, and the Trim Pages code would have its own code that trims pages from the files. But I understood from my Google "research" that forms should just be data input and validation devices. I thought it was better practice to have a class that deals with the collection (files, invoices, parts, whatever) that contains the logic for managing the collection, and separately a class for the individual item within the collect that would contain the logic for operating on the item.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          A "form" (i.e. Window / view) is an interface for the user to your app; it is not limited to "data input and validation". Parts of that interface include the screen, keyboard, mouse, joystick, "touch", etc. And the app you described was not a "data entry application"; it was a utility for manipulating files; which may or may not need to be modal.

          It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

          R 1 Reply Last reply
          0
          • L Lost User

            A "form" (i.e. Window / view) is an interface for the user to your app; it is not limited to "data input and validation". Parts of that interface include the screen, keyboard, mouse, joystick, "touch", etc. And the app you described was not a "data entry application"; it was a utility for manipulating files; which may or may not need to be modal.

            It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

            R Offline
            R Offline
            RobertSF
            wrote on last edited by
            #5

            Thanks for the input. You had suggested putting all the code in the form, but that violates the principle of separation of concerns. I know the application I described is not a "data entry application." I didn't describe it as such. The question was really about code organization, not about form modality or the definition of user interface. Please let me know if you have any answer to the question posed as is. I confess I have not read the Analects, but I love and know by heart its first line. The Master said: To study and at due times practice what one has studied, is this not a pleasure?

            1 Reply Last reply
            0
            • R RobertSF

              I have an application that reads a directory's files, displays them in a grid, and offers a toolbar with buttons that invoke functions that apply to all the files in the grid. In the following code, reduced to its essentials, when the user clicks the Trim button in the toolbar, toolTrim_Click executes, opening the form that collects the options for the Trim operation, and then invoking the method that trims the pages from the list of files. That method then invokes for each file the method that trims the pages from that particular file. The async part is just to keep the window draggable.

              //Main Form:
              private async void toolTrim_Click(object sender, EventArgs e)
              {
              var form = new TrimOptions();
              DialogResult result = form.ShowDialog();
              if (result == DialogResult.OK)
              {
              await Task.Run(() => filesList.TrimPages(form.trimFirst, form.trimLast, form.ifBlank));
              }
              form.Dispose();
              }

              //filesList Class:
              public void TrimPages(bool trimFirst, bool trimLast, bool ifBlank)
              {
              for (int entry = 0; entry < TotalPDFs; entry++)
              {
              FileEntry thisFile = list[entry];
              thisFile.Trim(trimFirst, trimLast, ifBlank);
              }
              }

              //thisFile Class:
              public void Trim(bool front, bool back, bool ifBlank)
              {
              // code
              }

              What would be the pros and cons of refactoring the above code like this? The difference is that we move opening the options form to the class that manages the list of files instead of keeping it in the main form class.

              //Main Form:
              private async void toolTrim_Click(object sender, EventArgs e)
              {
              await Task.Run(() => filesList.TrimPages());
              }

              //filesList Class:
              public void TrimPages(bool trimFirst, bool trimLast, bool ifBlank)
              {
              var form = new TrimOptions();
              DialogResult result = form.ShowDialog();
              if (result == DialogResult.OK)
              {
              for (int entry = 0; entry < TotalPDFs; entry++)
              {
              PDFEntry thisFile = list[entry];
              }
              }
              }

              //thisFile Class:
              public void Trim(bool front, bool back, bool ifBlank)
              {
              // code
              }

              It seems to me that the class that manages the list of files should be the class that collects the various operation options, but I don't know which is the best practice, or what the implications are of doing it the second way.

              E Offline
              E Offline
              englebart
              wrote on last edited by
              #6

              Keep the GUI classes separate. I prefer your original code over the proposed change. What if you want to build a console version of this app where you pass a wild card and an operation? Keeping the GUI separate would allow this to work without hassle. 2021*.pdf -trim first last blank

              R 1 Reply Last reply
              0
              • E englebart

                Keep the GUI classes separate. I prefer your original code over the proposed change. What if you want to build a console version of this app where you pass a wild card and an operation? Keeping the GUI separate would allow this to work without hassle. 2021*.pdf -trim first last blank

                R Offline
                R Offline
                RobertSF
                wrote on last edited by
                #7

                Thanks for the sanity check! :) Yes, keeping the GUI classes separate makes sense.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups