c# - ASP MVC4 + NHibernate objects in Session -
i need save hibernate object session , retrieve 1 of it's foreign key properties this:
public actionresult login(loginmodel model, string returnurl) {     user usr = _userrepository.getbyid(convert.toint32(modelstate["user"].value.attemptedvalue));     session["user"] = usr;  }  public actionresult index() {      customer customeractive = session["user"].customer.active;      // line throws error:     // initializing[myproj.models.customer#3]-could not initialize proxy - no session. }   as user.customer foreign key , nhibernate lazy loads it, call fails. how prevent "no session" failure?
if want continue existing approach want make sure customer initialized before put session, e.g.
var userid = convert.toint32(modelstate["user"].value.attemptedvalue); user usr = _userrepository.getbyid(userid); nhibernateutil.initialize(usr.customer); session["user"] = usr;   but...
as commentators have hinted there various better approaches session not great place store might become large , complex objects have serialized , stored remotely if in web farm.
if passing userid around , loading database each time performance hit there several things can do, e.g.
- you put caching in front of database call
 - store basic user data within cookie (or local storage) save db hit -
 
Comments
Post a Comment