在紧凑的框架中以多种形式提供变量

发布于 2024-11-28 19:55:25 字数 816 浏览 1 评论 0 原文

我正在为 Windows CE 5.0 设备编写一个应用程序,该应用程序在第一种表单中要求输入用户名(当应用程序启动时),然后我得到userId 来自数据库表。

之后,出现一个接收 userId 的菜单表单,我必须将 userId 发送到菜单选项的每个构造函数,以便在这些表单中使用它。我想一定有更好的方法来做这样的事情。

示例:

public partial 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();
    }
    ...

我可能应该使用这样的全局变量吗?

public static class UserConfiguration
{
    public static int userId;
}

这不也是一种不好的做法吗?

最后请记住,紧凑框架不支持 app.config 文件

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 partial 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫性小仙女 2024-12-05 19:55:25

就我个人而言,我会投票支持“两者都不是”,但会使用其他一些可用的架构工具。

我非常倾向于拥有一个包含所有用户信息的类(您正在使用的 ID,然后可能还有其他任何信息,例如姓名等)。我会创建一个实例并在提交第一个表单(登录)时填充该信息,并将其保存在 DI 容器中(我使用 具体是这个,但是任何 CF 支持容器 就可以了)。

然后,我要么使用注入自动将该实例推送到任何需要它的类中,要么让消费者根据需要将其从容器中拉出。我使用哪种机制取决于我正在使用哪个容器以及我如何/何时需要信息。

由于您想要的数据来自数据库,我实际上倾向于使用 ORM (我使用 this one )来提取数据,无论如何,这都会自动为您提供包含您想要的用户信息的实体实例。

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.

樱娆 2024-12-05 19:55:25

在我看来,两种方法都很好,在某些情况下,如果更改构造函数签名,某些控件将无法正常工作,或者在某些情况下,如果框架始终调用不带参数的构造函数,则不会调用构造函数。但确实要看具体情况。

我更喜欢方法参数的方式来传递值,但是具有静态字段的外部类也可以正常工作。

PS app.config 无论如何都不是存储运行时特定值的最佳位置,因此在这种情况下 CF 是否支持并不重要;-)

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 ;-)

爱,才寂寞 2024-12-05 19:55:25

如果您使用控制器,它可以保存所需的所有变量。控制器可以具有实例化自身的静态 Instance 属性(请参阅单例对象设计模式)。在开发移动应用程序时,这种情况很常见,因为内存通常是一个限制。其余的方法是公共成员(非静态),因此您可以像这样访问。您可以将它们设置为属性,也可以仅使用公共成员。即使使用移动设备,我们也倾向于不使用属性,因为它只会增加不必要的内容和装箱/拆箱。

在一种形式中,您可以使用:

MainController.Instance.loginID = "me123";

在另一种形式中,您可以使用

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

您还可以添加如下方法:

MainController.Instance.ClearSession();

在内部仅将 loginID 设置为 null。等等。我个人也使用主控制器来显示窗口。因为在移动设备中,我们需要确保我们的资源也得到清理。

MainController.Instance.ShowLoginForm();

MainController 代码作为开始应该如下所示:

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
}

因此,您在 Program.cs 代码中做的第一件事就是初始化您的主控制器

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

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

现在,之后的所有表单都可以通过 MainController 访问它的数据

我个人使用命令并拥有 main控制器根据传入的命令隐藏和显示表单,因此表单中的逻辑尽可能少。这可能适合也可能不适合你正在做的事情。

祝你好运

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文