标签:
1.说明
在《ASP.NET5+EntityFramework7开发实践(一)》介绍过仓储模式,只是没有在控制器中如何使用?
本章重新补充一下。注意,也会使用ASP.NET5中的依赖注入。
2.仓储模式
先看接口:
1 public interface IRoleRepository:IDisposable 2 { 3 //IEnumerable和IQueryable 4 //二者在EF都会延迟加载,不同的是: 5 //IEnumerable是数据加载到内存,刷选在内存中的数据上执行 6 //IQueryable是查询和刷选在数据源中执行 7 //TODO:复杂条件查询和异步方法 8 //IQueryable<Role> GetRoles(); 9 10 IEnumerable<Role> GetRoles(); 11 Role GetRole(int? id); 12 void Create(Role role); 13 void Update(Role role); 14 void Delete(int? id); 15 }
再看其实现:
public class RoleRepository : IRoleRepository { private EFContext db; public RoleRepository(EFContext _db) { db = _db; } public IEnumerable<Role> GetRoles() { return db.Roles.ToList(); } public Role GetRole(int? id) { return db.Roles.Single(m => m.Id == id); } public void Create(Role role) { db.Roles.Add(role); db.SaveChanges(); } public void Delete(int? id) { db.Roles.Remove(db.Roles.Single(m => m.Id == id)); db.SaveChanges(); } public void Update(Role role) { //注意: db.Roles.Update(role); 不行的 db.ChangeTracker.AcceptAllChanges(); db.SaveChanges(); } #region 释放资源 private bool disposed = false; protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { db.Dispose(); } } disposed = true; } public void Dispose() { Dispose(true); System.GC.SuppressFinalize(this); } #endregion }
3.控制器使用
1 public class RoleController : Controller 2 { 3 //注意使用接口哦 4 private IRoleRepository roleRepository; 5 public RoleController(IRoleRepository _roleRepository) 6 { 7 roleRepository = _roleRepository; 8 } 9 10 public IActionResult Index() 11 { 12 return View(roleRepository.GetRoles()); 13 } 14 public IActionResult Details(int? id) 15 { 16 if (id == null) { return HttpNotFound(); } 17 18 Role role = roleRepository.GetRole(id); 19 20 if (role == null) { return HttpNotFound(); } 21 22 return View(role); 23 } 24 25 public IActionResult Create() 26 { 27 return View(); 28 } 29 30 [HttpPost] 31 [ValidateAntiForgeryToken] 32 public IActionResult Create(Role role) 33 { 34 if (ModelState.IsValid) 35 { 36 roleRepository.Create(role); 37 return RedirectToAction("Index"); 38 } 39 return View(role); 40 } 41 42 public IActionResult Edit(int? id) 43 { 44 if (id == null) { return HttpNotFound(); } 45 46 Role role = roleRepository.GetRole(id); 47 if (role == null) 48 { 49 return HttpNotFound(); 50 } 51 return View(role); 52 } 53 54 [HttpPost] 55 [ValidateAntiForgeryToken] 56 public IActionResult Edit(Role role) 57 { 58 if (ModelState.IsValid) 59 { 60 roleRepository.Update(role); 61 return RedirectToAction("Index"); 62 } 63 return View(role); 64 } 65 66 [ActionName("Delete")] 67 public IActionResult Delete(int? id) 68 { 69 if (id == null) { return HttpNotFound(); } 70 71 Role role = roleRepository.GetRole(id); 72 73 if (role == null) { return HttpNotFound(); } 74 75 return View(role); 76 } 77 78 [HttpPost, ActionName("Delete")] 79 [ValidateAntiForgeryToken] 80 public IActionResult DeleteConfirmed(int id) 81 { 82 Role role = roleRepository.GetRole(id); 83 roleRepository.Delete(id); 84 return RedirectToAction("Index"); 85 } 86 }
4.依赖注入
在Startup.cs中,添加注入配置:
public void ConfigureServices(IServiceCollection services) { //... //依赖注入 services.AddSingleton<IRoleRepository, RoleRepository>(); services.AddMvc(); }
5.小结
下一章再补充工作单元……
ASP.NET5+EntityFramework7开发实践(三)
标签:
原文地址:http://www.cnblogs.com/givecase/p/4921150.html