UseValue ResolveUsing and Null Substitution Using AutoMapper in C#

UseValue ResolveUsing and Null Substitution Using AutoMapper in C#

In this article, I am going to discuss the UseValue ResolveUsing and Null Substitution Using AutoMapper in C# with some example. Please read our previous article before proceeding to this article where we discussed the AutoMapper Ignore Property in C# with an example.

Understanding the AutoMapper UseValue() and ResolveUsing() methods

The AutoMapper UseValue() method is used to retrieve a value on the first-run and stores it in the mapping whereas the ResolveUsing() method resolves at run-time.

Let us understand the above AutoMapper UseValue() and ResolveUsing() methods with an example
using AutoMapper;
namespace AutoMapperDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            InitializeAutomapper();

            A aObj = new A()
            {
                Name = "Pranaya",
                AAddress = "Mumbai"
            };

            var bObj = Mapper.Map<A, B>(aObj);
            Console.WriteLine("After Mapping : ");

            //Here FixedValue and DOJ will be empty for aObj
            Console.WriteLine("aObj.Member : " + aObj.Name + ", aObj.FixedValue : " + aObj.FixedValue + ", aObj.DOJ : " + aObj.DOJ + ", aObj.AAddress : " + aObj.AAddress);
            Console.WriteLine("bObj.Member : " + bObj.Name + ", bObj.FixedValue : " + bObj.FixedValue + ", bObj.DOJ : " + bObj.DOJ + ", bObj.BAddress : " + bObj.BAddress);
            
            bObj.Name = "Rout";
            bObj.BAddress = "Delhi";
            Mapper.Map(bObj, aObj);

            Console.WriteLine("After ReverseMap : ");
            Console.WriteLine("aObj.Member : " + aObj.Name + ", aObj.FixedValue : " + aObj.FixedValue + ", aObj.DOJ : " + aObj.DOJ + ", aObj.AAddress : " + aObj.AAddress);
            Console.WriteLine("bObj.Member : " + bObj.Name + ", bObj.FixedValue : " + bObj.FixedValue + ", bObj.DOJ : " + bObj.DOJ + ", bObj.BAddress : " + bObj.BAddress);

            Console.ReadLine();
        }

        static void InitializeAutomapper()
        {
            Mapper.Initialize(config =>
            {
                config.CreateMap<A, B>()
                    .ForMember(dest => dest.BAddress, act => act.MapFrom(src => src.AAddress))

                    //To Store Static Value use the UseValue() method
                    .ForMember(dest => dest.FixedValue, act => act.UseValue("Hello"))

                    //To Store DateTime value use ResolveUsing() method
                    .ForMember(dest => dest.DOJ, act => act.ResolveUsing(src =>
                    {
                        return DateTime.Now;
                    }))

                    .ReverseMap();
            });
        }
    }

    public class A
    {
        public string Name { get; set; }
        public string AAddress { get; set; }
        public string FixedValue { get; set; }
        public DateTime DOJ { get; set; }
    }

    public class B
    {
        public string Name { get; set; }
        public string BAddress { get; set; }
        public string FixedValue { get; set; }
        public DateTime DOJ { get; set; }
    }
}

When you run the application, it gives you the below output.

UseValue ResolveUsing and Null Substitution Using AutoMapper in C#

Note: Use the MapFrom(s => s.MemberName) when you return the actual source object member whereas you need to use the UseValue() method if you want to map a static value in the mapping, and you know the value itself won’t change.

Use ResolveUsing(s => {}) when you want to resolve a destination field from a derived value. This should be used for any DateTime mappings and any more complicated mapping functions.

Null Substitution in Automapper:

The Null substitution allows us to supply an alternate value for a destination member if the source value is null. That means instead of mapping the null value from the source object, it will map from the value we supply. We need to use the NullSubstitute() method to substitute the null value using AutoMapper.

Let us understand how to use Null Substitution using AutoMapper with an example

using AutoMapper;
namespace AutoMapperDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            InitializeAutomapper();

            A aObj = new A()
            {
                Name = "Pranaya",
                AAddress = null
            };

            var bObj = Mapper.Map<A, B>(aObj);
            Console.WriteLine("After Mapping : ");

            //Here FixedValue and DOJ will be empty for aObj
            Console.WriteLine("aObj.Member : " + aObj.Name + ", aObj.FixedValue : " + aObj.FixedValue + ", aObj.AAddress : " + aObj.AAddress);
            Console.WriteLine("bObj.Member : " + bObj.Name + ", bObj.FixedValue : " + bObj.FixedValue + ", bObj.BAddress : " + bObj.BAddress);

            Console.ReadLine();
        }

        static void InitializeAutomapper()
        {
            Mapper.Initialize(config =>
            {
                config.CreateMap<A, B>()
                    .ForMember(dest => dest.BAddress, act => act.MapFrom(src => src.AAddress))
                    //You need to use NullSubstitute method to substitute null value
                    .ForMember(dest => dest.FixedValue, act => act.NullSubstitute("Hello"))
                    .ForMember(dest => dest.BAddress, act => act.NullSubstitute("N/A"));
            });
        }
    }

    public class A
    {
        public string Name { get; set; }
        public string AAddress { get; set; }
        public string FixedValue { get; set; }
    }

    public class B
    {
        public string Name { get; set; }
        public string BAddress { get; set; }
        public string FixedValue { get; set; }
    }
}

When we run the application, it will give us the below output.

UseValue ResolveUsing and Null Substitution Using AutoMapper in C#

SUMMARY:

In this article, I try to explain UseValue ResolveUsing and Null Substitution Using AutoMapper in C# with some examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *