Created
January 14, 2012 17:29
-
-
Save danieleli/1612189 to your computer and use it in GitHub Desktop.
A Generic Controller and Repository
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
| // Model with ForiegnKey | |
| public class Foo : ModelBase | |
| { | |
| public string SomeProperty { get; set; } | |
| public int BarId { get; set; } | |
| public Bar Bar { get; set; } | |
| } |
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 FooController : GenericController<Foo> | |
| { | |
| // might not need constructors as there isn't much to test. | |
| public FooController() : base() { } | |
| public FooController(IRepository<Foo> repo) : base(repo) { } | |
| protected override void PopulateDropDowns() | |
| { | |
| PopulateDropDown(base.DbContext.Bars.ToList(), "PossibleBars"); | |
| } | |
| // overriding view method to populate joined entity | |
| public override ViewResult Index() | |
| { | |
| return View(base.Repository.AllIncluding(foo => foo.Bar).ToList()); | |
| } | |
| } |
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 GenericController<T> : Controller | |
| where T : ModelRoot | |
| { | |
| private readonly GenericRepository<T> _repository; | |
| #region -- Constructors --- | |
| protected GenericController() | |
| : this(new GenericRepository<T>()) | |
| { | |
| } | |
| protected GenericController(GenericRepository<T> repository) | |
| { | |
| this._repository = repository; | |
| } | |
| #endregion // -- Constructors --- | |
| public GenericRepository<T> Repository { get { return _repository; } } | |
| public Mvc4Context DbContext { get { return (Mvc4Context) _repository.DbContext; } } | |
| /// <summary> | |
| /// Override this in derived class like this: | |
| /// | |
| /// protected override void PopulateDropDowns() { | |
| /// PopulateDropDown(base.DbContext.Bars.ToList(), "PossibleBars"); | |
| /// } | |
| /// </summary> | |
| protected virtual void PopulateDropDowns() { } | |
| protected void PopulateDropDown(IEnumerable<ModelNamedBase> items, string fieldName) | |
| { | |
| var marketItems = items.Select(item => new SelectListItem() {Selected = false, Text = item.Title, Value = item.Id.ToString()}).ToList(); | |
| ViewData[fieldName] = marketItems; | |
| } | |
| public virtual ViewResult Index() | |
| { | |
| return View(_repository.All); | |
| } | |
| public virtual ViewResult Details(int id) | |
| { | |
| return View(_repository.Find(id)); | |
| } | |
| public virtual ActionResult Create() | |
| { | |
| PopulateDropDowns(); | |
| return View(); | |
| } | |
| [HttpPost] | |
| public ActionResult Create(T model) | |
| { | |
| if (ModelState.IsValid) | |
| { | |
| _repository.InsertOrUpdate(model); | |
| _repository.Save(); | |
| if (Request.IsAjaxRequest()) | |
| { | |
| return Json(model, JsonRequestBehavior.AllowGet); | |
| } | |
| return RedirectToAction("Index"); | |
| } | |
| PopulateDropDowns(); | |
| return View(); | |
| } | |
| public virtual ActionResult Edit(int id) | |
| { | |
| PopulateDropDowns(); | |
| return View(_repository.Find(id)); | |
| } | |
| [HttpPost] | |
| public virtual ActionResult Edit(T model) | |
| { | |
| if (ModelState.IsValid) | |
| { | |
| _repository.InsertOrUpdate(model); | |
| _repository.Save(); | |
| return RedirectToAction("Index"); | |
| } | |
| PopulateDropDowns(); | |
| return View(); | |
| } | |
| public ActionResult Delete(int id) | |
| { | |
| return View(_repository.Find(id)); | |
| } | |
| [HttpPost, ActionName("Delete")] | |
| public ActionResult DeleteConfirmed(int id) | |
| { | |
| _repository.Delete(id); | |
| _repository.Save(); | |
| return RedirectToAction("Index"); | |
| } | |
| } |
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 GenericRepository<T> : IRepository<T> where T: ModelRoot | |
| { | |
| private readonly DbContext _context = new Mvc4Context(); | |
| public GenericRepository():this(new Mvc4Context()){} | |
| public GenericRepository(DbContext context) | |
| { | |
| _context = context; | |
| } | |
| public DbContext DbContext { get { return _context; } } | |
| public virtual IQueryable<T> All | |
| { | |
| get { return _context.Set<T>(); } | |
| } | |
| public virtual IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties) | |
| { | |
| IQueryable<T> query = _context.Set<T>(); | |
| foreach (var includeProperty in includeProperties) | |
| { | |
| query = query.Include(includeProperty); | |
| } | |
| return query; | |
| } | |
| public virtual T Find(int id) | |
| { | |
| return _context.Set<T>().Find(id); | |
| } | |
| public virtual void InsertOrUpdate(T entity) | |
| { | |
| //entity.ModifyDate = DateTime.Now; // todo: fix later | |
| if (entity.Id == default(int)) | |
| { | |
| // New entity | |
| _context.Set<T>().Add(entity); | |
| return; | |
| } | |
| // Existing entity | |
| _context.Entry(entity).State = EntityState.Modified; | |
| } | |
| public virtual void Delete(int id) | |
| { | |
| var entity = _context.Set<T>().Find(id); | |
| _context.Set<T>().Remove(entity); | |
| } | |
| public virtual void Save() | |
| { | |
| _context.SaveChanges(); | |
| } | |
| } |
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
| @foreach (var item in Model) { | |
| <tr> | |
| <td> | |
| ... action links here | |
| </td> | |
| <td> | |
| @item.Title | |
| </td> | |
| <td> | |
| @item.SomeProperty | |
| </td> | |
| <td> | |
| @String.Format("{0:g}", item.ModifyDate) | |
| </td> | |
| <td> | |
| @(item.Bar== null ? "" : item.Bar.Title) | |
| </td> | |
| </tr> | |
| } |
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 interface IRepository<TModel> | |
| { | |
| IQueryable<TModel> All { get; } | |
| IQueryable<TModel> AllIncluding(params Expression<Func<TModel, object>>[] includeProperties); | |
| TModel Find(int id); | |
| void InsertOrUpdate(TModel model, string user); | |
| void Delete(int id); | |
| void Save(); | |
| } |
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 ModelBase | |
| { | |
| public int Id { get; set; } | |
| public string Title { get; set; } | |
| [DisplayFormat(DataFormatString = "mm:hh:ss")] | |
| public DateTime? ModifyDate { get; set; } | |
| public string ModifyUser { get; set; } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment