开发者

Make available a variable in multiple forms in compact framework

开发者 https://www.devze.com 2023-03-27 02:12 出处:网络
I\'m doing an application for a Windows CE 5.0 device that asks for the username in the first form (when the application is launched), and then I get the userId from a database table.

I'm doing an application for a Windows CE 5.0 device that asks for the username in the first form (when the application is launched), and then I get the userId from a database table.

After that a menu form appears which receives the userId, and I have to send to each constructor of the menu options the userId in order to use it in those forms. I assume there must be a better way to do something like this.

Example:

public part开发者_开发技巧ial class Menu : Form
{
    int userId;
    public Menu(int userId)
    {
        InitializeComponent();
        this.userId = userId;
    }

    private void buttonDelivery_Click(object sender, EventArgs e)
    {
        Delivery delivery = new Delivery(userId);
        delivery.Show();
        this.Hide();
    }
    ...

May be I should use a global variable like this?

public static class UserConfiguration
{
    public static int userId;
}

Isn't that also bad practice?

Finally bear in mind that compact framework doesn't support app.config files


Personally I'd vote for "neither", but would instead use some other architectural tools available.

I'd be highly inclined to have a class that incorporates all user info (the ID you're using and then maybe anything else, like name, etc). I'd create an instance and populate that info when the first Form (login) is submitted and I'd keep it in a DI container (I use this one specifically, but any CF-supporting container would work).

I'd then either use injection to either automatically push that instance into any class that needs it, or have the consumer pull it from the container as needed. Which mechanism I use would depend on which container I'm using and exactly how/when I need the info.

Since the data you're after is coming from a database, I'd actually be inclined to use an ORM (I use this one) to pull the data, which would give you the entity instance containing the user info you're after automatically anyway.


in my opinion both ways are good, in some cases some controls do not work properly if you change the constructor signature or in some cases your constructor would not be called if the framework always calls the one with no parameters. But really depends on the specific case.

I like more the method parameters way to pass the values, but the external class with static field would also work fine.

P.S. app.config is not the best place anyway to store runtime specific values so doesn't matter if supported or not by CF in this case ;-)


If you use a controller it can hold all the variables needed. The controller can have a static Instance property that instantiates itself (see Singleton object design pattern). When developing Mobile applications this is very common as memory is often a constraint. The rest of the methods are public members (not static) so you would access like this. You can either make them properties or just use the public member. Even with mobile we tend to not use properties as it just adds unecessary fluff and boxing/unboxing.

In one form you can use:

MainController.Instance.loginID = "me123";

on another you can use

MessageBox.Show("my loginID is: " + MainController.Instance.loginID);

You can also add methods like:

MainController.Instance.ClearSession();

Which internally just sets loginID to null. etc. Personally I use the main controller to show windows as well. Because in mobile we need to make sure our resources are cleaned up as well.

MainController.Instance.ShowLoginForm();

the MainController code as a start should look something like this:

public class MainController : IDisposable {
//all forms we are controlling
LoginForm _loginForm = null;

//all public members
public string loginID = null;


#region Singleton Instance stuff
private static MainController me = null;
private void MainController() { }
public static Instance {
    get {
        if(me == null) {
            me = new MainController();
        }
        return me;
    }
}
#endregion


//all public methods
public void Init(someargshere) {
    //TODO some init like load config files, etc.
}
public void Dispose() {
    //TODO cleanup
}
public void ClearSession() {
    loginID = "";
}

public void ShowLoginForm() {
    if(loginForm!=null) {
        loginForm.Dispose();
        loginForm == null;
    }

    loginForm = new LoginForm();
    loginForm.Show();
    loginForm.BringToFront();
}

//etc
}

So the very first thing you do in the Program.cs code is init your main controller

main(string[] args) {
    //start a controller
    MainController.Instance.Init(passomeargs if needed);

    //now fire off our main form
    Application.Run(new MainForm());
}

Now all forms there after can access it's data through the MainController

Personally I use commands and have the main controller hide and show forms based on the commands passed in so there is as little logic in the forms as possible. This may or may not lend well to what you are doing.

Good luck

0

精彩评论

暂无评论...
验证码 换一张
取 消