Winforms: access class properties throughout the application

I know that this must be an old, tired question, but I can’t find anything through my trusted friend (aka Google).

I have a .net 3.5 C # winforms application that presents the user with a login form when the application starts. After a successful login, I want to escape to the database, pull some user data and save it (in properties) in a class called AppCurrentUser.cs, access to which can be obtained through all assembly classes - the goal is to fill in some properties with a single read data, instead of calling the database every time. In a web application, I usually used session variables, and I know that the concept of this does not exist in WinForms.

The class structure is similar to the following:

public class AppCurrentUser {

    public AppCurrentUser() { }

    public Guid UserName { get; set; }
    public List<string> Roles { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
} 

, :

"" , , , ... , , ?

, , , ( , ), "reset" . ( , )

? .

!

+3
4

singleton :

public class AppUser
{
    private static _current = null;
    public static AppUser Current
    {
        get { return = _current; }
    }

    public static void Init()
    {
        if (_current == null)
        {
            _current = new AppUser();
            // Load everything from the DB.
            // Name = Dd.GetName();
        }
    }

    public string Name { get; private set; }
}

// App startup.
AppUser.Init();

// Now any form / class / whatever can simply do:
var name = AppUser.Current.Name;

"" . , , lock(), . , Init.

+5

, . - (, ), , , , . , Singleton.

Injection Dependency, , , (, StructureMap) . - , .

+1

IIdentity. , , , , .

Rocky Lhotka CLSA books google winforms.

0

, , - (, ):

public class Sessions
{
    // Variables
    private static string _Username;

    // properties
    public static string Username
    {
        get
        {
            return _Username;
        }
        set
        {
            _Username = value;
        }
    }
}

# ... vb.net...

Session.USername ..

0

Source: https://habr.com/ru/post/1714817/


All Articles