When is it best to use static functions in ASP.NET?

后端 未结 3 2137
走了就别回头了
走了就别回头了 2021-02-10 01:34

I have been wondering, when to use static functions, and when not to in ASP.NET?

What are the advantages and disadvantages in using them, in various aspects like perform

3条回答
  •  长发绾君心
    2021-02-10 02:09

    There are definitely situations where static is the appropriate solution, as with any application. Any time you have some object that ought to live at the application scope, and not at the request scope, it should be static and you should use static methods to access and manipulate it.

    As an example, here's a snippet of code I wrote recently for an ASP.NET application, which is essentially a serializer cache. Serializers are expensive to create and we can reuse the same one per type for as long as our application lives, so there's no need to waste time in each request thread for them:

    (Note: this has been stripped down to demonstrate the static aspects)

    public class XmlSerializerUtility
    {
        private static Dictionary serializers = new Dictionary();
        private static object sync = new object();
    
        public static T Deserialize(string input)
        {
           XmlSerializer xs = GetSerializer(typeof(T));
            using (StringReader sr = new StringReader(input))
            {
                return (T)xs.Deserialize(sr);
            }
        }
    
        public static XmlDocument Serialize(object input)
        {
            XmlDocument doc = new XmlDocument();
            XmlSerializer xs = GetSerializer(input.GetType());
            using (MemoryStream stream = new MemoryStream())
            {
                xs.Serialize(stream, input);
                stream.Position = 0;
                doc.Load(stream);
            }
            return doc;
        }
    
        private static XmlSerializer GetSerializer(Type type)
        {
            lock (sync)
            {
                XmlSerializer xs = null;
                if (!serializers.ContainsKey(type))
                {
                    xs = new XmlSerializer(type);
                    serializers.Add(type, xs);
                }
                else
                {
                    xs = serializers[type];
                }
                return xs;
            }
        }
    }
    

提交回复
热议问题