Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Automapper be used to bind class instances to Winforms, and if so, how?

I have a number of Data Transfer Objects (DTO's) that map onto data structures in a binary file. Some of the fields in these objects map to enums, like this one:

public enum DataChecksumExistence
{
    None = 0x00,
    Eight_Bits = 0x01,
    Sixteen_Bits = 0x02,
    Thirty_Two_Bits = 0x03
}

Other fields are just numbers or boolean bit values.

I have an extension method ToList() on Enum that returns a list of key-value pairs from any Enum that can be stuffed into a dropdown in the Form_Load:

uxDataChecksumExistence.DataSource = typeof(DataChecksumExistence).ToList();

In a public property of the Winform that returns an instance of my DTO, I have the following in the getter:

dto.DataChecksumExistence 
    = (DataChecksumExistence)uxDataChecksumExistence.SelectedValue

and the following in the setter:

uxDataChecksumExistence.SelectedValue = dto.ChecksumSize;

Multiply this code by about 50 fields, and then multiply it again by the number of forms I will need in my application (it is dozens), and you can see my dilemma.

Is there a way to use Automapper to do this? Or would it be better to code-gen it with something like T4 templates? Please provide a code sample if you can; I don't need the whole Bible, just a few verses to get pointed in the right direction.

like image 333
Robert Harvey Avatar asked Nov 05 '22 07:11

Robert Harvey


1 Answers

It is not possible with automapper, but you can see here (http://valueinjecter.codeplex.com) in the Demo a winforms project which uses mapping, you can also see a screenshot of the demo on the frontpage.

like image 193
Omu Avatar answered Nov 09 '22 04:11

Omu