2017-12-07 8 views
1

私はPagedListの実装を持っており、AutoMapperを使用してエンティティPagedListをDTO PagedListにマップしようとしています。ここでAutomapperインターフェイスマッピング

は私のインターフェイスである:ここでは

public interface IPagedList<T> : IList<T> 
{ 
    PagingInformation Paging { get; set; } 
} 

は私のクラスの実装です:

public class PagedList<T> : List<T>, IPagedList<T> //, IList<T> 
{ 
    public PagingInformation Paging { get; set; } 

    public PagedList() 
    { 
    } 

    public PagedList(IEnumerable<T> collection) : base(collection) 
    { 
    } 

    public PagedList(IEnumerable<T> collection, PagingInformation paging) : base(collection) 
    { 
     Paging = paging; 
    } 

    public PagedList(int capacity) : base(capacity) 
    { 
    } 

    PagingInformation IPagedList<T>.Paging 
    { 
     get => Paging; 
     set => Paging = value; 
    } 

    IEnumerator IEnumerable.GetEnumerator() 
    { 
     return GetEnumerator(); 
    } 


} 

私はマッパー設定なしで

public async Task<DomainResult<IPagedList<PositionDto>>> GetPagedListAsync(int pageIndex = 0, int pageSize = 20) 
{ 
    return DomainResult<IPagedList<PositionDto>>.Success(_mapper.Map<IPagedList<PositionDto>>(await _positionRepository.GetPagedListAsync(pageIndex, pageSize))); 
} 

ようAutomapperを使用しています: 私は次のようになっていますエラー:

Error mapping types.

Mapping types: PagedList 1 -> IPagedList 1 WestCore.Shared.Collections.Pagination.PagedList 1[[WestCore.Domain.Entities.Position, WestCore.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> WestCore.Shared.Collections.Pagination.IPagedList 1[[WestCore.AppCore.Models.PositionDto, WestCore.AppCore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

私はマッパーPofileにCreateMap(typeof(PagedList<>), typeof(IPagedList<>))を追加すると、私は次のエラーを取得しています:私はマッパープロファイルにCreateMap(typeof(PagedList<>),typeof(IPagedList<>)).As(typeof(PagedList<>));を追加すると、私はしかし、エラーを取得していない午前

Method 'get_Item' in type 'Proxy WestCore.Shared.Collections.Pagination.IPagedList`1[[WestCore.AppCore.Models.PositionDto_WestCore.AppCore_Version=1.0.0.0_Culture=neutral_PublicKeyToken=null]]_WestCore.Shared_Version=1.0.0.0_Culture=neutral_PublicKeyToken=null' from assembly 'AutoMapper.Proxies, Version=0.0.0.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005' does not have an implementation.

PagedListは、空の結果が

を設定して返します。 PagedListメソッドに実装がないかどうか、または設定上の問題であるかどうかはわかりません。

編集:

PagingInformationは、以下の追加:

public class PagingInformation 
{ 
    /// <summary> 
    /// Gets the index start value. 
    /// </summary> 
    /// <value>The index start value.</value> 
    public int IndexFrom { get; } 

    /// <summary> 
    /// Gets the page index (current). 
    /// </summary> 
    public int PageIndex { get; } 

    /// <summary> 
    /// Gets the page size. 
    /// </summary> 
    public int PageSize { get; } 

    /// <summary> 
    /// Gets the total count of the list of type <typeparamref name="TEntity"/> 
    /// </summary> 
    public int TotalCount { get; } 

    /// <summary> 
    /// Gets the total pages. 
    /// </summary> 
    public int TotalPages { get; } 

    /// <summary> 
    /// Gets the has previous page. 
    /// </summary> 
    /// <value>The has previous page.</value> 
    public bool HasPreviousPage => PageIndex - IndexFrom > 0; 

    /// <summary> 
    /// Gets the has next page. 
    /// </summary> 
    /// <value>The has next page.</value> 
    public bool HasNextPage => PageIndex - IndexFrom + 1 < TotalPages; 

    public PagingInformation(int pageIndex, int pageSize, int indexFrom, int count) 
    { 
     if (indexFrom > pageIndex) 
     { 
      throw new ArgumentException($"indexFrom: {indexFrom} > pageIndex: {pageIndex}, must indexFrom <= pageIndex"); 
     } 

     PageIndex = pageIndex; 
     PageSize = pageSize; 
     IndexFrom = indexFrom; 
     TotalCount = count; 
     TotalPages = (int) Math.Ceiling(TotalCount/(double) PageSize); 

    } 
} 

はマッパーがこれを作成する方法が分からないので、あなたは、マッピングの結果として、インタフェースを使用することはできませんあなた

答えて

2

ありがとうございます。

ConstructUsingを使用してIPagedListを作成できます。

AutoMapper.Mapper.CreateMap<DtoParent, ICoreParent>() 
      .ConstructUsing(parentDto => new CoreParent()) 
      .ForMember(dest => dest.Other, opt => opt.MapFrom(src => AutoMapper.Mapper.Map<DtoChild, ICoreChild>(src.Other))); 

EDIT:

作業この方法:次のようにも

class Example 
{ 
    static void Main() 
    { 
     AutoMapper.Mapper.Initialize(config => 
     { 
      config.CreateMap(typeof(PagedList<>), typeof(IPagedList<>)) 
       .ConvertUsing(typeof(Converter<,>)); 

      config.CreateMap<Entity, DTO>(); 

     }); 

     var entityList = new PagedList<Entity>(new [] { new Entity(), }, new PagingInformation() { Total = 2, PageNumber = 1, PageSize = 10}); 

     var mapped = Mapper.Map<IPagedList<DTO>>(entityList); 
    } 
} 

class Converter<TSource, TDest> : ITypeConverter<IPagedList<TSource> , IPagedList<TDest>> 
{ 
    public IPagedList<TDest> Convert(IPagedList<TSource> source, IPagedList<TDest> destination, ResolutionContext context) => new PagedList<TDest>(context.Mapper.Map<IEnumerable<TDest>>(source.AsEnumerable()), source.Paging); 
} 

class Entity 
{ 
    public Guid Id { get; set; } = Guid.NewGuid(); 
} 

class DTO 
{ 
    public Guid Id { get; set; } = Guid.NewGuid(); 
} 

public interface IPagedList<T> : IList<T> 
{ 
    PagingInformation Paging { get; set; } 
} 

public class PagingInformation 
{ 
    public int Total { get; set; } 

    public int PageSize { get; set; } 

    public int PageNumber { get; set; } 
} 

public class PagedList<T> : List<T>, IPagedList<T> 
{ 
    public PagingInformation Paging { get; set; } 

    public PagedList() { } 
    public PagedList(IEnumerable<T> collection) : base(collection) { } 

    public PagedList(IEnumerable<T> collection, PagingInformation paging) : base(collection) { Paging = paging; } 
} 

おそらくこれは私の例のように他の方法でPagingInformationをマップする必要が両方のリストがに参照されているページング1つのPagingInformationオブジェクトがマップの後にあります.PagingInformationが不変になるまではOKです。

+0

私のPagedListメソッドにはジェネリック型があり、そのためにConstructUsingメソッドを使用できませんでした。私はこれを行う簡単な方法がなければならないと信じています。 – Simonare

+0

反射?....本当に良い解決策ではありませんが、助けてください。私はそれについてもっと考えます。 –

+0

PagingInformationクラス定義を追加してください。これはページ番号、カウントなどを保持すると予想されますか? –