/// <summary> /// Map static properties (defined in the entity class) of dynamic DTO to a specified entity /// </summary> /// <typeparam name="TDynamicDto">Type of Dynamic DTO</typeparam> /// <typeparam name="TEntity">Type of entity</typeparam> /// <typeparam name="TPrimaryKey">Type of primary key</typeparam> /// <param name="dto">Source DTO</param> /// <param name="entity">Destination entity</param> /// <returns></returns> protected async Task MapStaticPropertiesToEntityDtoAsync <TDynamicDto, TEntity, TPrimaryKey>(TDynamicDto dto, TEntity entity) where TEntity : class, IEntity <TPrimaryKey> where TDynamicDto : class, IDynamicDto <TEntity, TPrimaryKey> { var mapper = await DynamicDtoMappingHelper.GetDtoToEntityMapperAsync(entity.GetType(), dto.GetType()); mapper.Map(dto, entity); }
/// <summary> /// Map entity to a custom <see cref="DynamicDto{TEntity, TPrimaryKey}"/> /// </summary> /// <typeparam name="TDynamicDto">Type of dynamic DTO</typeparam> /// <typeparam name="TEntity">Type of entity</typeparam> /// <typeparam name="TPrimaryKey">Type of entity primary key</typeparam> /// <param name="entity">entity to map</param> /// <param name="settings">mapping settings</param> /// <returns></returns> protected async Task <TDynamicDto> MapToCustomDynamicDtoAsync <TDynamicDto, TEntity, TPrimaryKey>(TEntity entity, IDynamicMappingSettings settings = null) where TEntity : class, IEntity <TPrimaryKey> where TDynamicDto : class, IDynamicDto <TEntity, TPrimaryKey> { // build dto type var context = new DynamicDtoTypeBuildingContext() { ModelType = typeof(TDynamicDto), UseDtoForEntityReferences = settings?.UseDtoForEntityReferences ?? false }; var dtoType = await DtoBuilder.BuildDtoFullProxyTypeAsync(typeof(TDynamicDto), context); var dto = Activator.CreateInstance(dtoType) as TDynamicDto; // create mapper var mapper = await DynamicDtoMappingHelper.GetEntityToDtoMapperAsync(typeof(TEntity), dtoType); // map entity to DTO mapper.Map(entity, dto); // map dynamic fields await DynamicPropertyManager.MapEntityToDtoAsync <TDynamicDto, TEntity, TPrimaryKey>(entity, dto); return(dto); }