MapDto

This attribute is a Attribute.
This attribute will specify that an object is mapped to another one.
The uses of this attribute implies that you are using AutoMapper to amp one object to another.

You must add this line to your global.asax
MvcToolbox.ModelMapperManager.Initialize();

  1. You have a Data transfert object (DTO)
namespace EmployeeApp.Transverse.Models
    {
        public class EmployeeDto
        {
            public decimal ID { get; set; }
            public string Gender { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }

        }
    }

  1. You have a model
namespace EmployeeApp.Models
    {
        public class Employee
        {
            public decimal ID { get; set; }
            public string Gender { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }

        }
    }

  1. Add MapDto attribute to your model. This will tell that this model is mapped to another object.
namespace EmployeeApp.Models
{
    [Core.Attributes.MapDto(typeof(EmployeeApp.Transverse.Models.ADO.EmployeeDto))]
    public class Employee
    {
        public decimal ID { get; set; }
        public string Gender { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
     }
}

  1. In your controller, you can now map this two object using AutoMapper
public virtual ActionResult Index()
{
    var dto = EmployeeService.GetAllEmployees();
    EmployeeApp.Web.Models.Employee model = AutoMapper.Mapper.Map(dto, new EmployeeApp.Web.Models.Employee()); 
    return View(NeosMVC.Employee.Views.Index, model);
}


*This example is pretty simple because the mapping between Dto and Model is simple, every property of the Dto match a property of the Model.
For mode complex mapping you can add a method in the model and you can write your own mapping.*
Example:
namespace EmployeeApp.Web.Models
{
    [Core.Attributes.MapDto(typeof(EmployeeApp.Transverse.Models.ADO.EmployeeDto))]
    public class Employee
    {
        public decimal IdEmployee { get; set; }
        public string MyGender { get; set; }
        public string MyFirstName { get; set; }
        public string MyLastName { get; set; }
        
        /// <summary>
        /// Override Default Mapping
        /// </summary>
        private static void Map()
        {
            AutoMapper.Mapper.CreateMap<EmployeeApp.Transverse.Models.ADO.EmployeeDto, Employee>()
                .ForMember(dest => dest.IdEmployee, opt => opt.MapFrom(src => src.ID))
                .ForMember(dest => dest.MyGender, opt => opt.MapFrom(src => src.Gender))
                .ForMember(dest => dest.MyFirstName, opt => opt.MapFrom(src => src.FirstName))
                .ForMember(dest => dest.MyMiddleName, opt => opt.MapFrom(src => src.MiddleName))
                .ForMember(dest => dest.MyLastName, opt => opt.MapFrom(src => src.LastName));
        }
    }
}

Last edited Jun 12, 2013 at 11:44 AM by jefflequeux, version 1

Comments

No comments yet.