Auto mapper 3 เข้าถึงผู้รับปลายทาง

ฉันมีแผนที่ซึ่งละเว้นรายการที่ไม่ได้แมปทั้งหมดในออบเจ็กต์ปลายทาง แต่ด้วยเหตุผลบางอย่างมันกำลังเข้าถึง getter ของรายการปลายทางที่แมปซึ่งจะส่งข้อยกเว้นค่า Null แผนที่ของผมมีดังนี้

Mapper.CreateMap<A, Entities.B>()
.IgnoreAllUnmapped()
.ForMember(d => d.Registration, opt => opt.MapFrom(s => s.Registration))
.ForMember(d => d.VIN, opt => opt.MapFrom(s => s.Vin))
.ForMember(d => d.MonthReg, opt => opt.MapFrom(s => s.MonthOfRegistration))
.ForMember(d => d.YearReg, opt => opt.MapFrom(s => s.YearOfRegistration))
.ForMember(d => d.BodyColour, opt => opt.MapFrom(s => s.BodyColour));

กำลังส่งข้อยกเว้นค่า Null บน d.MonthReg ในเมธอด get ของไอเท็มนั้น แต่ไม่ควรเรียกมันเนื่องจากฉันกำลังพยายามตั้งค่า

ฉันจะแก้ไขปัญหานี้ได้อย่างไร?

นี่คือข้อมูลโค้ด:

public class A
{
    public string Vin { get; set; }
    public string Registration { get; set; }
    public int? MonthOfRegistration { get; set; }
    public int? YearOfRegistration { get; set; }
    public string BodyColour { get; set; }
}

public class Entities.B
{
    public string VIN
    {
      get
      {
        return this.Attributes["VIN"].Value as string;
      }
      set
      {
        this.Attributes["VIN"].Value = (object) value;
      }
    }
    public string Registration
    {
      get
      {
        return this.Attributes["Registration"].Value as string;
      }
      set
      {
        this.Attributes["Registration"].Value = (object) value;
      }
    }
    public int MonthReg
    {
      get
      {
        return (int) this.Attributes["MonthReg"].Value;
      }
      set
      {
        this.Attributes["MonthReg"].Value = (object) value;
      }
    }
    public int YearReg
    {
      get
      {
        return (int) this.Attributes["YearReg"].Value;
      }
      set
      {
        this.Attributes["YearReg"].Value = (object) value;
      }
    }
    public string BodyColour
    {
      get
      {
        return this.Attributes["BodyColour"].Value as string;
      }
      set
      {
        this.Attributes["BodyColour"].Value = (object) value;
      }
    }
}

เกิดข้อผิดพลาด

Test method TestMappingV1 threw exception: 
AutoMapper.AutoMapperMappingException: 

Mapping types:
A-> B
A -> B

Destination path:
B

Source value:
A ---> System.NullReferenceException: Object reference not set to an instance of an object.

at B.get_MonthReg() in source file
at lambda_method(Closure, Object)
at AutoMapper.Impl.PropertyGetter.GetValue(Object source)
at AutoMapper.PropertyMap.GetDestinationValue(Object mappedObject)
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap)
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.Map(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)

รหัสที่กำลังเรียกใช้คือ

Mapper.CreateMap<Vehicle, Entities.Vehicle>()
.IgnoreAllUnmapped()
.ForMember(d => d.Registration, opt => opt.MapFrom(s => s.Registration))
.ForMember(d => d.VIN, opt => opt.MapFrom(s => s.Vin))
.ForMember(d => d.MonthReg, opt => opt.MapFrom(s => s.MonthOfRegistration))
.ForMember(d => d.YearReg, opt => opt.MapFrom(s => s.YearOfRegistration))
.ForMember(d => d.BodyColour, opt => opt.MapFrom(s => s.BodyColour));

Mapper.CreateMap<Assessment, Entities.Assessment>()
.IgnoreAllUnmapped()
.BeforeMap((s, d) =>
{
    if (d.IsNull.State)
    {
        d.State = Entities.AssessmentStateEnum.Uninitialised;
    }
})
.ForMember(d => d.AssessmentNumber, opt => opt.MapFrom(s => s.AssessmentNumber))
.ForMember(d => d.State, opt => opt.MapFrom(s => s.State))
.ForMember(d => d.Vehicle, opt => opt.MapFrom(s => s.Vehicle != null ? Mapper.Map<A, Entities.B>(s.Vehicle) : null))    


[TestMethod]
public void TestMappingV1()
{
    Interface.v1.Assessment assessment =
        new Interface.v1.Assessment
        {
            AssessmentNumber = "KDCTEST",
            State = "2",

            Vehicle = new A
            {
                Registration = "ET53 LCO",
                Vin = "VIN123456789",
                MonthOfRegistration = 12,
                YearOfRegistration = 2013
            }
        };

        Mapper.Map(assessment, _assessment);

}

public static IMappingExpression<TSource, TDest> IgnoreAllUnmapped<TSource, TDest>(
this IMappingExpression<TSource, TDest> expression)
{
    expression.ForAllMembers(opt => opt.Ignore());
    return expression;
}

person Ken Cowley    schedule 17.03.2014    source แหล่งที่มา
comment
คุณสามารถระบุคำจำกัดความของคลาส A และ B ได้หรือไม่ รวมถึงข้อมูลที่ทำให้เกิดข้อยกเว้นระหว่างการทำแผนที่   -  person Sergey Berezovskiy    schedule 17.03.2014
comment
ได้เพิ่มรหัสและข้อผิดพลาดในรายการต้นฉบับ   -  person Ken Cowley    schedule 17.03.2014
comment
คุณสามารถแสดงรหัสที่คุณกำลังดำเนินการเมื่อมีข้อยกเว้นเกิดขึ้นได้หรือไม่? สิ่งนี้ไม่ชัดเจนกับสิ่งที่คุณให้ไว้และไม่สามารถทำซ้ำได้สำหรับผู้อ่าน   -  person Ed Chapel    schedule 17.03.2014
comment
ได้เพิ่มโค้ดที่กำลังทำงานอยู่ เกิดข้อผิดพลาดเมื่อทำการแมป และวัตถุลูกของ B กำลังพยายามแมป วัตถุลูกใน B ไม่มีอยู่และไม่เป็นโมฆะ   -  person Ken Cowley    schedule 17.03.2014
comment
@KenCowley Attributes คืออะไร - คุณยังไม่ได้เพิ่มคำจำกัดความ แล้ว IgnoreAllUnmaped คืออะไร?   -  person Sergey Berezovskiy    schedule 17.03.2014
comment
ได้เพิ่ม def สำหรับ IgnoreAllUnmapped แล้ว Attributes คือชุดของคู่ค่าชื่อ (รวมถึงข้อมูลอื่น ๆ ) ฉันไม่สามารถใส่รหัสทั้งหมดที่นี่ได้ คุณลักษณะและ IgnoreAllUnmapped ไม่ใช่ปัญหาเนื่องจากทำงานได้อย่างถูกต้อง เป็นเพียงว่าผู้ทำแผนที่กำลังเรียกรับเมื่อไม่ควร   -  person Ken Cowley    schedule 17.03.2014


คำตอบ (1)


ตามคำถามเดียวกันบน Github ดูเหมือนว่าไม่พบวิธีแก้ปัญหาสำหรับปัญหาดังกล่าว

การอ้างอิง ผู้เขียนไลบรารี:

อย่าคิดว่าฉันจะเข้าถึงสิ่งนี้ได้ เมื่อดูที่ซอร์สโค้ดแล้ว ดูเหมือนว่าการดำเนินการที่เหมาะสมคือการมีผู้ได้รับที่ไม่โยน

person superjos    schedule 20.07.2016