private async Task <IReadOnlyDictionary <int, IPageBlockTypeDisplayModel> > MapGeneric <TDataModel>(
            IEnumerable <IEntityVersionPageBlock> pageBlocks,
            PublishStatusQuery publishStatusQuery,
            IExecutionContext executionContext
            ) where TDataModel : IPageBlockTypeDataModel
        {
            var mapperType = typeof(IPageBlockTypeDisplayModelMapper <TDataModel>);
            var mapper     = (IPageBlockTypeDisplayModelMapper <TDataModel>)_serviceProvider.GetService(mapperType);

            if (mapper == null)
            {
                string msg = @"{0} does not implement IPageBlockDisplayModel and no custom mapper could be found. You must create 
                               a class that implements IPageBlockDisplayModelMapper<{0}> if you are using a custom display model. Full type name: {1}";
                throw new Exception(string.Format(msg, typeof(TDataModel).Name, typeof(TDataModel).FullName));
            }

            var dataModels = new List <PageBlockTypeDisplayModelMapperInput <TDataModel> >();

            foreach (var pageBlock in pageBlocks)
            {
                var mapperModel = new PageBlockTypeDisplayModelMapperInput <TDataModel>();
                mapperModel.DataModel      = (TDataModel)_dbUnstructuredDataSerializer.Deserialize(pageBlock.SerializedData, typeof(TDataModel));
                mapperModel.VersionBlockId = pageBlock.GetVersionBlockId();
                dataModels.Add(mapperModel);
            }

            var context = new PageBlockTypeDisplayModelMapperContext <TDataModel>(dataModels, publishStatusQuery, executionContext);
            var result  = new PageBlockTypeDisplayModelMapperResult <TDataModel>(dataModels.Count);
            await mapper.MapAsync(context, result);

            return(result.Items);
        }
Пример #2
0
        private async Task <List <PageBlockTypeDisplayModelMapperOutput> > MapGeneric <T>(
            IEnumerable <IEntityVersionPageBlock> pageBlocks,
            PublishStatusQuery publishStatusQuery
            ) where T : IPageBlockTypeDataModel
        {
            var mapperType = typeof(IPageBlockTypeDisplayModelMapper <T>);
            var mapper     = (IPageBlockTypeDisplayModelMapper <T>)_serviceProvider.GetService(mapperType);

            if (mapper == null)
            {
                string msg = @"{0} does not implement IPageBlockDisplayModel and no custom mapper could be found. You must create 
                               a class that implements IPageBlockDisplayModelMapper<{0}> if you are using a custom display model. Full type name: {1}";
                throw new Exception(string.Format(msg, typeof(T).Name, typeof(T).FullName));
            }

            var dataModels = new List <PageBlockTypeDisplayModelMapperInput <T> >();

            foreach (var pageBlock in pageBlocks)
            {
                var mapperModel = new PageBlockTypeDisplayModelMapperInput <T>();
                mapperModel.DataModel      = (T)_dbUnstructuredDataSerializer.Deserialize(pageBlock.SerializedData, typeof(T));
                mapperModel.VersionBlockId = pageBlock.GetVersionBlockId();
                dataModels.Add(mapperModel);
            }

            var results = await mapper.MapAsync(dataModels, publishStatusQuery);

            return(results.ToList());
        }
        /// <summary>
        /// Adds a mapped model to the result collection.
        /// </summary>
        /// <param name="inputDataModel">
        /// The input data model that the display model is mapped from. This is
        /// used to track which block the mapped display model represents.
        /// </param>
        /// <param name="mappedDisplayModel">
        /// The mapped display model to render in the view. This should not
        /// be null; if the input cannot be mapped then exclude it from the
        /// result entirely.
        /// </param>
        public void Add(PageBlockTypeDisplayModelMapperInput <TDataModel> inputDataModel, IPageBlockTypeDisplayModel mappedDisplayModel)
        {
            if (inputDataModel == null)
            {
                throw new ArgumentNullException(nameof(inputDataModel));
            }
            if (mappedDisplayModel == null)
            {
                throw new ArgumentNullException(nameof(mappedDisplayModel), "When mapping block data the display model should not be null, if the input cannot be mapped then exclude it from the result entirely.");
            }

            if (_mappedDisplayModels.ContainsKey(inputDataModel.VersionBlockId))
            {
                throw new Exception($"The specified block model has already been added to the result. VersionBlockId {inputDataModel.VersionBlockId}, model type '{mappedDisplayModel.GetType()}'");
            }

            _mappedDisplayModels.Add(inputDataModel.VersionBlockId, mappedDisplayModel);
        }