c# - Entity Framework ObjectContext lifetime in an n-tier project -


i'm building large solution client code reuseability keyword since different types of projects (i.e. websites, wcf services, wpf etc) should use exact same businesslogic.

right now, solution looks this:

  • businesslogic layer (all of logic defines business rules added here)
  • interfaces
  • model (viewmodels, dtos etc)
  • repository (currently using entity framework 6. database transactions go)
  • webservices (wcf services)
  • mvc website

the point presentation layer (i.e. mvc website) use businesslogic layer uses repository make database transaction needed.

while works way want to, find myself battleing objectcontext entity framework, because, when querying data in repository, entities doesn't transferred businesslogic (which logical because of efs lazy-loading) i'm aware can make use of .include(x => x.myothertable), since database large, approach gets bloated , queries can rather large if included table has lot of records.

i've made dbcontextmanager class looks this:

public static class dbcontextmanager {     //unique context key per request , thread     private static string key     {                 {             return string.format("mydb_{0}{1}", httpcontext.current.gethashcode().tostring("x"), thread.currentcontext.contextid);         }     }      //get , set request context     private static myentities context     {         { return httpcontext.current.items[key] myentities ; }         set { httpcontext.current.items[key] = value; }     }      //context per request     public static myentities current     {                 {             //if null, create new context              if (context == null)             {                 context = new myentities ();                 httpcontext.current.items[key] = context;             }             return context;         }     }      //dispose created context @ end of request - called global.asax     public static void dispose()     {         if (context != null)         {             context.dispose();         }     } } 

and in global.asax, i'm disposing context when request ended:

private void application_endrequest(object sender, eventargs e)     {         dbcontextmanager.dispose();     } 

this works perfect, can make initial database call in repository , make businesslogic rules in businesslogic layer, because objectcontext lives http request.

however, when need call same businesslogic methods wcf services project (or i.e. wpf project), won't able make use of dbcontextmanager class since relies on httpcontext.

i feel i'm doing wrong @ moment , i'm fighting unnessecary battle entity framework. have missed? entity framework right orm these types of solutions? doesn't feel right :-)

any or hints appreciated!

thanks in advance.

i'm confused here. looks using entity layer objects directly in view layer.

this bad idea. model should encapsulate these. business logic layer should use model layer build viewmodel , dto objects, ui layer can produce/consume. allow separate ui layer database/entity layer.

that make re-using business logic in web service layer trivial. should know nothing of database layer. otherwise adding or changing field in entity layer have direct impact on ui layers.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -