This project has moved and is read-only. For the latest updates, please go here.

Custom Mapping a Property

  • Start by opening the result of the DIY: Simple Scenario (either do it yourself or download it)
  • Suppose we want the last name of a person displayed in capitals but always send it back to the model in lower case starting with a single upper case. (This might not be the most compelling example but remember this is just an example to show a feature of the MVMMapper)
  • Open the Person.xml mapping file and change the Property element of the LastName property like this:
      <Property name="LastName"
        <ModelMapping isCustom="true"/>
  • Hit the Transform All Templates button. If you want to you can examine the Person.g.cs file to see what was generated.
  • Add a C# class Person to the ViewModel folder (Person.cs)
namespace PersonLookup.ViewModel
    public partial class Person
        partial void MapLastNameFromModel(PersonServiceReference.Person model)
            this.LastName = model.LastName.ToUpper();

        partial void MapLastNameToModel(PersonServiceReference.Person model)
            model.LastName = this.LastName.Substring(0,1).ToUpper() + this.LastName.Substring(1).ToLower();
  • The MapLastNameFromModel method is called when the Person ViewModel class is being filled. In this method you can do anything you would like to do to fill the LastName property. The model parameter refers to the Model object that is being used to load the Person ViewModel class with data. These partial methods MapPropertyNameFromModel will be called in the same order that the properties are specified in the mapping file. So in this case the FirstName property has already been filled (either automatically or by custom mapping method).
  • The MapLastNameToModel method is called when a Model object is being filled with data from the ViewModel. So in this method you should write code that passes the data from the LastName property to the Model class.

Last edited Jun 12, 2010 at 12:54 PM by Erno_de_Weerd, version 1


No comments yet.