Skip to content

Instantly share code, notes, and snippets.

@kcuzner
Last active January 2, 2016 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kcuzner/8306451 to your computer and use it in GitHub Desktop.
Save kcuzner/8306451 to your computer and use it in GitHub Desktop.
namespace MyMvcApplication
{
class DbQueries : IDisposable
{
protected TimeShedulerEntities Context { get; private set; }
public DbQueries()
{
this.Context = new TimeShedulerEntities();
}
public Branch[] LoadBranches(int companyId, int page, int limit, string search, string sort, string sortOrder)
{
var _branches = (from ct in this.Context.Branches
where ct.Title.Contains(search) || ct.Code.Contains(search)
select ct).OrderBy(c => c.Title).Skip((page - 1) * limit).Take(limit);
return _branches.ToArray();
}
public void SaveChanges()
{
this.Context.SaveChanges();
}
public void Dispose()
{
this.Context.Dispose();
}
}
}
namespace MyMvcApplication
{
public class MvcApplication : HttpApplication //this should already exist
{
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Items["DbQueries"] = new DbQueries();
}
protected void Application_EndRequest(object sender, EventArgs e)
{
((DbQueries)HttpContext.Current.Items["DbQuerues"]).Dispose();
}
}
}
namespace MyMvcApplication
{
public class HomeController : Controller
{
public ActionResult Index()
{
DbQueries dbQueries = (DbQueries)HttpContext.Current.Items["DbQueries"];
ViewBag.Branches = dbQueries.LoadBranches(...);
return View();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment