ASP.NET MVC 5 + Entity Framework – Generic Repository 方法
( Project : SoccerMVCDAL ) \DAL\IDAL.cs |
using System; namespace SoccerMVCDAL.DAL { interface IDAL { void Insert<T>(T Entity) where T : class; void Update<T>(T Entity) where T : class; void Delete<T>(T Entity) where T : class; void SaveChange(); } } |
( Project : SoccerMVCDAL ) \DAL\DAL.cs |
using System; namespace SoccerMVCDAL.DAL { public class DAL : IDAL { public SoccerEntities entity; public DAL() { entity = new SoccerEntities(); } public virtual void Insert<T>(T Entity) where T : class { entity.Set<T>().Add(Entity); this.SaveChange(); } public virtual void Update<T>(T Entity) where T : class { this.SaveChange(); } public virtual void Delete<T>(T Entity) where T : class { entity.Set<T>().Remove(Entity); this.SaveChange(); } public virtual void SaveChange() { entity.SaveChanges(); } } } |
( Project : SoccerMVCDAL ) \DAL\PlayerDAL.cs |
using System; using System.Collections.Generic; using System.Linq; namespace SoccerMVCDAL { public class PlayerDAL : DAL { public List<PLAYER> getAllPlayer() { return entity.PLAYERs.ToList(); } public PLAYER getPlayerByPlayerID(int PlayerID) { PLAYER player = entity.PLAYERs.Where(x=>x.PLAYER_ID == PlayerID).FirstOrDefault(); if(player != null) return player; else return null; } public List<PLAYER> getPlayerByNation(int NationID) { return entity.PLAYERs.Where(x => x.PLAYER_NATION == NationID).ToList(); } public List<PLAYER> getPlayerByTeam(int TeamID) { return entity.PLAYERs.Where(x => x.PLAYER_TEAM == TeamID).ToList(); } public List<PLAYER> getPlayerByPosition(int PositionID) { return entity.PLAYERs.Where(x => x.PLAYER_POSITION == PositionID).ToList(); } } } |
( Project : SoccerMVCWebApp ) web.conf |
<configuration> <connectionStrings> <add name="SoccerEntities" connectionString="… …" providerName="System.Data.EntityClient" /> </connectionStrings> … … </configuration> |
( Project : SoccerMVCWebApp ) \Controllers\PlayerController.cs |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using SoccerMVCDAL.DAL; using SoccerMVCDAL; namespace SoccerMVCWebApp.Controllers { public class PlayerController : Controller { PlayerDAL DAL; public PlayerController() { DAL = new PlayerDAL(); } public ActionResult Index() { return View(DAL.getAllPlayer()); } public ActionResult Position(int? ID) { if(ID.HasValue) return View("Index", DAL.getPlayerByPosition(ID.Value)); else return View("Index", DAL.getAllPlayer()); } public ActionResult Nation(int? ID) { if (ID.HasValue) return View("Index", DAL.getPlayerByNation(ID.Value)); else return View("Index", DAL.getAllPlayer()); } public ActionResult Team(int? ID) { if (ID.HasValue) { return View("Index", DAL.getPlayerByTeam(ID.Value)); } else { return View("Index", DAL.getAllPlayer()); } } public ActionResult Insert() { PLAYER player = new PLAYER(); player.PLAYER_ID = 11111; player.PLAYER_NAME = "Dummy"; player.PLAYER_NATION = 1; player.PLAYER_POSITION = 1; player.PLAYER_TEAM = 1; player.PLAYER_BIRTHDAY = new DateTime(); DAL.Insert<PLAYER>(player); return RedirectToAction("Index"); } public ActionResult Update() { PLAYER player = DAL.getPlayerByPlayerID(11111); player.PLAYER_NAME = "Dummy X"; DAL.Update<PLAYER>(player); return RedirectToAction("Index"); } public ActionResult Delete() { PLAYER player = DAL.getPlayerByPlayerID(11111); DAL.Delete<PLAYER>(player); return RedirectToAction("Index"); } } } |