Created
January 20, 2016 09:13
-
-
Save Deathspike/5c6a23ef4b6ea81a3221 to your computer and use it in GitHub Desktop.
EF6 UoW MVC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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