mvcConf slides and code posted
Yesterday, I gave a talk at the virtual ASP.NET MVC conference, mvcConf on “Putting your controllers on a diet. You can find the slides and code at the Headspring Labs CodePlex site:
http://headspringlabs.codeplex.com/
Just clone the Hg repository, and you’ll find all the code and slides. In the talk, I showed how to incrementally refactor this:
public class ConferenceControllerBefore : DefaultController { private readonly IConferenceRepository _repository; public ConferenceControllerBefore() { _repository = new ConferenceRepository(Sessions.Current); } public ActionResult Index(int? minSessions) { minSessions = minSessions ?? 0; var list = (from conf in _repository.Query() where conf.SessionCount >= minSessions select new ConferenceListModel { Id = conf.Id, Name = conf.Name, SessionCount = conf.SessionCount, AttendeeCount = conf.AttendeeCount }).ToArray(); return View(list); } public ViewResult Show(string eventName) { var conf = _repository.GetByName(eventName); var model = new ConferenceShowModel { Name = conf.Name, Sessions = conf.GetSessions() .Select(s => new ConferenceShowModel.SessionModel { Title = s.Title, SpeakerFirstName = s.Speaker.FirstName, SpeakerLastName = s.Speaker.LastName, }).ToArray() }; return View(model); } public ActionResult Edit(string eventName) { var conf = _repository.GetByName(eventName); var model = new ConferenceEditModel { Id = conf.Id, Name = conf.Name, Attendees = conf.GetAttendees() .Select(a => new ConferenceEditModel.AttendeeEditModel { Id = a.Id, FirstName = a.FirstName, LastName = a.LastName, Email = a.Email, }).ToArray() }; return View(model); } [HttpPost] public ActionResult Edit(ConferenceEditModel form) { if (!ModelState.IsValid) { return View(form); } var conf = _repository.GetById(form.Id); conf.ChangeName(form.Name); foreach (var attendeeEditModel in form.Attendees) { var attendee = conf.GetAttendee(attendeeEditModel.Id); attendee.ChangeName(attendeeEditModel.FirstName, attendeeEditModel.LastName); attendee.Email = attendeeEditModel.Email; } return this.RedirectToAction(c => c.Index(null), "Default"); } }
Into this:
public class ConferenceAfterController : DefaultController { public ActionResult Index(int? minSessions) { return Query<ConferenceListModel[]>(View(new ListConferences(minSessions))); } public ActionResult Show(Conference eventName) { return AutoMapView<ConferenceShowModel>(View(eventName)); } public ActionResult Edit(Conference eventname) { return AutoMapView<ConferenceEditModel>(View(eventname)); } [HttpPost] public ActionResult Edit(ConferenceEditModel form) { var success = this.RedirectToAction(c => c.Index(null), "Default"); return Form(form, success); } }
The recordings for the talks will be posted soon, so stay tuned to the mvcConf website for details. Thanks again to the organizers (Eric Hexter, Javier Lozano, Jon Galloway, and some others I know I’m forgetting). The conference went more smoothly than a lot of non-virtual conferences I’ve been to, so hopefully we have another one soon (well, not TOO soon…)