1

A lot of a particular C# WinForms app I've seen is code doing simple maps between data and forms and back again. Surely, there has to be a better way. What's the best tool to do this type of mapping?

Goals:

  • Industrial strength, reliable, tested, wide user base
  • Preferably open source
  • Convention based, minimal code for simple maps
  • Configurable for more complicated maps
  • Gets out of the way for cases that I want to do completely by hand
  • Simple learning curve for the simple cases (i.e. 80% of usage)

Not that I necessarily am expecting something which meets all of those.

Extra points if you tell me about your experience, likes and dislikes of any tool you recommend.

SRobertJames
  • 131
  • 2
  • 6

2 Answers2

1

Take a look at AutoMapper.

AutoMapper uses a fluent configuration API to define an object-object mapping strategy. AutoMapper uses a convention-based matching algorithm to match up source to destination values. Currently, AutoMapper is geared towards model projection scenarios to flatten complex object models to DTOs and other simple objects, whose design is better suited for serialization, communication, messaging, or simply an anti-corruption layer between the domain and application layer.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Automapper just hooks up fields in one object to fields in another. AFAIK it doesn't recognize Winforms controls, so you would still need to write glue code to hook up your Winform controls to some sort of ViewData or DTO object. – Robert Harvey Mar 25 '11 at 20:21
1

Personally I use AutoMapper to map back and forth between my domain models (used by the service tier) and my view models (used by the UI tier). It's a tool that I have been using in large scale production application without ever having any issues.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • So you would still need glue code to hook up your ViewModel object to your winform controls, correct? – Robert Harvey Mar 25 '11 at 20:22
  • @Robert Harvey, no, I use **[MVP pattern in WinForms](http://stackoverflow.com/questions/185993/winforms-mvp-examples)** and MVVM in WPF so my controls are directly bound to the view models, no glue needed :-) – Darin Dimitrov Mar 25 '11 at 20:23
  • @Darin @Robert This was my concern exactly - AutoMapper is in terms of fields, not Winforms control. @Darin , can you elaborate how to use MVP to bind the controls automatically sans glue? – SRobertJames Mar 25 '11 at 21:51
  • @SRobertJames: Have a look at the link in Darin's comment. The examples provided there should show you how to use MVP to databind the controls directly to a ViewModel/Presenter object. From there, you can use Automapper to bind that object's fields to your own data domain objects. – Robert Harvey Mar 25 '11 at 22:02
  • @Robert - Thanks. It's been a while since I've done WinForms C#, and it seems a lot of modern best practices have been adopted. I'm comfortable with the back end stuff, like ORM, anonymous functions, and unit testing. Where can I learn more about modern best practices on the GUI / WinForms side? – SRobertJames Mar 25 '11 at 22:04
  • 1
    [Jeremy Miller's "Build your own CAB" series](http://codebetter.com/blogs/jeremy.miller/archive/2007/07/25/the-build-your-own-cab-series-table-of-contents.aspx) is apparently very good; I plan on going through his series myself. – Robert Harvey Mar 25 '11 at 22:08