views:

346

answers:

1

I using extension for Object, because it provides very short accessible string.

public static BusinessLayer.Models.SearchEngineEntities db(this object o)
 {

  if (HttpContext.Current == null)
   return new BusinessLayer.Models.SearchEngineEntities(ConfigurationManager.ConnectionStrings["SearchEngineEntities"].ConnectionString);

  if (HttpContext.Current.Items.Contains("DataContext"))
  {
   return (BusinessLayer.Models.SearchEngineEntities)HttpContext.Current.Items["DataContext"];
  }

  BusinessLayer.Models.SearchEngineEntities context = new BusinessLayer.Models.SearchEngineEntities(ConfigurationManager.ConnectionStrings["SearchEngineEntities"].ConnectionString);
  HttpContext.Current.Items.Add("DataContext", context);
  return context;
 }

So in a result I have execution string like:

"".db().Users.Include("")....

But I'm not 100% sure that is best solution. I hope you know better than me, where and how to store DataContext or ObjectContext instance. Maybe you know better solution...

Thank you in advice.

+2  A: 

Why do you need an Extension Method to do this? You should abandon the extension method and use just a static method (and a static class). Also I found an article that I think it might help you:

Managing Entity Framework ObjectContext lifespan and scope in n-layered ASP.NET applications

bruno conde