Home > Software engineering >  Function not working inside MapFrom() in AutoMapper
Function not working inside MapFrom() in AutoMapper

Time:09-23

I have this MapFrom() where I need to check some logic and this is what I am doing and it works fine

.ForMember(c => c.res, opt => opt.MapFrom(src => src.Result.condition1 == "Pass" && src.Result.condition2 == "Pass" ? "Pass" : null))

But the issue is that I need to do some complex condition checking so I tried using extension method like this

public static class Extensions
    {
        public static string SResultData(this string SResult,domainEntityModel src)
        {
            if (src.Condition1 == "Pass" && src.Condition2 == "Pass")
                SResult = "Pass";
            return SResult;
        }
}

Unfortunately above method doesn't works and throws exception So I tried doing this

.ForMember(c => c.SResult, opt =>
            {
                opt.MapFrom((src, c) =>
                {
                    if (src?.Result?.Condition1 == "Pass" && src?.Result?.Condition2== "Pass")
                    {
                        return "Pass";
                    }
                    else
                    {
                        return null ;
                    }
                });
            })

I also tried using this

 public class SResultValueResolver : IValueResolver<PreResultModel,ResultModel, string>
    {
        public string Resolve(PreResultModel source, ResultModel destination, string destMember, ResolutionContext context)
        {
            if (source?.Result?.Condition1 == "Pass" && source?.Result?.Condition2 == "Pass")
                return "Pass";
            else
                return null;
        }
    }
    public class CustomResolver : IMemberValueResolver<PreResultModel, ResultModel, string, string>
    {
        public string Resolve(PreResultModel source, ResultModel destination, string sourceMember, string destinationMember, ResolutionContext context)
        {
            if (source?.Result?.Condition1 == "Pass" && source?.Result?.Condition2 == "Pass")
                return "Pass";
            else
                return null;
        }
    }

None of these seems to work i always get error of automapper unable to map or error like the query made was incorrect

EDIT:- These are the classes

 public class PreResultModel
    {
        public Guid Id { get; set; }
        public Domain.Entities.DomainModel Result { get; set; }
    }
        public class DomainModel : Entity
    {
        public Guid Id { get; set; }
        public string QuestionsResult { get; set; }
        public string TempResult { get; set; }
       
        public string? ResultCombinedSOF() =>
    QuestionsResult == "Pass" && TempResult == "Pass" ? "Pass" : null;
        public string ResultCombined()
        {
            if (QuestionsResult == "Pass" && TempResult == "Pass")
            {
                return "Pass";
            }
            else
                return null;
        }
    }
    public class ResultModel
    {
        public Guid? Id { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string QuestionsResult { get; set; } = "NA";

        public string SResult { get; set; }

        public string TempResult { get; set; } = "NA";

    }

and this is how i am creating mapping

 CreateMap<PreResultModel, ResultModel>()

CodePudding user response:

Can you add members to your domainEntityModel? If so you could do something like:

public class domainEntityModel {
  // other members

  public string? ResultCombined() => 
    Result.condition1 == "Pass" && Result.condition2 == "Pass" ? "Pass" : null;
}

Then in your mapping config:

//...
.ForMember(c => c.res, opt => opt.MapFrom(src => src.ResultCombined()))

CodePudding user response:

Your extension method looks wrong. It should look like this:

public static class Extensions
{
    public static string SResultData(this PreResultModel src)
    {
        if (src.Result.Condition1 == "Pass" && src.Result.Condition2 == "Pass")
        {
            return "Pass";
        }

        return null;
    }
}

And in your mapping profile:

CreateMap<PreResultModel, ResultModel>()
    .ForMember(dest => dest.SResult, opt => opt.MapFrom(src => src.SResultData()));
  • Related