C# 自动映射接口映射

C# 自动映射接口映射,c#,domain-driven-design,automapper,C#,Domain Driven Design,Automapper,我有PagedList实现,我正试图使用AutoMapper将实体PagedList映射到数据到PagedList 这是我的界面: public interface IPagedList<T> : IList<T> { PagingInformation Paging { get; set; } } 谢谢由于映射,您无法使用该界面,因为映射者不知道如何创建该界面 您可以使用ConstructUsing创建IPagedList。像这样: AutoMapper.Ma

我有PagedList实现,我正试图使用AutoMapper将实体PagedList映射到数据到PagedList

这是我的界面:

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

谢谢

由于映射,您无法使用该界面,因为映射者不知道如何创建该界面

您可以使用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)));
AutoMapper.Mapper.CreateMap()
.ConstructUsing(parentDto=>newCoreParent())
.ForMember(dest=>dest.Other,opt=>opt.MapFrom(src=>AutoMapper.Mapper.Map(src.Other));
编辑:

这样做:

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; }
}
类示例
{
静态void Main()
{
AutoMapper.Mapper.Initialize(配置=>
{
config.CreateMap(typeof(PagedList)、typeof(IPagedList))
.转换器使用(类型(转换器));
config.CreateMap();
});
var entityList=new PagedList(new[]{new Entity(),},new paginformation(){Total=2,PageNumber=1,PageSize=10});
var-mapped=Mapper.Map(entityList);
}
}
类转换器:ITypeConverter
{
公共IPagedList转换(IPagedList源、IPagedList目标、ResolutionContext上下文)=>新建页面列表(context.Mapper.Map(source.AsEnumerable()),source.Paging);
}
类实体
{
公共Guid Id{get;set;}=Guid.NewGuid();
}
类DTO
{
公共Guid Id{get;set;}=Guid.NewGuid();
}
公共接口IPagedList:IList
{
分页信息分页{get;set;}
}
公共类分页信息
{
公共整数总计{get;set;}
公共int PageSize{get;set;}
公共整数页码{get;set;}
}
公共类页面列表:列表,IPagedList
{
公共分页信息分页{get;set;}
公共页面列表(){}
公共页面列表(IEnumerable集合):基本(集合){}
公共分页列表(IEnumerable集合,分页信息分页):基本(集合){paging=paging;}
}

另外,这可能需要以其他方式映射分页信息,因为在我的示例中,两个分页列表都引用映射后的一个分页信息对象,我认为在分页信息不可变之前,这是可以的

我的PagedList方法具有泛型类型,因此我不能使用ConstructUsing方法。我相信一定有更简单的方法来做到这一点。反思?。。。。不是很好的解决方案,但应该有帮助。我会考虑更多。你能添加分页信息类定义吗。这将保留添加到原始帖子中的页码、计数等。我可以使用反射,但据我所知,我需要非泛型的基本方法。我不确定我将如何实现它。事实上,我没有使用setter。它工作得非常好。非常感谢。
    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);

    }
}
AutoMapper.Mapper.CreateMap<DtoParent, ICoreParent>()
            .ConstructUsing(parentDto => new CoreParent())
            .ForMember(dest => dest.Other, opt => opt.MapFrom(src => AutoMapper.Mapper.Map<DtoChild, ICoreChild>(src.Other)));
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; }
}