Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why my Null substitution is not working in automapper

Tags:

c#

automapper

I have written a programme in c# and used automapper to map.I have used Null substitution but it is not substituting the given value in result i am getting null only.please check the below code.I want my result to show 8099000078.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Automapper;

namespace Automapper
{
    public class User
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
        public string Mobile { get; set; }
    }
    public class Patient
    {
        public string Name { get; set; }
        public string Location { get; set; }
        public int age { get; set; }
        public string phone { get; set; }


        static void Main(string[] args)
        {
            User obj = new User();
            obj.FirstName = "sujit";
            obj.LastName = "kumar";
            obj.Address = "Bangalore";
            obj.Mobile = "";

            AutoMapper.Mapper.CreateMap<User, Patient>().ForMember(
                emp => emp.Name, map => map.MapFrom(p => p.FirstName + " " + p.LastName))
                .ForMember(dest => dest.Location, source => source.MapFrom(x => x.Address))
               .ForMember(dest => dest.phone, source => source.NullSubstitute("8099000078"));

            var result = AutoMapper.Mapper.Map<User, Patient>(obj);

           // Console.WriteLine(result.Name);
           // Console.WriteLine(result.Location);
            Console.WriteLine(result.phone);
            Console.ReadLine();

        }
    }
}
like image 559
Debendra Dash Avatar asked Oct 26 '25 12:10

Debendra Dash


1 Answers

You missed how null substitute works.

From AutoMapper Wiki:

Null substitution allows you to supply an alternate value for a destination member if the source value is null anywhere along the member chain.

In your code you didn't provide any source for phone property, therefore it doesn't have any value to check on null and it does not perform null substitution.

I made some changes in your code to make it works (see below):

void Main()
{
    User obj = new User();
    obj.FirstName = "sujit";
    obj.LastName = "kumar";
    obj.Address = "Bangalore";
    obj.Mobile = null;

    var config = new MapperConfiguration(cfg => cfg.CreateMap<User, Patient>()
        .ForMember(emp => emp.Name, map => map.MapFrom(p => p.FirstName + " " + p.LastName))
        .ForMember(dest => dest.Location, source => source.MapFrom(x => x.Address))
        .ForMember(dest => dest.phone, source => source.MapFrom(x => x.Mobile))
        .ForMember(dest => dest.phone, source => source.NullSubstitute("8099000078")));

    var mapper = config.CreateMapper();
    var result = mapper.Map<User, Patient>(obj);

    result.Dump();
}

public class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string Mobile { get; set; }
}
public class Patient
{
    public string Name { get; set; }
    public string Location { get; set; }
    public int age { get; set; }
    public string phone { get; set; }
}

Here I provided a source for phone property (I mapped it from Mobile) and set the null value.

like image 178
MaKCbIMKo Avatar answered Oct 28 '25 01:10

MaKCbIMKo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!