4

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 want rewrite my Mapping Configuration.

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);
}

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

Download: http://wordpress.ad-factum.de/AutoMapperConfig_UsingValueInjecter.zip

BitKFu
  • 3,649
  • 3
  • 28
  • 43
  • lol now where's the question here exactly? ;) – Jimmy Bogard Aug 10 '11 at 12:36
  • I think it's interessting enough to share ;) I prefer the AutoMapper, but it was mandatory to compile against the .NET 4.0 Client Framework. So no other chance ... – BitKFu Aug 10 '11 at 13:58
  • 1
    Sorry, this space is for questions and answers only. If you'd like to share your code, share a repo on CodePlex, GitHub or somewhere similar. Or if you can find some way to turn your code into an actual answer to a real question, you can rephrase it as a question and answer. – BoltClock Dec 08 '11 at 11:52
  • The URL returns a 404 page not found error. – Rami A. May 26 '22 at 19:38

0 Answers0