Sunday, August 14, 2011

one model for In and Out

I spoke with Joel Holder about the approach in my last posting on Friday and there seemed to be a consensus that it was overkill (in most cases) to have two separate models for passing content both in and out of a view. If the two models could be combined into one, while not truly shaped for either, perhaps that would lessen the number of files sprinkled about the application. Perhaps it is not unreasonable to believe a person could make sense of the following:

using System;

using System.Collections.Generic;

using Core;

namespace MyApp.Models

{

   public class PersonModel

   {

      public string Name { get; set; }

      public string BirthDay { get; private set; }

      public string Age { get; set; }

      public Dictionary<Guid, string> HomeCollection { get; private set; }

      public string Home { get; set; }

      

      public void PrepModel(Person person, Address[] potentialHomes)

      {

         Name = person.Name;

         BirthDay = person.BirthDay.Month.ToString();

         BirthDay = BirthDay + "/" + person.BirthDay.Day;

         BirthDay = BirthDay + "/" + person.BirthDay.Year;

         Age = person.Age.ToString();

         HomeCollection = new Dictionary<Guid, string>();

         HomeCollection.Add(person.Home.Id, person.Home.Street);

         foreach (Address address in potentialHomes)

         {

            if (address != person.Home)

            {

               HomeCollection.Add(address.Id, address.Street);

            }

         }

         Home = person.Home.Id.ToString();

      }

   }

}

 
 

This model is a hodgepodge of the two models in my last posting. The controller from my last posting gets refined to look like this:

using System;

using System.Web.Mvc;

using Core;

using MyApp.Models;

namespace MyApp.Controllers

{

   public class HomeController : Controller

   {

      public ActionResult Index()

      {

         Address austin = new Address();

         austin.Id = new Guid("75b5e18c-13c5-4211-bf8d-9f3d0117d88c");

         austin.Street = "1507 Houston Street #145";

         austin.ZipCode = 78756;

         

         Address haverhill = new Address();

         haverhill.Id = new Guid("bb61cd17-c176-4102-838a-9f3d0117d892");

         haverhill.Street = "5055 Club Road";

         haverhill.ZipCode = 33415;

         

         Address houston = new Address();

         houston.Id = new Guid("31ba86cb-991f-4e71-a2df-9e4000a8b3bc");

         houston.Street = "14795 Memorial Drive";

         houston.ZipCode = 77079;

         

         Address lampasas = new Address();

         lampasas.Id = new Guid("d74e7f2c-ad8d-4522-bb1e-9f3d0117d895");

         lampasas.Street = "1006 East Avenue F";

         lampasas.ZipCode = 76550;

         

         Address[] addresses = new Address[] {austin, haverhill, houston, lampasas};

         Session["potentialAddress"] = addresses;

         

         Person person = new Person();

         person.Id = new Guid("705b5d2b-6bed-4d6d-af29-9e4000a8b25e");

         person.Name = "Tom";

         person.setBirthDay(new DateTime(1974, 8, 24));

         person.Home = houston;

         Session["personAtHand"] = person;

         

         PersonModel personModel = new PersonModel();

         personModel.PrepModel(person, addresses);

         

         return View(personModel);

      }

      

      [HttpPost]

      public ActionResult Index(PersonModel personModel)

      {

         Person person = Session["personAtHand"] as Person;

         Address[] addresses = Session["potentialAddress"] as Address[];

         person.Name = personModel.Name;

         try

         {

            person.alterBirthDay(Convert.ToInt32(personModel.Age));

         }

         catch(InvalidCastException exception)

         {

            throw (exception);

         }

         Guid addressGuid = new Guid(personModel.Home);

         foreach(Address address in addresses)

         {

            if (address.Id == addressGuid) person.Home = address;

         }

         Session["personAtHand"] = person;

         return View("About", personModel);

      }

   }

}

 
 

The view from my last posting gets updated too. The model binding at the top becomes:

@model MyApp.Models.PersonModel

 
 

...while the dropdown list takes this shape:

@Html.DropDownListFor(m => m.Home, new SelectList(Model.HomeCollection, "key", "value"))

 
 

Some pain points I had in refactoring to one model:

  1. Until I changed get; private set; to get; set; in applicable cases, values would come back from the View as null. Duh.
  2. I original tried the wrong approach to the dropdown list:

    @Html.DropDownListFor(m => m.HomeCollection, new SelectList(Model.HomeCollection, "key", "value"), new { @id = "Home", @name = "Home" })

    While one may override the default value of id in this manner, I could not seem to change the name parameter.

No comments:

Post a Comment