I am creating an MVC3 application with the Entity Framework, where almost all actions are allowed only to authorized users. Therefore, I often have to refer to a member object. I experimented with various ways of caching a member, and came up with an approach that is pretty fast. But I would like to get advice / point of view on the risks / disadvantages of what I do.
I defined a factory class to retrieve a Member instance that is "registered" with Ninject, so I can use it wherever I need it. Linking to Ninject is a “session” (I will explain this in a moment). The factory method first checks the session to see if it contains a previously created Member instance. If the session does not work, the procedure creates an instance from the base database through EF, storing the value in the session and also returning it.
Since some calls to the Member instance are EF calls, I had to ensure that the context of the EF object was also in the session area (I learned a difficult way to call EF routines against the Member instance that was originally created in another context does not work too well). Since the factory and object context are created using Ninject, I had to define the session scope for Ninject.
I found a piece of code for this here that I was able to modify to meet my needs. But this is such a simple fragment that I wonder if there is a reason why the opportunity is not provided “initially” by Ninject (or Ninject MVC). Which makes me wonder, I don’t care what I do.
I understand that there are a number of aspects of storing things in a session that you must program, the main one being the fact that the stored object can "disappear" at any time (i.e. you always need to have a way to recreate it when you receive). But while this adds enough complexity that I would not want to do this for multiple objects, it is not so difficult to do this for a single member object.
In any case, advice and feedback on viewing the Ninject bindings on binding and storing EF objects in a session for an MVC application will be appreciated.
user553671
source share