Using AutoMapper Config with ValueInjecter

For all who are interessted, I created a ValueInjecter extension in order to use your existing AutoMapper Mapping Configuration with ValueInjecter. I created this extension because I had to switch over to ValueInjecter, because I could not compile AutoMapper against Client Framework 4.0. And I didn’t wanted rewrite my Mapping Configuration.

It looks like the AutoMapper, but internally it uses ValueInjecter ;) Hope you enjoy the extension.

Example:

public class CalendarEvent
{
    public DateTime EventDate { get; set; }
    public string Title { get; set; }
}

public class CalendarEventForm
{
    public DateTime EventDate { get; set; }
    public int EventHour { get; set; }
    public int EventMinute { get; set; }
    public string Title { get; set; }

    // Additional Members to show further mapping possibilities
    public string BarToIgnore { get; set; }
    public bool AlwaysTrue { get; set; }
}

static void Main(string[] args)
{
    // You can use the AutoMapper extension for the ValueInjecter like you would user AutoMapper

    // Model
    var calendarEvent = new CalendarEvent
    {
        EventDate = new DateTime(2008, 12, 15, 20, 30, 0),
        Title = "Company Holiday Party"
    };

    // Configure AutoMapper
    Mapper.CreateMap<CalendarEvent, CalendarEventForm>()
        .ForMember(dest => dest.EventDate, opt => opt.MapFrom(src => src.EventDate.Date))
        .ForMember(dest => dest.EventHour, opt => opt.MapFrom(src => src.EventDate.Hour))
        .ForMember(dest => dest.EventMinute, opt => opt.MapFrom(src => src.EventDate.Minute))
        .ForMember(dest => dest.Title, opt => opt.MapFrom(src => src.Title))
        .ForMember(dest => dest.BarToIgnore, opt => opt.Ignore())
        .ForMember(dest => dest.AlwaysTrue, opt => opt.UseValue(true));

    Mapper.AssertConfigurationIsValid();

    // Perform mapping
    CalendarEventForm form = Mapper.Map<CalendarEvent, CalendarEventForm>(calendarEvent);

    // Assertions
    Debug.Assert(form.EventDate == new DateTime(2008, 12, 15));
    Debug.Assert(form.EventHour == 20);
    Debug.Assert(form.EventMinute == 30);
    Debug.Assert(form.Title == "Company Holiday Party");
    Debug.Assert(form.BarToIgnore == null);
    Debug.Assert(form.AlwaysTrue == true);
}

You can download the extension from: http://wordpress.ad-factum.de/AutoMapperConfig_UsingValueInjecter.zip

And if you like it, I really would appreciate an UpVote on StackOverflow ;)

UPDATE
And because of dhollifield (see comments) I updated the code so that unmapped properties are getting an automatic mapping. It saves time for the most mappings, but keep in mind that this enhancement is not exchangeable with the AutoMapper anymore.

Call the Method “MapUnmappedProperties” in the automapper configuration in order to use this feature. The updated code can be downloaded from here : http://wordpress.ad-factum.de/AutoMapperConfig_UsingValueInjecter2.zip

Posted in .NET, C#. 5 Comments »
Follow

Get every new post delivered to your Inbox.

Join 77 other followers