2006-09-23

HttpContext Items Strongly Typed

I have just refactored my nHibernate session store in HttpContext.Items to use this as the base class. This fits perfectly with the spring wiring and means that i can also use it in other projects, for other items that i want strongly typed access to the object. I forgot the dispose method so i was having bad memory leaks. See also the default(T) keyword, interesting.

    public abstract class AbstractObjectCacher<T> {
        #region IoC
        private IObjectCache<T> cache;
        public IObjectCache<T> Cache {
            set { cache = value; }
            get { return cache; }
        }
        #endregion
        public T Get() {
            return cache.Get();
        }
        public void Set(T obj) {
            cache.Set(obj);
        }
        public void Dispose() {
            T obj = Get();
            if(obj != null) {
                Set(default(T));
                if(obj is IDisposable) {
                    ((IDisposable)obj).Dispose();
                }
                obj = default(T);
            }
            cache.Clear();
        }
    }

So for instance, the thread store i use for unit testing will look like this:

    public class ThreadObjectCache<T> : IObjectCache<T> {
        #region IoC
        private string key;
        public string Key {
            set {
                key = value;
                ThreadStore.Initialise(key);
            }
            get { return key; }
        }
        #endregion
        public T Get() {
            return (T)ThreadStore.Get(key);
        }
        public void Set(T obj) {
            ThreadStore.Set(key, obj);
        }
        public void Clear() {
            ThreadStore.Set(key, null);
        }
        public void Dispose() {
            ThreadStore.Dispose(key);
        }
    }

And the HttpContext store will look like this:

    public class HttpContextCache<T> : IObjectCache<T> {
        #region IoC
        private string key;
        public string Key {
            set { key = value; }
            get { return key; }
        }
        #endregion
public T Get() {
            return (T)HttpContext.Current.Items[key];
        }
        public void Set(T obj) {
            HttpContext.Current.Items[key] = obj;
        }
        public void Clear() {
            HttpContext.Current.Items[key] = null;
        }
        public void Dispose() {
            HttpContext.Current.Items.Remove(key);
        }
    }

5 comments:

oakleyses said...

ray ban sunglasses, tory burch outlet, nike roshe, oakley sunglasses, ray ban sunglasses, christian louboutin, ray ban sunglasses, louis vuitton outlet, christian louboutin uk, christian louboutin outlet, louis vuitton, nike outlet, cheap oakley sunglasses, oakley sunglasses, longchamp outlet, polo ralph lauren, longchamp outlet, jordan pas cher, polo ralph lauren outlet online, burberry pas cher, longchamp pas cher, nike free, prada outlet, nike free run, ugg boots, tiffany jewelry, ugg boots, air max, polo outlet, oakley sunglasses, christian louboutin shoes, longchamp outlet, uggs on sale, louis vuitton outlet, tiffany and co, louis vuitton outlet, kate spade outlet, michael kors pas cher, replica watches, louboutin pas cher, sac longchamp pas cher, gucci handbags, louis vuitton, oakley sunglasses wholesale, chanel handbags, nike air max, replica watches, prada handbags

oakleyses said...

michael kors outlet online, nike blazer pas cher, timberland pas cher, replica handbags, michael kors, sac hermes, coach outlet, ralph lauren uk, vans pas cher, burberry outlet, ray ban pas cher, michael kors outlet, burberry handbags, uggs outlet, mulberry uk, michael kors outlet, michael kors, converse pas cher, north face uk, polo lacoste, nike air max uk, michael kors outlet, abercrombie and fitch uk, nike air force, kate spade, oakley pas cher, nike air max uk, michael kors outlet online, true religion outlet, uggs outlet, north face, ray ban uk, michael kors outlet online, hollister pas cher, nike air max, nike tn, guess pas cher, nike roshe run uk, true religion jeans, true religion outlet, coach purses, sac vanessa bruno, michael kors outlet online, new balance, lululemon canada, nike free uk, hogan outlet, true religion outlet

oakleyses said...

instyler, hollister, oakley, nfl jerseys, jimmy choo outlet, nike air max, north face outlet, soccer jerseys, louboutin, hollister, celine handbags, ghd hair, baseball bats, soccer shoes, gucci, ray ban, beats by dre, longchamp uk, lululemon, nike air max, ralph lauren, ferragamo shoes, bottega veneta, converse outlet, p90x workout, mcm handbags, nike trainers uk, converse, vans outlet, north face outlet, giuseppe zanotti outlet, hermes belt, reebok outlet, mont blanc pens, hollister clothing, chi flat iron, babyliss, herve leger, mac cosmetics, nike roshe run, valentino shoes, insanity workout, vans, nike huaraches, wedding dresses, iphone cases, timberland boots, new balance shoes, abercrombie and fitch, asics running shoes

oakleyses said...

canada goose, louis vuitton, replica watches, juicy couture outlet, pandora jewelry, canada goose, ugg,uggs,uggs canada, doudoune moncler, canada goose outlet, canada goose uk, canada goose jackets, louis vuitton, moncler outlet, barbour, pandora charms, ugg, moncler, louis vuitton, ugg uk, lancel, barbour uk, marc jacobs, moncler, links of london, karen millen uk, toms shoes, swarovski, wedding dresses, canada goose, louis vuitton, ugg,ugg australia,ugg italia, pandora jewelry, moncler, moncler outlet, supra shoes, hollister, swarovski crystal, thomas sabo, juicy couture outlet, canada goose outlet, moncler uk, pandora uk, coach outlet, ugg pas cher, louis vuitton, montre pas cher, canada goose outlet, moncler

Mie Helal said...



I was excited to discover this website
http://www.prokr.net/2016/09/furniture-moving-company-al-madina.html
http://www.prokr.net/2016/09/furniture-moving-company-yanbu.html