c# - EF DbContext VS Session lifetime -


i'm using entity framework code first ecommerce website. want store basket in session. i'm using ninject manage lifecycle of dbcontext, , i've set scope of per request.

my problem because lifetime of basket per session, lifetime of dbcontext per request - basket isn't attached each request's new dbcontext instance.

i re-instantiate basket on each request, seems quite inefficient getting basket details every request.

i reattach session's basket @ start of each request. seems best solution can see.

what's recommended way deal scenario?

i've found other questions / posts ask scope of dbcontext, , recommendations use ioc container use same dbcontext per request. i'm doing though - i've not found answer question how keep per-request dbcontext attached longer lifetime of entity in session state.

the recommended way not store persistence objects in basket rather pocos (plain objects) that:

  • can used show contents of basket efficiently
  • still contain database ids when order accepted can use ids retrieve database entites in current instance of database context

attaching , reattaching persistence objects possibly work, when have farm of application servers , decide persist session in database, persistence objects have serialized , deserialized, lead other suble issues (as long session persisted inproc session provider not seeing these issues).


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -