Using Automapper ValueResolver for complex, reusable mapping

Tuesday, October 23, 2012
Automapper is a great tool to help automate the tedious task of mapping Domain Model to View Model and back.  However, sometimes, mappings are not as straightforward as a simple one to one, left to right mapping.  The example below is a real-world implementation of ValueResolver for mapping date of birth to age:

using System;
using AutoMapper;

namespace SomeCompany.SomeProject.Web.Models.ValueResolvers
    public class AgeValueResolver : ValueResolver<DateTime?, int>
        protected override int ResolveCore(DateTime? dateOfBirth)
            if (dateOfBirth.HasValue)
                var age = DateTime.Today.Year - dateOfBirth.Value.Year;

                if (dateOfBirth.Value > DateTime.Today.AddYears(-age))

                return age;

            return 0;

To use this within your mapping, you could do something like:

CreateMap<IPerson, PersonViewModel>()
  .ForMember(dest => dest.Age, opt => opt.ResolveUsing<AgeValueResolver>().FromMember(src => src.DOB));

JSLint - '$' was used before it was defined

Friday, September 14, 2012
I'm using JSLint to validate my JavaScript.  For no other reason, it keeps things consistent.  However, I get the below warning message when performing a $(document).ready(...) call:

'$' was used before it was defined

To get around this, I pass jQuery in, assign it to $, and set my JSLint options.  Below is an example usage:

<script type="text/javascript">
    /// <reference path="../../Scripts/jquery-1.7.2.js" />
    /*jslint browser: true*/

    (function ($) {
        "use strict";
        /*global jQuery, document*/
        $(document).ready(function () {