How can i make 2 forms in seperate projects access and .Show() each other without circular dependency

114 Views Asked by At

LoginInfo Class

I have a LoginInfo class which gets and sets the Username of the user logging to track the current user in multiple forms or projects.

public class LoginInfo
{
    public static string Username = "NoLoginDetected";
    public static void SetLoginData(string un)
    {
        Username = un;
    }
    public static string GetLoginData()
    {
        return Username;
    }
}

The User entity

public class User:EntityBase
{
    public string Name{ get; set; }
    public string LastName { get; set; }

    public string Username{ get; set; } 
    public string Password{ get; set; } 

    public string UserType { get; set; }
}

Also I have Multiple Projects but we can divide them into 2 Main parts.

1- Login Module

This Project contains the login form which references the other modules and depending on the login authorization it leads to the corresponding module's Main Form. Also sets the username with LoginInfo.SetLoginData(username); if login succeeds.

(ex. The user logging has the UserType = "Designer" -> Leads to the Designer module Main Menu )

2- User-authorization dependent modules/projects.

  • Designer Module

Contains a Main Menu with certain tasks for the designer.

  • Admin Module

Contains a Main Menu with certain tasks for the admin.


And few more modules like those above.

I can access the other modules from the login form with the basic

FormDesigner frm = new FormDesigner(); 
frm.ShowDialog();

But i also want to prevent access to those modules, if by any chance those other projects or their forms were loaded outside the access from the login formm and if thats the case redirect the user to the login form. The issue is, my Login project already references these other projects so i cannot add the login project as a referance to them because of circular dependency.

// - Designer or Admin Main Form

if(LoginInfo.GetLoginData() == "NoLoginDetected")
{ 
MessageBox.Show("No login detected. Redirecting to the login page..."); 
this.Hide();
// ---  HERE IS THE ISSUE  ------
FormLogin frmLogin = new FormLogin(); 
frmLogin.ShowDialog();
// Because i cannot add a referance to the Login Project since it causes Circular Dependency 
this.Close();
 }

Summary

My goal is to be able to login to the other modules by a single login form/project depending on the privileges of the user logging in while also preventing access to the other modules if they get accessed without a login thus leading back to the login form.

1

There are 1 best solutions below

2
Emanuele On

You can try like this:

  1. Add one project (eg. project A) as reference of the other (project B). Then you can call the A window from the B project.
  2. Create a static Window variable in project A. Save the B window into the A variable. Now project A can show projec B's window.

If you need to use some more methods or fields/property, then try with Reflection.