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…)

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Jimmy Bogard

I'm a technical architect with Headspring in Austin, TX. I focus on DDD, distributed systems, and any other acronym-centric design/architecture/methodology. I created AutoMapper and am a co-author of the ASP.NET MVC in Action books.
This entry was posted in ASP.NET MVC. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://www.squaredroot.com Troy Goode

    Hey Jimmy – this was the best presentation of the day, and I’ll being using it as a reference example for the rest of my team to provide some focus. Great work!

  • http://www.overridethis.com Roberto Hernandez

    I agree completely with Troy, your presentation was very good and insightful. I will be reading the provided code very diligently to steal all your ideas.

  • http://blog.buzzuti.com RyanAnderson

    I concur, outstanding JB!

  • http://www.larryruckman.com Larry Ruckman

    Agreed, great examples as always. I’ve learned a lot since I stumbled across the blog. Thanks for taking the time to write these.

  • http://www.larryruckman.com Larry Ruckman

    Question: How do you handle models that need a select list? Do you make an extra call from the view?

  • Dave

    Just watched your presentation – absolutely great! I just went to implement some of your ideas into my current project.

    Just one question: How would you handle/create EditModels that needs some other information like data for a select list or the actual User? The AutoMapViewResult is a great idea and I’d like to take this approach. But there are many EditModels I have that use additional data. You got an example?

    By the way: thanks for writing on this blog, your articles are very inspiring :-)

  • http://www.lostechies.com/members/bogardj/default.aspx bogardj

    @Dave @Larry

    This one’s actually a pretty tough question. It really depends on what information is needed to build the list. We often try to build this list in the view, with something like:

    InputFor(x => x.States).SelectListBuilder();

  • Dave

    Thanks!
    Let me get this right: The States property of the Model is null (or an empty List or just contains the id for the selected State) and then the StateBuilder gets the States from the repository and builds the list? Or if I’m wrong with my assumptions, how come the States into the edit model?

    Are you using FluentHtml here? Or Input Builders? It’s the next thing I want to take a look at :-)

  • Dave

    FYI: I also posted this question to StackOverflow, I think, this could interest quite some people:
    http://stackoverflow.com/questions/3507466/how-to-get-data-for-a-dopdownlist-into-viewmodel-when-using-automapper-automapvie

  • Greg

    I can’t find the database in the sample code. I see the NHibernate configuration below, but no MDF file or code to generate the database.
    server=.sqlexpress;database=HeadspringExample;Integrated Security=true;

    • Greg

      Never mind. I figured it out. :-)