如何定义“全局"?变数 [英] How define "Global" variables

查看:82
本文介绍了如何定义“全局"?变数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在开发MVC3应用程序;

我需要一系列变量,其中包含有关特定用户(和用户操作)的必要信息,几乎可以由我的应用程序的任何部分访问.

我想出了创建必要的静态类的解决方案,其中的属性引用了特定的会话变量,这样,一旦设置了它们,它们(理论上)将保持不变,直到被更改/删除为止.例如:

Hi everyone,

I''m developing an MVC3 application;

I need a series of variables which contains necessary info regarding the specific user (and user operations) to be accessed by almost any part of my application.

I came up with the solution of creating the necessary Static Classes, with the properties referring to specific Session Variables, so that once they are set, they''ll (theorically) stay the same until changed/removed; for example:

public static class TestClass
    {

        public static string TestProperty {
            get{
                return HttpContext.Current.Session["MyApplication_TestProperty"].ToString();
            }
            set{
                HttpContext.Current.Session["MyApplication_TestProperty"] = value;
            }
        }

    }



这是一个好的(或至少是体面的)解决方案,还是直接将其发布在耻辱大厅中并寻找另一种方法更好?

有什么建议或更好的解决方案吗?

预先谢谢您,
Alberto



Is this a good (or at least decent) solution, or is it better to directly post it in the Hall of Shame and find another way??

Any suggestion or better solution?

Thank you in advance,
Alberto

推荐答案

这个想法没有错,但是示例中有一个bug(请参阅下面的修复程序).另一个问题是使用立即常量并将其重复两次.您将如何支持它?除了一些琐碎的常量(例如0、1或null)外,所有常量都应该是显式的.但是可以这样修复:

There is nothing wrong with the idea, but there is a bug in the sample (see the fix below). Another problem is using immediate constant and repeating it twice. How are your going to support it? All constants should be explicit, except some trivial ones like 0, 1, or null. But it could be fixed like this:

public static class TestClass
    {
        const string SessionId = "MyApplication_TestProperty"; //a little better

        public static string TestProperty {
            get{
                return HttpContext.Current.Session[SessionId].ToString();
            }
            set{
                HttpContext.Current.Session[SessionId] = value; //bug fixed.
            }
        }

    }



它可以工作,但不灵活.更通用和通用的方法是使用单人模式,请参见:
http://en.wikipedia.org/wiki/Singleton_pattern [ http://en.wikipedia.org/wiki/Design_pattern_(computer_science) [ http://csharpindepth.com/Articles/General/Singleton.aspx [



It will work but is not flexible. More universal and general approach is using the singleton pattern, see:
http://en.wikipedia.org/wiki/Singleton_pattern[^],
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)[^].

This is a good article with implementation samples in C#. I suggest you learn how to use the pattern:
http://csharpindepth.com/Articles/General/Singleton.aspx[^].

Good luck.

—SA


如果它可以满足您的需求,那么为什么不呢?

要记住的一件事是HttpContext.Current.Session仅在MVC产生的主线程中对您可用. IE.如果您产生另一个线程来执行某些工作,则这些属性将不可用.

像这样使用静态类也意味着您将无法轻松地对依赖于这些属性之一的调用的任何单元进行单元测试.

但是,如果您不打算使用其他线程来调用这些属性,并且不想编写单元测试,那为什么不呢?
If it works for what you need it for then why not.

One thing to bear in mind is that the HttpContext.Current.Session will only be availble to you in the main thread spawned by MVC. ie. if you spawn another thread to do some work these properties will not be available to you.

Using Static classes like this also mean that you will not be able to easily unit test anything that depends on a call to one of these properties.

But if you''re not going to be using additional threads that make calls to these properties and you don''t want to write unit tests, why not.


这篇关于如何定义“全局"?变数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆