Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自动映射避免嵌套循环_C#_Automapper_Dto - Fatal编程技术网

C# 自动映射避免嵌套循环

C# 自动映射避免嵌套循环,c#,automapper,dto,C#,Automapper,Dto,我目前正在使用Automapper将我的域模型映射到DTO。在我的内心 域模型I有3个布尔属性(IsHomeowner,IsTenant,IsLivingWithParents),用于设置属性的值 在视图模型内部,即PersonDTO调用LivingStatus 然而,为了得到最终结果,我必须循环使用Person模型,创建字典 要存储我的值,然后使用AfterMap创建一个嵌套循环并在其中设置值 虽然它可以工作,但不是一个理想的解决方案,因为随着数据的增加,它更有可能造成内存泄漏 在尺寸上 所以

我目前正在使用Automapper将我的域模型映射到DTO。在我的内心 域模型I有3个布尔属性(
IsHomeowner
IsTenant
IsLivingWithParents
),用于设置属性的值 在视图模型内部,即
PersonDTO
调用
LivingStatus

然而,为了得到最终结果,我必须循环使用
Person
模型,创建字典 要存储我的值,然后使用
AfterMap
创建一个嵌套循环并在其中设置值

虽然它可以工作,但不是一个理想的解决方案,因为随着数据的增加,它更有可能造成内存泄漏 在尺寸上

所以我想知道AutoMapper中是否有任何东西可以避免这种情况

这是我的密码

查看模型

public class PersonDTO{
  public int Id { get; set; }
  public string FirstName { get; set; }
  public string Surname { get; set; }
  public Status LivingStatus { get; set; }
}
public class Person{
   public int Id { get; set; }
   public string FirstName { get; set; }
   public string Surname { get; set; }
   public bool IsHomeOwner { get; set; }
   public bool IsTenant { get; set; }
   public bool IsLivingWithParents { get; set; }
}

public enum Status{
   Homeowner=1,
   Tenant=2,
   LivingWithParents=3
}
域模型

public class PersonDTO{
  public int Id { get; set; }
  public string FirstName { get; set; }
  public string Surname { get; set; }
  public Status LivingStatus { get; set; }
}
public class Person{
   public int Id { get; set; }
   public string FirstName { get; set; }
   public string Surname { get; set; }
   public bool IsHomeOwner { get; set; }
   public bool IsTenant { get; set; }
   public bool IsLivingWithParents { get; set; }
}

public enum Status{
   Homeowner=1,
   Tenant=2,
   LivingWithParents=3
}
public List GetEmployee(列出人员)
{
var livingStatus=新字典();
foreach(var个人对个人)
{
if(person.IsHomeOwner)
{
livingStatus.Add(person.Id,Status.Homeowner);
}
else if(person.IsTenant)
{
livingStatus.Add(person.Id,Status.Tenant);
}
其他的
{
livingStatus.Add(person.Id,Status.livingtheparents);
}
}
返回_mapper.Map(persons,opts=>opts.AfterMap((src,dest){
foreach(目标中的var人员)
{
person.LivingStatus=LivingStatus.Single(x=>x.Key==person.Id).Value;
}
}));
}

您可以避免所有这些,并以一种方式扩展PersonDto,使值映射为它们应该映射的值,或者您可以为此编写自己的解析器 也许这不是一个最好的方法,把一个逻辑在一个setter

public class PersonDto
  {
    private Status status;
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string Surname { get; set; }

    public Status LivingStatus
    {
      get => status;

      set
      {
        status = value;

        switch (status)
        {
          case Status.Homeowner:
            IsHomeOwner = true;
            IsTenant = false;
            IsLivingWithParents = false;
            break;

          case Status.LivingWithParents:
            IsHomeOwner = false;
            IsTenant = false;
            IsLivingWithParents = true;
            break;

          case Status.Tenant:
            IsHomeOwner = false;
            IsTenant = true;
            IsLivingWithParents = false;
            break;

          default:
            throw new ArgumentOutOfRangeException();
        }
      }
    }


    public bool IsHomeOwner { get; set; }
    public bool IsTenant { get; set; }
    public bool IsLivingWithParents { get; set; }
  }

让我知道你是否喜欢解决方案与解析器。我会尽力帮助您。

通过创建一个处理转换的
方法,然后在映射配置中使用它,最终找到了一个更好的解决方案:)

映射配置

CreateMap<Person, PersonDTO>()
                .ForMember(dest => dest.LivingStatus, opt => opt.MapFrom(src => TypeConverter(src)));
CreateMap()
.ForMember(dest=>dest.LivingStatus,opt=>opt.MapFrom(src=>TypeConverter(src));

您说“它可以工作”,但它甚至不能编译
persons
是一个
列表
,但您正在访问单个
Person
上存在的属性,如
IsHomeOwner
?Good spot@stuartd我在键入示例时错过了它:)立即排序为什么不在映射期间为特定属性使用自定义值解析程序?谢谢您的建议。但是,我不确定将逻辑放在setter中是否是更好的解决方案。它不会为
单元测试提供更好的依据