0
votes

I am developing a .net core application in which I am using Automapper version 10.0.0

I have the following model class in which I have a Country object. I need to resolve it using CustomIvalue Resolver. When I map from model to domain class I need to map country object while in reverse I am setting string property.

My domain class is:

 public class MongoIdentityUser
    {
        public CompanyInfo Company { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Country Country { get; set; }
        public string City { get; set; }
        public string EncryptedPassword { get; set; }

    }

My Model Class:

public class UserEditModel
    {
        public CompanyInfoModel Company { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Country { get; set; }
        public string City { get; set; }
        public string EncryptedPassword { get; set; }

    }

Here is an automapper setting:

CreateMap<UserEditModel, MongoIdentityUser>()
                 .ForMember(dest => dest.Country, opt => opt.MapFrom<CountryToObjectResolver<UserEditModel, MongoIdentityUser>>())
                 
                .ReverseMap()
                  .ForMember(dest => dest.Country, opt => opt.MapFrom(u => u.Country.Code));

            CreateMap<UserListModel, User>().ReverseMap();

Here is my Custom CountryToObject Resolver:

public class CountryToObjectResolver<TSource, TDestination> : IValueResolver<TSource, TDestination, Country>
    {
        private readonly LookupService _lookupService;
        public CountryToObjectResolver(LookupService lookupService)
        {
            _lookupService = lookupService;

        }

        public Country Resolve(TSource source, TDestination destination, Country destMember, ResolutionContext context)
        {

            var country = source.GetType().GetProperty("Country").GetValue(source, null);
            if (country != null)
                return new Country
                {
                    Name = _lookupService.GetCountryByCode(country.ToString()),
                    Code = country.ToString()
                };
            return null;
        }
    }

It's giving me compile-time error which is

enter image description here

I have the same code in my other project and that is working fine

WHats wrong with the code this time?

1
A repro would help. Make a gist that we can execute and see fail.Lucian Bargaoanu

1 Answers

0
votes

How about:

  private class CountryConverter : ITypeConverter<Country, string>
    { 
      public string Convert(Country source, string destination, ResolutionContext context)
        {...}
    }
    
    this.CreateMap<Country, string>().ConvertUsing<CountryConverter>();
    this.CreateMap<MongoIdentityUser, UserEditModel>()
        .ForMember(dest => dest.Country, opt => opt.MapFrom(src => src.Country)); // this ForMember is probably not necessary even, for the members have the same name