AutoMapper - Profiles

Profiles is another great feature from AutoMapper which helps you to mappings and it comes in handy when we need to apply different mapping rules for the same object. Let's take the case of a DateTime field being mapped to a String field and the requirements can be different based on the business requirements .

In the below example I have a source model which has got a DateTime field and the destination model with a string property. In one instance I needs to show the data in mm/dd/yyyy format and another case I need to show only month and year.

using System;
using AutoMapper;

namespace AutoMapperSamples.Configuration
{
    namespace Profiles
    {
        public class DateTimeProfile1 : Profile
        {
            protected override void Configure()
            {
                CreateMap<DateTime, String>().ConvertUsing<DateTimeToString1TypeConverter>();
            }
        }

        public class DateTimeProfile2 : Profile
        {
            protected override void Configure()
            {
                CreateMap<DateTime, String>().ConvertUsing<DateTimeToString2TypeConverter>();
            }

        }

        public class DateTimeToString1TypeConverter : ITypeConverter<DateTime, String>
        {
            public string Convert(ResolutionContext context)
            {
                return DateTime.Parse(((object)context.SourceValue).ToString()).ToString("dd/MMM/yyyy");
            }
        }

        public class DateTimeToString2TypeConverter : ITypeConverter<DateTime, String>
        {
            public string Convert(ResolutionContext context)
            {
                return DateTime.Parse(((object)context.SourceValue).ToString()).ToString("MMMM - yyyy");
            }
        }

        public class Model1
        {
            public DateTime StartedOn { get; set; }
        }

        

        public class ViewModel
        {
            public String StartedOn { get; set; }
        }


        public class TestProfile
        {

            public static void Main()
            {
                var config1 = new MapperConfiguration(cfg =>
                {
                    cfg.AddProfile<DateTimeProfile1>();
                    cfg.CreateMap<Model1, ViewModel>();
                });

                var config2 = new MapperConfiguration(cfg1 =>
                {
                    cfg1.AddProfile<DateTimeProfile2>();
                    cfg1.CreateMap<Model1, ViewModel>();
                });

                var model1 = new Model1{ StartedOn = DateTime.Now};
                
                var viewModel1 = config1.CreateMapper().Map<ViewModel>(model1);
                Console.WriteLine(viewModel1.StartedOn);
                viewModel2 = config2.CreateMapper().Map< ViewModel>(model1);
                Console.WriteLine(viewModel2.StartedOn);

                
                Console.WriteLine(viewModel2.StartedOn);
                Console.ReadKey();
            }
        }
    }
}

Here I have created two profiles by inheriting the Profile class for storing our mappings. The mapping is created inside the Profile class by overriding the Convert method. Also I have created two custom type converters for converting from DateTime to String in different format. One will return the string in dd/MMM/yyyy format where as the other one will return in MMMM - yyyy format.

Now in our main class I have created two mapping configuration for holding these profiles and calls the Map method using these configurations. The output will be 

As you can see from the output we were able to apply different rules for the mapping with with very little code. Both our source and destination objects were same for both the mappings yet we got different value in the destination model.

The profiles will come handy when we want to segregate mapping based on your business requirements.


No Comments

Add a Comment

Recent Posts