Leon Barkan Leon Barkan - 2 months ago 14
C# Question

.Net WebBrowser architecture

I'm using .Net webBrowser for html parsing

in my program i have one MainForm (form object) and the program class that initializing the MainForm.

in the form i have the webBrowser initialization and when i entering to the web site i need to handle different page's (get data from it/download files and more)

today i have all that cases in the form itself separated by regions as functions :

public partial class MainForm : Form
{
if(DoTypeA())
{
...
}
...
#region TYPEA
private bool DoTypeA()
{
HtmlDocument doc = webBrowser.Document ....
...
}
#endregion TYPEA
...
}


in type A i have redirection to the page and lot of data parsing from it

can i separate that functionality in different class ? for making the code cleaner.

The problem is that the
webBrowser
and all it's events happening in the form class.

What could be the best Design patter / Architecture pattern for that case.

What i Tried:


  • make
    webBrowser
    object as public.

  • Make prop in
    MainForm
    for getting the
    webBrowser
    instance.



The only solution that i see is too pass the webBrowser as parameter for
DoTypeA(webBrowser);
but can i do it without passing the parameter ?

Thank's.

Answer

One of soulution i thought about, is the DI pattern

class Types
{
   IWebDriver webBrowser = null;

   public Types(IWebDriver wb)
   {
      if(wb == null)
         throw new NullReferenceException();

      this.webBrowser = wb;
   }

   public bool DoTypeA()
   {
       HtmlDocument doc = webBrowser.Document //working fine
   }
}

The call from MainForm

Types dt = new Types(webBrowser);
if(dt.GetTypeA())
{
   //done.
}
Comments