将模型对象映射到视图模型的包装器?

时间:2019-01-25 11:14:20

标签: c# dictionary model automapper viewmodel

我想将我在服务层中调用的数据对象映射到包装视图模型,以便可以 最小化下面的代码。

我收到错误消息发现未映射成员: 主题详情

内容

ContentRelated

我不确定viewmodel中的这些层如何映射? 感谢您的帮助

控制器

    public async Task<ActionResult> WorkAreaDetail(Guid id)
    {
        if (!settings.EnableNavV2)
            return RedirectToAction("Index");

        var topic = await navigatorService.GetTopicContentAsync(id);

        var model = mappingService.Map<NavigatorTopicWrapperVM>(topic);


        if (topic == null)
            throw new HttpException(404, "Topic does not exist");

        var viewModel = new NavigatorTopicWrapperVM
        {
            TopicDetails = new NavigatorTopicSummaryVM
            {
                Title = topic.Name,
                SummaryText = topic.SummaryText,
            },
            Content = new NavigatorTopicContentVM
            {
                Jurisdictions = topic.Jurisdictions.Select(x => new NavigatorTopicJurisdictions
                {
                    Jurisdiction = x.Jurisdiction,
                    FlagGifName = x.FlagGifName,
                    FirmName = x.FirmName,
                })
                .ToList()
            }
        };

        if (topic.Articles != null && topic.Articles.Any())
        {
            viewModel.Content.Articles = topic.Articles.Select(x => new NavigatorTopicArticlesMock
            {
                Title = x.Title,
                ArticleEditors = x.ArticleEditors.Select(b => (b.AuthorName, b.Url)).ToList()
            })
            .ToList();
        }

        if (topic.ConsultingEditors.Any())
        {
            viewModel.TopicDetails.Editors = topic.ConsultingEditors.Select(editor => new NavigatorTopicEditorsVM
            {
                AuthorGuid = editor.AuthorGuid,
                AuthorName = editor.AuthorName,
                PermalinkAuthorName = editor.PermalinkAuthorName,
                Firm = new NavigatorTopicFirmsVM
                {
                    FirmRef = editor.FirmRef,
                    ContributorGuid = editor.ContributorGuid,
                    FirmName = editor.FirmName,
                    Url = editor.FirmUrl
                }
            }).ToList();
        }

        if (topic.RelatedTopics != null)
        {
            viewModel.ContentRelated = topic.RelatedTopics
                .Select(x => new NavigatorTopicSummaryVM
                {
                    Id = x.Id,
                    Title = x.Name,
                    SummaryText = x.SummaryText,
                    Editors = topic.RelatedTopics.Where(t => t.Id == x.Id)
                        .SelectMany(e => e.ConsultingEditors)
                        .Select(editor => new NavigatorTopicEditorsVM
                        {
                            AuthorGuid = editor.AuthorGuid,
                            AuthorName = editor.AuthorName,
                            PermalinkAuthorName = editor.PermalinkAuthorName,
                            Firm = new NavigatorTopicFirmsVM
                            {
                                FirmRef = editor.FirmRef,
                                ContributorGuid = editor.ContributorGuid,
                                FirmName = editor.FirmName,
                                Url = editor.FirmUrl
                            }
                        }).ToList()
                }).ToList();
        }

        return View(viewModel);
    }
}

//服务层

    public async Task<NavigatorTopicContent> GetTopicContentAsync(Guid topicId)
    {
        var topicDetails = await GetTopicAsync(topicId);

        var relatedTopics =
            await GetTopicSummaryAsync(
                onlyActive: true,
                inGroups: topicDetails.TopicGroups.Select(x => x.GroupId)
            );

        relatedTopics = relatedTopics.Where(x => x.Id != topicId).Take(3).InRandomOrder();

        var jurisdictions = new List<NavigatorTopicJurisdictions>();

        if (topicDetails.Jurisdictions != null)
        {
            foreach (var item in topicDetails.Jurisdictions.DistinctBy(x => x.JurisdictionRef))
            {
                var jurisdiction = contentMetadataService.GetJurisdiction((short)item.JurisdictionRef);

                if (jurisdiction != null && item.FirmRef.HasValue)
                {
                    jurisdictions.Add(new NavigatorTopicJurisdictions
                    {
                        Jurisdiction = jurisdiction.JurisdictionName,
                        FlagGifName = jurisdiction.FlagGifName,
                        FirmName = contributorDisplayNameService.GetSingularContributorDisplayName(item.FirmRef.Value)
                    });
                }
            }
        }

        var topicContent = new NavigatorTopicContent
        {
            Id = topicId,
            Name = topicDetails.Title,
            SummaryText = topicDetails.PractiseAreasSummary,
            ConsultingEditors = topicDetails.ConsultingEditors.Select(x => new NavigatorAuthorDTO
            {
                AuthorGuid = x.AuthorGuid,
                AuthorName = x.AuthorName,
                PermalinkAuthorName = x.PermalinkAuthorName,
                ContributorGuid = x.ContributorGuid,
                FirmRef = x.FirmRef,
                FirmName = contributorDisplayNameService.GetSingularContributorDisplayName(x.FirmRef),
                FirmUrl = contributorService.GetContributorUrl(x.FirmRef, hubConfigService.GetRootPathForContentType(ContentTypes.Contributor))
            }),
            Jurisdictions = jurisdictions,
            RelatedTopics = relatedTopics.Select(x => new NavigatorTopicContent
            {
                ConsultingEditors = x.ConsultingEditors.Select(e => new NavigatorAuthorDTO
                {
                    AuthorGuid = e.AuthorGuid,
                    AuthorName = e.AuthorName,
                    PermalinkAuthorName = e.PermalinkAuthorName,
                    ContributorGuid = e.ContributorGuid,
                    FirmRef = e.FirmRef,
                    FirmName = contributorDisplayNameService.GetSingularContributorDisplayName(e.FirmRef),
                    FirmUrl = contributorService.GetContributorUrl(e.FirmRef, hubConfigService.GetRootPathForContentType(ContentTypes.Contributor))
                }),
                Id = x.Id,
                Name = x.Title,
                SummaryText = x.Description
            }),
            Articles = navigatorTopicRepository.GetTopicArticlesMock()
        };


        return topicContent;
    }

//创建地图

CreateMap<NavigatorTopicContent, NavigatorTopicSummaryVM>();

0 个答案:

没有答案