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:
- 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
add a comment |
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:
- 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
add a comment |
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:
- 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
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:
- 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
c# design-patterns winforms
asked 8 mins ago
Ramji R
284
284
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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