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

  • 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? – candied_orange Jun 19 '18 at 6:13
  • Wait 1 min i edit the question – Badulake Jun 19 '18 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 – Badulake Jun 19 '18 at 6:21
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));
     }
}
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.

  • If i pass a Form type parameter to the IGUIErrorHandler I am in front of a leak implementation – Badulake Jun 19 '18 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);
     }
   }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.