So this week we got to start working a brand spanking new MVC project. So far we’re leveraging Castle Windsor, NHibernate, Fluent Nhibernate, and kind of running Linq to NHibernate. It’s amazing how quickly you can get a project up and running in such a short amount of time. (BTW, Fluent NHibernate rocks!) When you’re building off the trunk of these projects, it’s almost like the contributors to all these great projects are extended members of the team. Thank you all!

Moving on… One of the things that are cool, but also slightly annoying, is how the MVC framework parses out items from the http payload to populate any input arguments on controller actions.

It’s great how it just works, but it’s a little annoying if it’s under test and you have to add more fields, or remove fields from a form, then you have to go update the signature of the action then go update the test…. yada yada The changes just ripple down…

So one thing we tried out this week was to create a payload parser. What this guy does is take a DTO parse out the values for each of the properties on the DTO from the current requests payload and fill it. This makes it easy to package up the form parameters in a nicely packaged DTO and fire it off down to a service layer to do some work.

So instead of declaring an action method on a controller that looks like this, where the signature would have to change based on what fields are submitted on a form:

ViewResult register_new_account(string user_name, string first_name, string last_name)

We can write this…

public ViewResult register_new_account() 
  {
      var accountSubmissionDTO = parser.MapFromPayloadTo<AccountSubmissionDTO>();
      var validationResult = task.Validate(accountSubmissionDTO);
      if (validationResult.IsValid) {
          task.Submit(accountSubmissionDTO);
          return View("Success", accountSubmissionDTO);
      }

      return View("Index", validationResult.BrokenRules);
  }

This better allows us to adhere to the OCP. If we need to include additional fields on the form, we can add them to the form as long as the control name is the same as the name of the property on the DTO that it will be bound to. The implementation of the payload parser is quite primitive for now, but at the moment it’s all that we needed.

First up the specs… simple enough, for now!

public class when_parsing_the_values_from_the_current_request_to_populate_a_dto : context_spec<IPayloadParser>
  {
      [Test]
      public void should_return_a_fully_populated_dto()
      {
          result.Name.should_be_equal_to("adam");
          result.Age.should_be_equal_to(15);
          result.Birthdate.should_be_equal_to(new DateTime(1982, 11, 25));
          result.Id.should_be_equal_to(1);
      }

      protected override IPayloadParser UnderTheseConditions()
      {
          var current_request = Dependency<IWebRequest>();
          var payload = new NameValueCollection();

          payload["Name"] = "adam";
          payload["Age"] = "15";
          payload["Birthdate"] = new DateTime(1982, 11, 25).ToString();
          payload["Id"] = "1";

          current_request.setup_result_for(r => r.Payload).Return(payload);

          return new PayloadParser(current_request);
      }

      protected override void BecauseOf()
      {
          result = sut.MapFromPayloadTo<SomeDTO>();
      }

      private SomeDTO result;
  }

  public class when_parsing_values_from_the_request_that_is_missing_values_for_properties_on_the_dto : context_spec<IPayloadParser>
  {
      private AccountSubmissionDTO result;

      [Test]
      public void it_should_apply_the_default_values_for_the_missing_properties()
      {
          result.LastName.should_be_null();
          result.EmailAddress.should_be_null();
      }

      protected override IPayloadParser UnderTheseConditions()
      {
          var current_request = Dependency<IWebRequest>();

          var payload = new NameValueCollection();

          payload["FirstName"] = "Joel";
          current_request.setup_result_for(x => x.Payload).Return(payload);

          return new PayloadParser(current_request);
      }

      protected override void BecauseOf()
      {
          result = sut.MapFromPayloadTo<AccountSubmissionDTO>();
      }
  }

  public class SomeDTO
  {
      public long Id { get; set; }
      public string Name { get; set; }
      public int Age { get; set; }
      public DateTime Birthdate { get; set; }
  }

The current implementation:

public interface IPayloadParser 
  {
      TypeToProduce MapFromPayloadTo<TypeToProduce>() where TypeToProduce : new();
  }

  public class PayloadParser : IPayloadParser 
  {
      private readonly IWebRequest current_request;

      public PayloadParser(IWebRequest current_request) 
      {
          this.current_request = current_request;
      }

      public TypeToProduce MapFromPayloadTo<TypeToProduce>() where TypeToProduce : new() 
      {
          var dto = new TypeToProduce();
          foreach (var propertyInfo in typeof (TypeToProduce).GetProperties()) {
              var value = Convert.ChangeType(current_request.Payload[propertyInfo.Name], propertyInfo.PropertyType);
              propertyInfo.SetValue(dto, value, null);
          }

          return dto;
      }
  }
comments powered by Disqus