views:

61

answers:

2

I'm looking for an elegant way to have AppContext configured right and here is it:

public class AppContext : IAppContext
{
    public AppContext()
    {
        Application = new AppStorage();    // app scoped hashtable
        Local = new LocalStorage();        // current thread scoped hashtable
        Session = new SessionStorage();    // session for some reasons hashtable
    }

    public CultureInfo Culture { get; set; }     // session scoped
    public UserProfile AuthProfile { get; set; } // session scoped

    public IStorage Application { get; private set; } // application 
    public IStorage Session { get; private set; }     // session
    public IStorage Local { get; private set; }       // current thread
    public IStorage WcfSession { get; private set; }  // wcf session

    private ISecurityWriter SecurityWriter;           // session scoped
    private ISecurityContext SecurityContext;         // session scoped

    /// 1. START WEB CONTEXT
    /// 2. START WCF CONTEXT
}

currently I am balancing between a)

public class Global : HttpApplication
{
    public static AppContext Context;

    protected void Application_Start(object sender, EventArgs e)
    {
       Context = new AppContext();
    }
}

but I don't like the ideea to have

Global.Context.Sesstion.Set<Order>(theOrderInstance);

b) and the addition to AppContext following lines

public class AppContext{
private static AppContext instance;

public AppContext Instance
{ 
   get{ 
        if(instance == null) 
          instance = new AppContext();
        return instance;
   }
}

this also is not nice looking

AppContext.Instance.Session.Set<Order>(theOrderInstance);

QUESTION: I like the idea of having

AppContext.Session.Set<Order>(theOrderInstance);

any toughs how to achieve this ? something OSS and relevant for this topic would be greatly appreciated

have fun :)

A: 

How about this way?

protected AppContext Instance
{ 
   get{ 
        if(instance == null) 
          instance = new AppContext();
        return instance;
   }
}

public IStorage Session
{ 
   get{ 
        return Instance.Session;
   }
}
Greco
mmmm ... in your way I don't like the idea to add forwarders for every added member to AppContext
ruslander
A: 

look here:

public static class AppContextExtensions
{
    public static AppContext Context(this Page page)
    {
        return AppContext.Instance;
    }
}

usage

this.Context().Session.Set<Order>(theOrderInstance)

and i'm happy with it :)

ruslander