views:

399

answers:

4

I am busy converting a web application to MVC and have some information saved to Application variables used across multiple tenants/accounts to make things a bit more efficient.

I realise the point of MVC is to keep things as stateless as possible, Sesion State obviously makes sense to have and exists in MVC but we dont want to just convert Application to Session variables as we would rather have something more global and more secure. Do MVC applications have Application Variables? I have seen some examples where caching is used? Is this now standard and How robust/secure is this compared to Application/Session State?

+1  A: 

Make a static class?

svinto
I find static classes work great under the MVC context, it also helps you get away from the app/session state idea.
Jeremy B.
@jeremy except the session state is specifically updated for use in MVC. I'd definitely support "getting away from" it in order to reduce the amount of state stored on the server, but it definitely still has a place in the framework.
Will
application scope and static class are very different things. Though they can be interchanged in some situations.
kervin
A: 

Do they have Application Variables? Yes, MVC is a framework that sits on top of the normal asp.net framework.

I would however create a static class that uses a cache store as it's backing.

used2could
A: 

Session state or the Cache are better choices. They are mockable in MVC and are designed to store session and application-scoped data.

Static classes seems like a popular choice here. However static classes create dependencies between your types and make versioning/testing harder. Its also a bit of an odd pattern to use in a framework that is designed to break apart these kinds of dependencies. For instance, the standard ASP.NET framework is riddled with statics and sealed types. These are all replaced with mock-able instances.

"Secure" is a bit unclear in this context. Exactly what do you mean by "secure?"

Will
Of course for optimal decoupling, testing etcetera one should store them in an ordinary class and put a instance that class in a the IoC-container.
svinto
@svinto that all depends on the rest of the design. IOC isn't configuration. You can configure for IOC but that's more about what type to use in this situation, not what color to use on the header background, for instance.
Will
+2  A: 

Yes, you can access Application variables from .NET MVC. Here's how:

System.Web.HttpContext.Current.Application.Lock();
System.Web.HttpContext.Current.Application["Name"] = "Value";
System.Web.HttpContext.Current.Application.UnLock();
WWC