Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.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# 使用Automapper从DTO属性填充实体类中的列表_C#_Automapper - Fatal编程技术网

C# 使用Automapper从DTO属性填充实体类中的列表

C# 使用Automapper从DTO属性填充实体类中的列表,c#,automapper,C#,Automapper,我对使用AutoMapper非常陌生,我花了几天时间在谷歌上搜索解决方案,但运气不佳。我正在尝试将一个DTO(如下所示)映射到我的实体类,该类包含多个级别的信息年、季、季、期、周、日 这是我的DTO: public class CalendarDto { public int Year { get; set; } public int Quarter { get; set; } public string Season { get; set; } public i

我对使用AutoMapper非常陌生,我花了几天时间在谷歌上搜索解决方案,但运气不佳。我正在尝试将一个DTO(如下所示)映射到我的实体类,该类包含多个级别的信息年、季、季、期、周、日

这是我的DTO:

public class CalendarDto
{
    public int Year { get; set; }
    public int Quarter { get; set; }
    public string Season { get; set; }
    public int DayOfWeek { get; set; }
    public string Date { get; set; }
    public int WeekOfYear { get; set; }
    public int DayOfYear { get; set; }
    public int WeekOfPeriod { get; set; }
}
以下是我的实体的前两个级别:

    public class Calendar : ICalendar<string>
    {
        public string Id { get; set; }
        public int Year { get; set; }
        public string Date { get; set; }
        public CalendarQuarter Quarters { get; set; }
    }

   public class CalendarQuarter
    {
        public int Year { get; set; }
        public int Quarter { get; set; }
        public string Date { get; set; }
        public List<CalendarSeason> Seasons { get; set; }
    }
正如你们所看到的,我有额外的筑巢水平,从一年到一个季度,从一个季节到一个时期,从一周到一天。我只提供了第一个级别,每年到每个季度,因为我确信一旦我找到了第一个级别,其余的将是直接的

我尝试将概要文件设置为从DTO分别映射每个类,如下所示,因为所有属性值都匹配

public class CalendarProfile : Profile
{
    public CalendarProfile()
    {
        CreateMap<CalendarDto, Calendar>().ReverseMap();
        CreateMap<CalendarDto, CalendarQuarter>().ReverseMap();
        CreateMap<CalendarDto, CalendarSeason>().ReverseMap();
        CreateMap<CalendarDto, CalendarPeriod>().ReverseMap();
        CreateMap<CalendarDto, CalendarWeek>().ReverseMap();
        CreateMap<CalendarDto, CalendarDay>().ReverseMap();
    }
}
当我尝试将DTO映射到处理程序中的实体时,只映射Calendar类,CalendarQuarters列表为空。据我所知,配置文件与配置文件相同,在配置文件中创建的所有映射都将在执行映射时执行。显然,我要么错了,要么映射设置不正确,要么两者都错了!哈哈

var calendar = _mapper.Map<CalendarDto, Calendar>(parms.Dto);

如有任何建议,将不胜感激。谢谢你的帮助

首先,重要的是,使用automapper可以使映射看起来更容易,但是调试代码是不可能的,当您的项目变大时,您可能会遇到问题,而且映射并不是那么复杂,您可以始终显式地映射并轻松地进行调试。这很无聊,所以在决定使用它之前,最好先记住这一点,阅读以下文章:

如果使用automapper,您应该阅读。我认为你需要这样的东西:

CreateMap.FormMemberDest=>dest.Quarter.Quarter, opt=>opt.Quarter .FormMemberDest=>dest.Quarter.Quarter.Seasons.Seasons 选择=>选择季节;
我删除了反向映射,因为我从项目中复制了它并更改了属性,我希望您可以应用于所有属性。当然,您有许多属性,因此可以链接FormMember并定义所有映射,如果您不这样做,则映射器仅映射具有相同名称的属性。

切勿以这种方式使用automaper。Automper的一位作者说,Automper设计用于从实体映射到DTO或VM。从不从数据到实体。您应该使用构造函数或任何设计模式,例如factory。在项目成长过程中,以这种方式使用automaper会给您带来很多问题。在大型商业项目中,永远不要使用automaper。它将是不可维护的,尤其是当DTO的结构与实体不同时。好的,我看到了评论,但你能详细说明一下吗?我在大学里和同事们一起工作,我们成功地完成了这个项目,我们有很多实体和DTO。这里的区别是,在这种情况下,有很多层次的实体,但我不认为这是一个大问题,因为你的项目持续了一年。我希望你不要在10年内尝试调试基于Automper的应用程序;。有很多文章。所以问题是在实践中,它变得非常丑陋,并且不可维护?还有,如何处理这个问题呢?最好创建某种工厂方法或工厂类。然后可以调用var product=\u factory.createProductsDTO;Automper的开发速度非常快,但在维护方面非常差。编写单元测试很难。当您的业务逻辑编码在Automper配置中而不是业务服务中时,这是非常糟糕的。