1. Description
In the "Asp.net5+entityframework7 Development Practice (i)" introduced the storage mode, but not in the controller how to use?
This chapter re-adds. Note that dependency injection in ASP.NET5 is also used.
2. Warehousing mode
First look at the interface:
1 Public Interfaceirolerepository:idisposable2 {3 //IEnumerable and IQueryable4 //both of them delay loading in EF, but the difference is:5 //IEnumerable is the data that is loaded into memory, which is executed by swiping the data in memory6 //IQueryable is the query and swipe selected in the data source to execute7 //TODO: Complex conditional queries and Async methods8 //iqueryable<role> getroles ();9 TenIenumerable<role>getroles (); OneRole Getrole (int?ID); A voidCreate (role role); - voidUpdate (role role); - voidDelete (int?ID); the}
Then look at its implementation:
Public classRolerepository:irolerepository {PrivateEfcontext DB; Publicrolerepository (Efcontext _db) {db=_db; } PublicIenumerable<role>GetRoles () {returndb. Roles.tolist (); } PublicRole Getrole (int?ID) {returnDb. Roles.single (m = m.id = =ID); } Public voidCreate (role role) {db. Roles.add (role); Db. SaveChanges (); } Public voidDelete (int?ID) {db. Roles.remove (db. Roles.single (M= = M.id = =id)); Db. SaveChanges (); } Public voidUpdate (role role) {//Note: Db. Roles.update (role); I can't.db. Changetracker.acceptallchanges (); Db. SaveChanges (); } #regionFreeing resourcesPrivate BOOLdisposed =false; protected Virtual voidDispose (BOOLdisposing) { if(!disposed) { if(disposing) {db. Dispose (); }} disposed=true; } Public voidDispose () {Dispose (true); System.GC.SuppressFinalize ( This); } #endregion }
3. Controller use
1 Public classRolecontroller:controller2 {3 //pay attention to using the interface Oh4 Privateirolerepository rolerepository;5 PublicRolecontroller (irolerepository _rolerepository)6 {7Rolerepository =_rolerepository;8 }9 Ten Publiciactionresult Index () One { A returnView (Rolerepository.getroles ()); - } - PublicIactionresult Details (int?ID) the { - if(id = =NULL) {returnhttpnotfound ();} - -Role role =rolerepository.getrole (ID); + - if(Role = =NULL) {returnhttpnotfound ();} + A returnView (role); at } - - Publiciactionresult Create () - { - returnView (); - } in - [HttpPost] to [Validateantiforgerytoken] + PublicIactionresult Create (role role) - { the if(modelstate.isvalid) * { $ rolerepository.create (role);Panax Notoginseng returnRedirecttoaction ("Index"); - } the returnView (role); + } A the PublicIactionresult Edit (int?ID) + { - if(id = =NULL) {returnhttpnotfound ();} $ $Role role =rolerepository.getrole (ID); - if(Role = =NULL) - { the returnHttpnotfound (); - }Wuyi returnView (role); the } - Wu [HttpPost] - [Validateantiforgerytoken] About PublicIactionresult Edit (role role) $ { - if(modelstate.isvalid) - { - rolerepository.update (role); A returnRedirecttoaction ("Index"); + } the returnView (role); - } $ the[ActionName ("Delete")] the PublicIactionresult Delete (int?ID) the { the if(id = =NULL) {returnhttpnotfound ();} - inRole role =rolerepository.getrole (ID); the the if(Role = =NULL) {returnhttpnotfound ();} About the returnView (role); the } the +[HttpPost, ActionName ("Delete")] - [Validateantiforgerytoken] the PublicIactionresult deleteconfirmed (intID)Bayi { theRole role =rolerepository.getrole (ID); the rolerepository.delete (ID); - returnRedirecttoaction ("Index"); - } the}
4. Dependency Injection
In Startup.cs, add the injection configuration:
Public void configureservices (iservicecollection services) { //... // Dependency Injection Services. Addsingleton<irolerepository, rolerepository>(); Services. Addmvc (); }
5. Summary
The next chapter complements the unit of work ...
Asp.net5+entityframework7 Development Practice (III)