Question

I have a windows forms application contains of 3 projects in 1 solution.

Hall.Client <-- Winforms

Hall.Admin <-- Winforms

Hall.Processor <-- Class Library

Hall.Client and Hall.Admin need Hall.Processor as a reference. Hall.Processor can't add reference to both Hall.Client and Hall.Admin because of circular dependency. All I need is to get instance of each caller class in Hall.Processor

In Hall.Client I have a class called Canopy

public class Canopy : System.Windows.Form
{
    public void SetProcessName(string name)
    {
        this.txtProcessName.Text = name;
    }
}

In Hall.Admin I have a class called Roof

public class Roof : System.Windows.Form
{
    public void SetProcessName(string name)
    {
        this.txtProcessName.Text = name;
    }
}

I have a method in Hall.Processor inside the Builder class

public class Builder
{
    Form form;
    public Builder(Form form)
    {
        //Here the main problem.
        //if the caller class is Hall.Admin.Roof then this.form should be Hall.Admin.Roof
        //if the caller class is Hall.Client.Canopy then this.form should be Hall.Client.Canopy
    }
    public void SetProcessName()
    {
        //How to call method in caller class directly from this class
        //if(Admin.Roof) then form.SetProcessName("something about admin");
        //if(Client.Canopy) then form.SetProcessName("something about client");
    }
}

I need suggestion how to solve my problem. Is there any design pattern related to my problem?

Was it helpful?

Solution

Decouple classes from each other through the use of interfaces. You can have all interfaces declared inside the Processor library, or better yet in a separate library, shared between Client, Admin and Processor projects. Then you could do the checks like form is IRoof or form is ICanopy.

Note however, in this case nothing would prevent Admin from implementing ICanopy or Client from doing IRoof. If this is really an issue, make the interfaces internal, and control their visibility to other assemblies via [assembly: InternalsVisibleTo("Assembly")] (see "Friend Assemblies").

Further, search the web for "Dependency Injection".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top