0

I have a MDI MyMDIWinForm that needs an instance of IGUIErrorHanlder for error handling, At the same time IGUIErrorHanlder depends on the MDI MyMDIWinForm for having a place from where to launch pop-ups and other stuff.

public class MyMDIWinForm 
{
 readonly IGUIErrorHanlder GUIErrorHandler;
 public MyMDIWinForm(IGUIErrorHanlder guiErrorHandler)
 {
 GUIErrorHandler=guiErrorHandler;
 }
 public void DumbMethod()
 {
 try
 {
 //dumb code
 }
 catch(Exception e)
 {
 GUIErrorHandler.OnError(e.Message);
 }
 }
}
public FormGUIErrorHanlder:IGUIErrorHanlder
{
 readonly Form Parent;
 public FormGUIErrorHanlder(Form form)
 {
 //this form should be the MDI form
 Parent=form;
 }
 public OnError(string message)
 {
 Parent.ShowDialog(new PopUpForm(message));
 }
}

I want that MyMDIWinForm has a instance of FormGUIErrorHanlder and that FormGUIErrorHanlder has the same instance of MyMDIWinForm

How do I remove that circular dependency?

Many thanks

asked Jun 19, 2018 at 6:01
3
  • I don't get it. I don't see MyWinForm in FormGUIErrorHanlder. I just see it using some form that could be any form. If there is a circular dependency here it isn't a static one. Maybe pass it some other form? Commented Jun 19, 2018 at 6:13
  • Wait 1 min i edit the question Commented Jun 19, 2018 at 6:14
  • @candied_orange sorry because you take your time to read my question. But i can solve t by creating a factory of IGUIErrorHandler. If you are right i will delete the question Commented Jun 19, 2018 at 6:21

3 Answers 3

1

Can you not just use delegation?

public class MyMDIWinForm 
{
 ...
 public void DumbMethod()
 {
 try
 {
 //dumb code
 }
 catch(Exception e)
 {
 GUIErrorHandler.OnError(this, e.Message);
 }
 }
}
public FormGUIErrorHanlder:IGUIErrorHanlder
{
 ...
 public OnError(Form parentForm, string message)
 {
 parentForm.ShowDialog(new PopUpForm(message));
 }
}
answered Jun 19, 2018 at 9:11
0

The simple solution here is to not have the constructor injection in FormGUIErrorHandler. Instead, have an extra parameter in OnError of type Form and pass in the form from the MyMDIWinForm class.

The key here is that the dependency you were trying to model is actually too tight. The FormGUIErrorHandler class should be able to deal with any Form. You might even want to consider having only one IGUIErrorHandler in your application.

answered Jun 19, 2018 at 7:34
1
  • If i pass a Form type parameter to the IGUIErrorHandler I am in front of a leak implementation Commented Jun 19, 2018 at 10:32
0

I finally solved by injecting a factory:

 public interface IErrorGUIHandlerFactory
 {
 IErrorGUIHandler Create(Form form);
 }
public class MyMDIWinForm 
{
 readonly IErrorGUIHandlerFactory GUIErrorHandlerFactory;
 public MyMDIWinForm(IErrorGUIHandlerFactory guiErrorHandlerFact)
 {
 GUIErrorHandlerFactory=guiErrorHandlerFact;
 }
 public void DumbMethod()
 {
 try
 {
 //dumb code
 }
 catch(Exception e)
 {
 //use the factory passing Form as parameter
 GUIErrorHandlerFactory.Create(this).OnError(e.Message);
 }
 }
}
answered Jun 19, 2018 at 10:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.