How to implement dependency inversion Principle











up vote
0
down vote

favorite












I'm reading on how to implement design patterns. I'm trying to implement the DIP after reading SOLID principles. I'm a beginner in design patterns.



Application scope:



I'm building a Windows form C# project that keeps some information. It will create a bunch of textboxes and comboboxes dynamically, depend upon user input.



In this application, I created multiple class where all the class will have the same behavior. So I thought of creating a abstract class called Component .



All the subclass which is also a form of component will have a is-a relationship with the main class.



On whenever I create a new dynamic component I can just inherit the add_components class so that the new class will implement the add_dynamic_components methods.



public abstract class add_components
{
public abstract void add_dynamic_components(int noOfComponets, int locationX, int locationY, Form1 l, int currentTabIndex);
}

public class addDynamicCptboxComponents : add_components
{
public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f, int currentTabIndex)
{
TextBox txtBox = new TextBox();
f.panel1.Controls.Add(txtBox);
txtBox.Location = new Point(pointX, pointY);
txtBox.Size = new System.Drawing.Size(75, 23);
f.panel1.Controls.Add(txtBox);
txtBox.Name = "Add_txtBox" + getNoOfTxtBox;
txtBox.TabIndex = currentTabIndex * 7 + 2;
}
}

public class addDynamicDateofServiceComponents : add_components
{

public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f, int currentTabIndex)
{
TextBox txtBox = new TextBox();
f.panel1.Controls.Add(txtBox);
txtBox.Location = new Point(pointX, pointY);
txtBox.Size = new System.Drawing.Size(75, 23);
f.panel1.Controls.Add(txtBox);
txtBox.Name = "Add_dos_txtBox" + getNoOfTxtBox;
txtBox.TabIndex = currentTabIndex * 7 + 1;
}
}
public class addDynamicSpellboxComponents : add_components
{

public override void add_dynamic_components(int getNoOfSpellTxtBox, int spPointX, int spPointY, Form1 f, int currentTabIndex)
{
RichTextBox sp = new RichTextBox();
f.panel1.Controls.Add(sp);
sp.Location = new Point(spPointX, spPointY);
sp.Size = new System.Drawing.Size(230, 43);
f.panel1.Controls.Add(sp);
sp.Name = "RichTextbox" + getNoOfSpellTxtBox;
sp.TabIndex = currentTabIndex * 7 + 3;

}
}


On implementation I found that my form class is tightly coupled with the lower class.



Implementation:



    public partial class Form1 : Form
{
addDynamicCptboxComponents addcomponent = new addDynamicCptboxComponents();
addDynamicSpellboxComponents addSpellboxComponent = new addDynamicSpellboxComponents();
addDynamicDateofServiceComponents adddoscomponents = new addDynamicDateofServiceComponents();

public Form1()
{
try
{
InitializeComponent();
}
catch (Exception ex)
{
//Write ex.Message to a file
using (StreamWriter outfile = new StreamWriter(@".error.txt"))
{
outfile.Write(ex.Message.ToString());
}
}
initalizeUserdefinedComponents();

}

public void createDynamiccomponents()
{
int padding = 24;
int gap = 50;
int value;
if (int.TryParse(txtboxvalLines.Text, out value))
{
int txtno = int.Parse(txtboxvalLines.Text);
int pointX = 125;
int pointY = padding ;
int spPointX = 240;
int spPointY = padding ;
int txtboxRecommendedpointX = 640;
int txtboxRecommendedpointY = padding ;
int btnPointX = 1170;
int btnPointY = padding ;
int cmbPointX = 520;
int cmbPointY = padding ;
int pncPointX = 920;
int pncPointY = padding ;
int finalcmbPointX = 780;
int finalcmbPointY = padding ;
int DOSPointX = 35;
int DOSPointY = padding ;

for (int i = 0; i < txtno; i++)
{
customize_panel();

addcomponent.add_dynamic_components(i, pointX, pointY, this, i);
addSpellboxComponent.add_dynamic_components(i, spPointX, spPointY, this, i);

adddoscomponents.add_dynamic_components(i, DOSPointX, DOSPointY, this, i);
panel1.Show();
spPointY += gap;
btnPointY += gap;
cmbPointY += gap;
pointY += gap;
pncPointY += gap;
txtboxRecommendedpointY += gap;
finalcmbPointY += gap;
DOSPointY += gap;
}

btnAdd.Enabled = false;
txtboxvalLines.Enabled = false;
panel1.Focus();
}
else
{
MessageBox.Show("Please provide the count of total Claim lines");
txtboxvalLines.Text = "";
}
}
}


In the above case you can see that the form is tightly coupled with the lower class i.e. addDynamicCptboxComponents, addDynamicSpellboxComponents which is seems to be a violation of DIP as far what i learned in internet.



Now my questions is:




  1. Whether my application violates DIP ? - I believe so. If yes In my case how to implement DIP in the above case. I have never used one.









share


























    up vote
    0
    down vote

    favorite












    I'm reading on how to implement design patterns. I'm trying to implement the DIP after reading SOLID principles. I'm a beginner in design patterns.



    Application scope:



    I'm building a Windows form C# project that keeps some information. It will create a bunch of textboxes and comboboxes dynamically, depend upon user input.



    In this application, I created multiple class where all the class will have the same behavior. So I thought of creating a abstract class called Component .



    All the subclass which is also a form of component will have a is-a relationship with the main class.



    On whenever I create a new dynamic component I can just inherit the add_components class so that the new class will implement the add_dynamic_components methods.



    public abstract class add_components
    {
    public abstract void add_dynamic_components(int noOfComponets, int locationX, int locationY, Form1 l, int currentTabIndex);
    }

    public class addDynamicCptboxComponents : add_components
    {
    public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f, int currentTabIndex)
    {
    TextBox txtBox = new TextBox();
    f.panel1.Controls.Add(txtBox);
    txtBox.Location = new Point(pointX, pointY);
    txtBox.Size = new System.Drawing.Size(75, 23);
    f.panel1.Controls.Add(txtBox);
    txtBox.Name = "Add_txtBox" + getNoOfTxtBox;
    txtBox.TabIndex = currentTabIndex * 7 + 2;
    }
    }

    public class addDynamicDateofServiceComponents : add_components
    {

    public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f, int currentTabIndex)
    {
    TextBox txtBox = new TextBox();
    f.panel1.Controls.Add(txtBox);
    txtBox.Location = new Point(pointX, pointY);
    txtBox.Size = new System.Drawing.Size(75, 23);
    f.panel1.Controls.Add(txtBox);
    txtBox.Name = "Add_dos_txtBox" + getNoOfTxtBox;
    txtBox.TabIndex = currentTabIndex * 7 + 1;
    }
    }
    public class addDynamicSpellboxComponents : add_components
    {

    public override void add_dynamic_components(int getNoOfSpellTxtBox, int spPointX, int spPointY, Form1 f, int currentTabIndex)
    {
    RichTextBox sp = new RichTextBox();
    f.panel1.Controls.Add(sp);
    sp.Location = new Point(spPointX, spPointY);
    sp.Size = new System.Drawing.Size(230, 43);
    f.panel1.Controls.Add(sp);
    sp.Name = "RichTextbox" + getNoOfSpellTxtBox;
    sp.TabIndex = currentTabIndex * 7 + 3;

    }
    }


    On implementation I found that my form class is tightly coupled with the lower class.



    Implementation:



        public partial class Form1 : Form
    {
    addDynamicCptboxComponents addcomponent = new addDynamicCptboxComponents();
    addDynamicSpellboxComponents addSpellboxComponent = new addDynamicSpellboxComponents();
    addDynamicDateofServiceComponents adddoscomponents = new addDynamicDateofServiceComponents();

    public Form1()
    {
    try
    {
    InitializeComponent();
    }
    catch (Exception ex)
    {
    //Write ex.Message to a file
    using (StreamWriter outfile = new StreamWriter(@".error.txt"))
    {
    outfile.Write(ex.Message.ToString());
    }
    }
    initalizeUserdefinedComponents();

    }

    public void createDynamiccomponents()
    {
    int padding = 24;
    int gap = 50;
    int value;
    if (int.TryParse(txtboxvalLines.Text, out value))
    {
    int txtno = int.Parse(txtboxvalLines.Text);
    int pointX = 125;
    int pointY = padding ;
    int spPointX = 240;
    int spPointY = padding ;
    int txtboxRecommendedpointX = 640;
    int txtboxRecommendedpointY = padding ;
    int btnPointX = 1170;
    int btnPointY = padding ;
    int cmbPointX = 520;
    int cmbPointY = padding ;
    int pncPointX = 920;
    int pncPointY = padding ;
    int finalcmbPointX = 780;
    int finalcmbPointY = padding ;
    int DOSPointX = 35;
    int DOSPointY = padding ;

    for (int i = 0; i < txtno; i++)
    {
    customize_panel();

    addcomponent.add_dynamic_components(i, pointX, pointY, this, i);
    addSpellboxComponent.add_dynamic_components(i, spPointX, spPointY, this, i);

    adddoscomponents.add_dynamic_components(i, DOSPointX, DOSPointY, this, i);
    panel1.Show();
    spPointY += gap;
    btnPointY += gap;
    cmbPointY += gap;
    pointY += gap;
    pncPointY += gap;
    txtboxRecommendedpointY += gap;
    finalcmbPointY += gap;
    DOSPointY += gap;
    }

    btnAdd.Enabled = false;
    txtboxvalLines.Enabled = false;
    panel1.Focus();
    }
    else
    {
    MessageBox.Show("Please provide the count of total Claim lines");
    txtboxvalLines.Text = "";
    }
    }
    }


    In the above case you can see that the form is tightly coupled with the lower class i.e. addDynamicCptboxComponents, addDynamicSpellboxComponents which is seems to be a violation of DIP as far what i learned in internet.



    Now my questions is:




    1. Whether my application violates DIP ? - I believe so. If yes In my case how to implement DIP in the above case. I have never used one.









    share
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm reading on how to implement design patterns. I'm trying to implement the DIP after reading SOLID principles. I'm a beginner in design patterns.



      Application scope:



      I'm building a Windows form C# project that keeps some information. It will create a bunch of textboxes and comboboxes dynamically, depend upon user input.



      In this application, I created multiple class where all the class will have the same behavior. So I thought of creating a abstract class called Component .



      All the subclass which is also a form of component will have a is-a relationship with the main class.



      On whenever I create a new dynamic component I can just inherit the add_components class so that the new class will implement the add_dynamic_components methods.



      public abstract class add_components
      {
      public abstract void add_dynamic_components(int noOfComponets, int locationX, int locationY, Form1 l, int currentTabIndex);
      }

      public class addDynamicCptboxComponents : add_components
      {
      public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f, int currentTabIndex)
      {
      TextBox txtBox = new TextBox();
      f.panel1.Controls.Add(txtBox);
      txtBox.Location = new Point(pointX, pointY);
      txtBox.Size = new System.Drawing.Size(75, 23);
      f.panel1.Controls.Add(txtBox);
      txtBox.Name = "Add_txtBox" + getNoOfTxtBox;
      txtBox.TabIndex = currentTabIndex * 7 + 2;
      }
      }

      public class addDynamicDateofServiceComponents : add_components
      {

      public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f, int currentTabIndex)
      {
      TextBox txtBox = new TextBox();
      f.panel1.Controls.Add(txtBox);
      txtBox.Location = new Point(pointX, pointY);
      txtBox.Size = new System.Drawing.Size(75, 23);
      f.panel1.Controls.Add(txtBox);
      txtBox.Name = "Add_dos_txtBox" + getNoOfTxtBox;
      txtBox.TabIndex = currentTabIndex * 7 + 1;
      }
      }
      public class addDynamicSpellboxComponents : add_components
      {

      public override void add_dynamic_components(int getNoOfSpellTxtBox, int spPointX, int spPointY, Form1 f, int currentTabIndex)
      {
      RichTextBox sp = new RichTextBox();
      f.panel1.Controls.Add(sp);
      sp.Location = new Point(spPointX, spPointY);
      sp.Size = new System.Drawing.Size(230, 43);
      f.panel1.Controls.Add(sp);
      sp.Name = "RichTextbox" + getNoOfSpellTxtBox;
      sp.TabIndex = currentTabIndex * 7 + 3;

      }
      }


      On implementation I found that my form class is tightly coupled with the lower class.



      Implementation:



          public partial class Form1 : Form
      {
      addDynamicCptboxComponents addcomponent = new addDynamicCptboxComponents();
      addDynamicSpellboxComponents addSpellboxComponent = new addDynamicSpellboxComponents();
      addDynamicDateofServiceComponents adddoscomponents = new addDynamicDateofServiceComponents();

      public Form1()
      {
      try
      {
      InitializeComponent();
      }
      catch (Exception ex)
      {
      //Write ex.Message to a file
      using (StreamWriter outfile = new StreamWriter(@".error.txt"))
      {
      outfile.Write(ex.Message.ToString());
      }
      }
      initalizeUserdefinedComponents();

      }

      public void createDynamiccomponents()
      {
      int padding = 24;
      int gap = 50;
      int value;
      if (int.TryParse(txtboxvalLines.Text, out value))
      {
      int txtno = int.Parse(txtboxvalLines.Text);
      int pointX = 125;
      int pointY = padding ;
      int spPointX = 240;
      int spPointY = padding ;
      int txtboxRecommendedpointX = 640;
      int txtboxRecommendedpointY = padding ;
      int btnPointX = 1170;
      int btnPointY = padding ;
      int cmbPointX = 520;
      int cmbPointY = padding ;
      int pncPointX = 920;
      int pncPointY = padding ;
      int finalcmbPointX = 780;
      int finalcmbPointY = padding ;
      int DOSPointX = 35;
      int DOSPointY = padding ;

      for (int i = 0; i < txtno; i++)
      {
      customize_panel();

      addcomponent.add_dynamic_components(i, pointX, pointY, this, i);
      addSpellboxComponent.add_dynamic_components(i, spPointX, spPointY, this, i);

      adddoscomponents.add_dynamic_components(i, DOSPointX, DOSPointY, this, i);
      panel1.Show();
      spPointY += gap;
      btnPointY += gap;
      cmbPointY += gap;
      pointY += gap;
      pncPointY += gap;
      txtboxRecommendedpointY += gap;
      finalcmbPointY += gap;
      DOSPointY += gap;
      }

      btnAdd.Enabled = false;
      txtboxvalLines.Enabled = false;
      panel1.Focus();
      }
      else
      {
      MessageBox.Show("Please provide the count of total Claim lines");
      txtboxvalLines.Text = "";
      }
      }
      }


      In the above case you can see that the form is tightly coupled with the lower class i.e. addDynamicCptboxComponents, addDynamicSpellboxComponents which is seems to be a violation of DIP as far what i learned in internet.



      Now my questions is:




      1. Whether my application violates DIP ? - I believe so. If yes In my case how to implement DIP in the above case. I have never used one.









      share













      I'm reading on how to implement design patterns. I'm trying to implement the DIP after reading SOLID principles. I'm a beginner in design patterns.



      Application scope:



      I'm building a Windows form C# project that keeps some information. It will create a bunch of textboxes and comboboxes dynamically, depend upon user input.



      In this application, I created multiple class where all the class will have the same behavior. So I thought of creating a abstract class called Component .



      All the subclass which is also a form of component will have a is-a relationship with the main class.



      On whenever I create a new dynamic component I can just inherit the add_components class so that the new class will implement the add_dynamic_components methods.



      public abstract class add_components
      {
      public abstract void add_dynamic_components(int noOfComponets, int locationX, int locationY, Form1 l, int currentTabIndex);
      }

      public class addDynamicCptboxComponents : add_components
      {
      public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f, int currentTabIndex)
      {
      TextBox txtBox = new TextBox();
      f.panel1.Controls.Add(txtBox);
      txtBox.Location = new Point(pointX, pointY);
      txtBox.Size = new System.Drawing.Size(75, 23);
      f.panel1.Controls.Add(txtBox);
      txtBox.Name = "Add_txtBox" + getNoOfTxtBox;
      txtBox.TabIndex = currentTabIndex * 7 + 2;
      }
      }

      public class addDynamicDateofServiceComponents : add_components
      {

      public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f, int currentTabIndex)
      {
      TextBox txtBox = new TextBox();
      f.panel1.Controls.Add(txtBox);
      txtBox.Location = new Point(pointX, pointY);
      txtBox.Size = new System.Drawing.Size(75, 23);
      f.panel1.Controls.Add(txtBox);
      txtBox.Name = "Add_dos_txtBox" + getNoOfTxtBox;
      txtBox.TabIndex = currentTabIndex * 7 + 1;
      }
      }
      public class addDynamicSpellboxComponents : add_components
      {

      public override void add_dynamic_components(int getNoOfSpellTxtBox, int spPointX, int spPointY, Form1 f, int currentTabIndex)
      {
      RichTextBox sp = new RichTextBox();
      f.panel1.Controls.Add(sp);
      sp.Location = new Point(spPointX, spPointY);
      sp.Size = new System.Drawing.Size(230, 43);
      f.panel1.Controls.Add(sp);
      sp.Name = "RichTextbox" + getNoOfSpellTxtBox;
      sp.TabIndex = currentTabIndex * 7 + 3;

      }
      }


      On implementation I found that my form class is tightly coupled with the lower class.



      Implementation:



          public partial class Form1 : Form
      {
      addDynamicCptboxComponents addcomponent = new addDynamicCptboxComponents();
      addDynamicSpellboxComponents addSpellboxComponent = new addDynamicSpellboxComponents();
      addDynamicDateofServiceComponents adddoscomponents = new addDynamicDateofServiceComponents();

      public Form1()
      {
      try
      {
      InitializeComponent();
      }
      catch (Exception ex)
      {
      //Write ex.Message to a file
      using (StreamWriter outfile = new StreamWriter(@".error.txt"))
      {
      outfile.Write(ex.Message.ToString());
      }
      }
      initalizeUserdefinedComponents();

      }

      public void createDynamiccomponents()
      {
      int padding = 24;
      int gap = 50;
      int value;
      if (int.TryParse(txtboxvalLines.Text, out value))
      {
      int txtno = int.Parse(txtboxvalLines.Text);
      int pointX = 125;
      int pointY = padding ;
      int spPointX = 240;
      int spPointY = padding ;
      int txtboxRecommendedpointX = 640;
      int txtboxRecommendedpointY = padding ;
      int btnPointX = 1170;
      int btnPointY = padding ;
      int cmbPointX = 520;
      int cmbPointY = padding ;
      int pncPointX = 920;
      int pncPointY = padding ;
      int finalcmbPointX = 780;
      int finalcmbPointY = padding ;
      int DOSPointX = 35;
      int DOSPointY = padding ;

      for (int i = 0; i < txtno; i++)
      {
      customize_panel();

      addcomponent.add_dynamic_components(i, pointX, pointY, this, i);
      addSpellboxComponent.add_dynamic_components(i, spPointX, spPointY, this, i);

      adddoscomponents.add_dynamic_components(i, DOSPointX, DOSPointY, this, i);
      panel1.Show();
      spPointY += gap;
      btnPointY += gap;
      cmbPointY += gap;
      pointY += gap;
      pncPointY += gap;
      txtboxRecommendedpointY += gap;
      finalcmbPointY += gap;
      DOSPointY += gap;
      }

      btnAdd.Enabled = false;
      txtboxvalLines.Enabled = false;
      panel1.Focus();
      }
      else
      {
      MessageBox.Show("Please provide the count of total Claim lines");
      txtboxvalLines.Text = "";
      }
      }
      }


      In the above case you can see that the form is tightly coupled with the lower class i.e. addDynamicCptboxComponents, addDynamicSpellboxComponents which is seems to be a violation of DIP as far what i learned in internet.



      Now my questions is:




      1. Whether my application violates DIP ? - I believe so. If yes In my case how to implement DIP in the above case. I have never used one.







      c# design-patterns winforms





      share












      share










      share



      share










      asked 8 mins ago









      Ramji R

      284




      284



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209416%2fhow-to-implement-dependency-inversion-principle%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209416%2fhow-to-implement-dependency-inversion-principle%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Create new schema in PostgreSQL using DBeaver

          Deepest pit of an array with Javascript: test on Codility

          Costa Masnaga