mor*_*ols 2 c# ienumerable list automapper protobuf-csharp-port
我想在两个类之间映射:
public class A {
public IEnumerable<C> someList
}
Run Code Online (Sandbox Code Playgroud)
和
public class B {
public RepeatedField<D> someList
}
Run Code Online (Sandbox Code Playgroud)
其中 RepeatedField 是来自 Google.Protobuf.Collections 的一个类,用于处理 gRPC 数据。
编辑:事实证明,gRPC 通过其原型创建类的方式与创建类 B 并不完全一样。请参阅我的答案。
我像这样创建了一个 Automapper MappingConfiguration
return new MapperConfiguration(cfg =>
{
cfg.CreateMap<C, D>().ReverseMap();
cfg.CreateMap<A, B>().ReverseMap();
});
Run Code Online (Sandbox Code Playgroud)
然后它通过 ASP.NET Startup 类注册。
如果我在另一堂课上做这样的事情
A instanceA; // assume A's list has values inside
var listofD = this.mapper.Map<List<D>>(A.someList)
Run Code Online (Sandbox Code Playgroud)
它正确返回一个包含值的列表。然而:
A instanceA; // assume A's list has values inside
B instanceB = this.mapper.Map<B>(A);
Run Code Online (Sandbox Code Playgroud)
返回 B 的一个实例,但 instanceB 中的列表为空。我该如何解决?
您需要创建一个自定义类型转换器来执行转换:
private class EnumerableToRepeatedFieldTypeConverter<TITemSource, TITemDest> : ITypeConverter<IEnumerable<TITemSource>, RepeatedField<TITemDest>>
{
public RepeatedField<TITemDest> Convert(IEnumerable<TITemSource> source, RepeatedField<TITemDest> destination, ResolutionContext context)
{
destination = destination ?? new RepeatedField<TITemDest>();
foreach (var item in source)
{
// obviously we haven't performed the mapping for the item yet
// since AutoMapper didn't recognise the list conversion
// so we need to map the item here and then add it to the new
// collection
destination.Add(context.Mapper.Map<TITemDest>(item));
}
return destination;
}
}
Run Code Online (Sandbox Code Playgroud)
另一种方式,如果需要:
private class RepeatedFieldToListTypeConverter<TITemSource, TITemDest> : ITypeConverter<RepeatedField<TITemSource>, List<TITemDest>>
{
public List<TITemDest> Convert(RepeatedField<TITemSource> source, List<TITemDest> destination, ResolutionContext context)
{
destination = destination ?? new List<TITemDest>();
foreach (var item in source)
{
destination.Add(context.Mapper.Map<TITemDest>(item));
}
return destination;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以像这样注册:
ce.CreateMap(typeof(IEnumerable<>), typeof(RepeatedField<>)).ConvertUsing(typeof(EnumerableToRepeatedFieldTypeConverter<,>));
ce.CreateMap(typeof(RepeatedField<>), typeof(List<>)).ConvertUsing(typeof(RepeatedFieldToListTypeConverter<,>));
Run Code Online (Sandbox Code Playgroud)
mor*_*ols -1
我已经解决了这个问题。
C# 类中的 Google.Protobuf.Collections.RepeatedField 是只读的,这意味着直接向其中赋值是行不通的,并且只会在返回时返回一个空列表。因此,我在两个较大的类之间创建了一个自定义类型转换器,以将它们组合在一起。它的作用是将值直接添加到 RepeatedField 中,而不是填充我自己的 RepeatedField 并将值分配给类。
public static class mapConfig
{
public static ContainerBuilder RegisterObjectMappers(this ContainerBuilder builder)
{
builder.Register(c => GetV1MapperConfiguration().CreateMapper())
.As<IMapper>().SingleInstance();
return builder;
}
private static MapperConfiguration GetMapConfig()
{
return new MapperConfiguration(cfg =>
{
// some mappings here
cfg.CreateMap<C, D>().ReverseMap();
cfg.CreateMap<A, B>().ConvertUsing<AToBConverter>();
});
}
}
public class AToBConverter : ITypeConverter<A, B>
{
public B Convert(A source, B destination, ResolutionContext context)
{
var b = new B
{
// internal values here aside from the repeated field(s)
};
// Need to use the Add method to add values rather than assign it with an '=' sign
foreach (var someValue in source.someList)
{
b.someList.Add(context.Mapper.Map<D>(someValue));
}
return b;
}
}
Run Code Online (Sandbox Code Playgroud)
从 RepeatedField 转换为 List 或映射类中的任何其他 IEnumerable 没有任何麻烦,并且不需要另一个转换器。