代码之家  ›  专栏  ›  技术社区  ›  TorbenJ

protobuf重复场<T>型的映射

  •  0
  • TorbenJ  · 技术社区  · 7 年前

    class StudentCourse
    {
        public int StudentId { get; set; }
        public int CourseId { get; set; }
    }
    
    class Student
    {
        public virtual ICollection<StudentCourse> Courses { get; set; }
    }
    

    Protobuf 消息

    message StudentDto {
        repeated int32 courseIds = 1;
    }
    

    我怎样才能绘制地图 Student.Courses StudentDto.CourseIds 使用 AutoMapper ?

    class StudentProfile : Profile
    {
        class Converter : IValueConverter<ICollection<StudentCourse>, RepeatedField<int>>
        {
            public RepeatedField<int> Convert(ICollection<StudentCourse> sourceMember, ResolutionContext context)
            {
                return new RepeatedField<int> { sourceMember.Select(x => x.CourseId) };
            }
        }
    
        public StudentProfile()
        {
            CreateMap<Student, StudentDto>()
                .ForMember(dst => dst.CourseIds, o => o.MapFrom(src => src.Courses.Select(x => x.CourseId)));
                //.ConvertUsing(student => new StudentDto{CourseIds = { student.Courses.Select(x => x.CourseId)}});
                //.ForMember(dst => dst.CourseIds, o => o.ConvertUsing(new Converter(), src => src.Courses));
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            var student = new Student
            {
                Courses = new List<StudentCourse>
                {
                    new StudentCourse { CourseId = 1, StudentId  = 1 },
                    new StudentCourse { CourseId = 6, StudentId  = 1 },
                    new StudentCourse { CourseId = 11, StudentId = 1 }
                }
            };
    
            IMapper mapper = new Mapper(new MapperConfiguration(cfg => cfg.AddProfile(new StudentProfile())));
    
            var mapped = mapper.Map<StudentDto>(student);
            // Expecting: {{ "courseIds": [ 1, 6, 11 ] }}
            // Actually: {{ }}
        }
    }
    

    ConvertUsing 声明 StudentProfile 工作和映射成员是正确的,但由于我在我的项目中使用的类要复杂得多,这是我真正想使用的最后一个选项。

    你们中有人知道这个问题的原因和解决办法吗?
    Google.Protobuf 3.7.0 AutoMapper 8.0.0

    编辑:

    (src, dest, ctxt) =>
    {
        StudentDto typeMapDestination;
        return (src == null)
            ? null
            : {
                typeMapDestination = dest ?? new StudentDto();
                try
                {
                    var resolvedValue =
                    {
                        try
                        {
                            return ((src == null) || ((src.Courses == null) || false))
                                ? null
                                : src.Courses.Select(x => x.CourseId);
                        }
                        catch (NullReferenceException)
                        {
                            return null;
                        }
                        catch (ArgumentNullException)
                        {
                            return null;
                        }
                    };
    
                    var propertyValue = (resolvedValue == null)
                        ? {
                            var collectionDestination = (ICollection<int>)((dest == null) ? null : typeMapDestination.CourseIds);
    
                            if ((collectionDestination == null) || collectionDestination.IsReadOnly)
                            {
                            }
                            else
                            {
                                collectionDestination.Clear();
                            }
    
                            return new RepeatedField<int>();
                        }
                        : {
                            var passedDestination = (dest == null) ? null : typeMapDestination.CourseIds;
                            var collectionDestination = ((passedDestination == null) || ((ICollection<int>)passedDestination).IsReadOnly)
                                ? new RepeatedField<int>()
                                : passedDestination;
                            collectionDestination.Clear();
    
                            var enumerator = resolvedValue.GetEnumerator();
                            while (true)
                            {
                                if (enumerator.MoveNext())
                                {
                                    var item = enumerator.Current;
                                    collectionDestination.Add(item);
                                }
                                else
                                {
                                    break;
                                }
                            }
    
                            return collectionDestination;
                        };
    
                    return propertyValue;
                }
                catch (Exception ex)
                {
                    throw new AutoMapperMappingException(
                        "Error mapping types.",
                        ex,
                        AutoMapper.TypePair,
                        TypeMap,
                        PropertyMap);
    
                    return null;
                }
    
                return typeMapDestination;
            };
    };
    
    0 回复  |  直到 7 年前