Thursday, 13 June 2013

AutoMapper ValueResolver - it's not big and it's not clever but it's handy!!

I had a task where I needed to allow the client to enter a money value in a form field. 

They could enter any of the following:


For example.

I could have just written some code to strip out the special characters but I really wanted to use AutoMapper as it already maps my view models to my domain entities.

Any way I wrote this:

What it does is grabs an array of special characters to strip from my config. (I suppose I could have made it strip out everything that was not a number but for the sake of this post i'll leave it as is.)
It then cycles through the array and strips out the character from the entered amount ($10,000).

It is set up here:

So now every time I map a ChildViewModel to a Child domain entity I get this character stripping for free.
Here is where I use it:

Hope this helps.