将不同的具体类型映射到接口集合

huangapple go评论101阅读模式
英文:

Map Different Concrete Types to Collection of Interface

问题

我在使用AutoMapper将不同的具体类型映射到一个接口集合时遇到困难。例如:

领域模型:

  1. public interface INameInterface {
  2. string Name;
  3. }
  4. public class FullName : INameInterface {
  5. string Name;
  6. }
  7. public class FirstNameOnly : INameInterface {
  8. string Name;
  9. }
  10. public class MyDomain {
  11. List<INameInterface> Names;
  12. }

DTO模型:

  1. public class NameDTO {
  2. int NameType;
  3. string Name;
  4. }
  5. public class MyDTO {
  6. List<NameDTO> NameDTOs;
  7. }

我想将MyDTO映射到MyDomain。我想根据NameDTO的NameType解析它,并将NameType 1映射到FullName,将NameType 2映射到FirstNameOnly的具体类,并将它们放在MyDomain.Names集合中。如何在AutoMapper中实现这个目标?

非常感谢任何帮助。

注:示例已经简化。

英文:

I'm having difficulty in mapping different concrete types to a single collection of interface in automapper. For example:

Domain:

  1. public interface INameInterface {
  2. string Name;
  3. }
  4. public class FullName: INameInterface {
  5. string Name;
  6. }
  7. public class FirstNameOnly: INameInterface {
  8. string Name;
  9. }
  10. public class MyDomain {
  11. List&lt;INameInterface&gt; Names;
  12. }

DTOs:

  1. public class NameDTO {
  2. int NameType;
  3. string Name;
  4. }
  5. public class MyDTO {
  6. List&lt;NameDTO&gt; NameDTOs;
  7. }

I would like to map MyDTO to MyDomain. I would like to resolve NameDTO by its NameType and map NameTypes 1 to Fullname and 2 to FirstNameOnly concrete classes and place them in MyDomain.Names collection. How can I do it in automapper.

Any help is highly appreciated.

PS. Example is simplified

答案1

得分: 1

可以使用自定义类型转换器来解决这个问题。

在automapper配置中:

  1. cfg.CreateMap<NameDTO, INameInterface>()
  2. .ConvertUsing<SomeConverter>();

然后创建自定义转换器类。

  1. public class SomeConverter : ITypeConverter<NameDTO, INameInterface>
  2. {
  3. public INameInterface Convert(NameDTO source, INameInterface destination, ResolutionContext context)
  4. {
  5. if (source.NameType == 0)
  6. {
  7. return context.Mapper.Map<FullName>(source);
  8. }
  9. if (source.NameType == 1)
  10. {
  11. return context.Mapper.Map<FirstNameOnly>(source);
  12. }
  13. return context.Mapper.Map<FirstNameOnly>(source);
  14. }
  15. }

只需为每个具体类型添加必要的映射。

不过需要注意的是,我尝试在投影中使用这种方法,但这不会起作用,因为它无法转换为表达式。

英文:

This can be solved using custom type converter.

In automapper config:

  1. cfg.CreateMap&lt;NameDTO, INameInterface&gt;()
  2. .ConvertUsing&lt;SomeConverter&gt;();

Then create the custom converter class.

  1. public class SomeConverter : ITypeConverter&lt;NameDTO, INameInterface&gt;
  2. {
  3. public INameInterface Convert(NameDTO source, INameInterface destination, ResolutionContext context)
  4. {
  5. if (source.NameType == 0)
  6. {
  7. return context.Mapper.Map&lt;FullName&gt;(source);
  8. }
  9. if (source.NameType == 1)
  10. {
  11. return context.Mapper.Map&lt;FirstNameOnly&gt;(source);
  12. }
  13. return context.Mapper.Map&lt;FirstNameOnly&gt;(source);
  14. }
  15. }

Just add the necessary mapping for each concrete types.

Warning tho, I have tried to use this approach in projection but this won't work as it cannot be converted to an expression.

huangapple
  • 本文由 发表于 2020年1月3日 16:01:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定