0

I am trying to build a session/tempdata provider that can be swapped. The default provider will work on top of asp.net mvc and it needed to access the .net mvc TempData from the business object class. I know the tempdata is available through the controller context, but i cant seem to find if that is exposed through HttpContext or something. I dont really want to pass the Controller context as an argument as that would dilute my interface definition since only asp.net based session provider needs this, other (using NoSQL DB etc) doesn't care about Controller Context.

To clarify further, adding little more code here. my ISession interface look like this. and when this code goes to production, the session/tempdata is expected to work using NoSql db. But i also like to have another implementation that works on top of asp.net mvc session/tempdata for my dev testing etc.

public interface ISession

{
    T GetTempData<T>(string key);

    void PutTempData<T>(string key, T value);

    T GetSessiondata<T>(string key);

    void PutSessiondata<T>(string key, T value);

}
thanikkal
  • 3,326
  • 3
  • 27
  • 45

1 Answers1

0

I don't know exactly what you are trying to do but TempDataDictionary implements IDictionary<string, object> so you could have your business objects take it as parameter or use constructor injection. Then you could have your controller pass the TempData as parameter to the business object. By using the dictionary interface your business objects no longer depend on ASP.NET MVC.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks for replying. I was trying to avoid having to pass the tempdata/controller context to those methods. hope my edit above makes it clearer. – thanikkal Mar 05 '11 at 15:26
  • @thanikkal, are you trying to override the default ASP.NET Session provider which stores data in memory with a custom provider? – Darin Dimitrov Mar 05 '11 at 16:00
  • Not sure override is the term, but yes, i am trying to have my own session provider. – thanikkal Mar 05 '11 at 16:11
  • @thanikkal, in this case you will need to write a [Session-State Store Provider](http://msdn.microsoft.com/en-us/library/ms178587.aspx) by implementing the [SessionStateStoreProviderBase](http://msdn.microsoft.com/en-us/library/28eshy5z.aspx) class and registering your session state provider in web.config. – Darin Dimitrov Mar 05 '11 at 16:16
  • Thanks for pointing. But I am not exactly looking to go the msdn way for session stuff. and my original question of accessing MVC intrinsic objects from outside still stands unanswered. – thanikkal Mar 06 '11 at 02:57
  • Thinking over this again, i think its probably a sensible approach to go with the built in asp.net session since it already has the ability to support different providers and there is mysql and mongo db providers readily available. I moved the above methods as Controller extension methods so that i get strongly typed objects back from the session and tempdata. – thanikkal Mar 08 '11 at 15:14