Skip to content

Instantly share code, notes, and snippets.

@Deathspike
Created January 20, 2016 09:13
Show Gist options
  • Save Deathspike/5c6a23ef4b6ea81a3221 to your computer and use it in GitHub Desktop.
Save Deathspike/5c6a23ef4b6ea81a3221 to your computer and use it in GitHub Desktop.
EF6 UoW MVC
public class HomeController : DataController
{
#region Actions
[HttpGet]
public ActionResult Index()
{
return View(Context.Accounts // Using context.
.With(x => x.Identities) // Using includes.
.Where(x => x.AccountType >= AccountType.Administrator)); // Using filters.
}
[HttpPost]
public ActionResult Index(Account account)
{
Context.Accounts.Create(account);
Context.SaveChanges();
return RedirectToAction(nameof(Index));
}
#endregion
}
public abstract class DataController : ContractController
{
private IContext _context;
#region Properties
// TODO: Use Dependency Injection here, and inject a IContext appropriate to the user permissions/actions.
public IContext Context
{
get { return _context ?? (_context = new Context()); }
}
#endregion
#region Overrides of Controller
protected override void Dispose(bool disposing)
{
if (disposing)
{
_context?.Dispose();
}
base.Dispose(disposing);
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment