3

我有这些实体和模型:NHibernate投影,AutoMapping,IPagedList,如何?

实体:

public class BlogPost { 
    public virtual int Id { get; set; } 
    public virtual IList<Keyword> Keywords { get; set; } 
    public virtual IList<BlogComment> Comments { get; set; } 
} 

public class BlogComment { 
    public virtual int Id { get; set; } 
    public virtual BlogPost Post { get; set; } 
} 

public class Keyword { 
    public virtual int Id { get; set; } 
    public virtual IList<BlogPost> BlogPosts { get; set; } 
} 

VIEW的模型:

public class BlogPostModel { 
    public int Id { get; set; } 
    // instead of IList<BlogComment> I have this: 
    public int CommentsCount { get; set; } 
    public IList<KeywordModel> Keywords { get; set; } 
} 

public class KeywordModel { 
    public int Id { get; set; } 
    public IList<BlogPostModel> Posts { get; set; } 
} 

public class BlogCommentModel { 
    public int Id { get; set; } 
    public BlogPostModel Post { get; set; } 
} 

现在我要加载博客,帖子的分页列表与他们的关键字和评论计数和地图他们AutoMapper库到IPagedList<BlogPostModel> 。你能帮我吗?我使用的是Mvc IPagedList提供的NuGet:

public interface IPagedList<out T> : IPagedList, IEnumerable<T> { 
    T this[int index] { get; } 
    int Count { get; } 
    IPagedList GetMetaData(); 
} 

public interface IPagedList { 
    int PageCount { get; } 
    int TotalItemCount { get; } 
    int PageNumber { get; } 
    int PageSize { get; } 
    bool HasPreviousPage { get; } 
    bool HasNextPage { get; } 
    bool IsFirstPage { get; } 
    bool IsLastPage { get; } 
    int FirstItemOnPage { get; } 
    int LastItemOnPage { get; } 
} 

我测试的许多方面和google搜索的问题,但是我无法找到任何解决方案。感谢您的任何建议。

回答

0

我有同样的问题,并找到了如何解决这个问题,可能这种方法也会帮助你。

public class GenericModelViewConverter<TModel, TView> : IModelViewConverter<TModel, TView> 
     where TModel : class 
     where TView : class 
    { 
     /// <summary> 
     /// Initializes a new instance of the <see cref="GenericModelViewConverter{TModel, TView}"/> class. 
     /// </summary> 
     public GenericModelViewConverter() 
     { 
      // It is not he best place to create the mapping here 
      Mapper.CreateMap<TView, TModel>(); 
      Mapper.CreateMap<TView, TModel>().ReverseMap(); 
     } 

     /// <summary> 
     /// Converts the view to model. 
     /// </summary> 
     /// <param name="view">The view as a source.</param> 
     /// <param name="model">The model as a destination value.</param> 
     /// <returns>The destination model.</returns> 
     public virtual TModel ConvertViewToModel(TView view, TModel model = null) 
     { 
      return model == null ? Mapper.Map<TView, TModel>(view) : Mapper.Map(view, model); 
     } 

     /// <summary> 
     /// Converts the model to view. 
     /// </summary> 
     /// <param name="model">The model as a source.</param> 
     /// <param name="view">The view as a destination.</param> 
     /// <returns>The destination view.</returns> 
     public virtual TView ConvertModelToView(TModel model, TView view = null) 
     { 
      return view == null ? Mapper.Map<TModel, TView>(model) : Mapper.Map(model, view); 
     } 

     /// <summary> 
     /// Converts the view to model. 
     /// </summary> 
     /// <param name="viewCollection">The collection of views.</param> 
     /// <returns>The collection of models.</returns> 
     public virtual IEnumerable<TModel> ConvertViewToModel(IEnumerable<TView> viewCollection) 
     { 
      return Mapper.Map<IEnumerable<TView>, HashSet<TModel>>(viewCollection); 
     } 

     /// <summary> 
     /// Converts the model to view. 
     /// </summary> 
     /// <param name="modelCollection">The collection of models.</param> 
     /// <returns>The collection of views.</returns> 
     public virtual IEnumerable<TView> ConvertModelToView(IEnumerable<TModel> modelCollection) 
     { 
      return Mapper.Map<IEnumerable<TModel>, HashSet<TView>>(modelCollection); 
     } 

     /// <summary> 
     /// Converts the model to view. 
     /// </summary> 
     /// <param name="modelCollection">The model collection.</param> 
     /// <returns>The collection of models.</returns> 
     public virtual IPagedCollection<TView> ConvertModelToView(IPagedCollection<TModel> modelCollection) 
     { 
      var list = modelCollection.ToList(); 
      var resultList = ConvertModelToView(list); 

      return new PagedCollection<TView>(resultList, modelCollection.PageIndex, modelCollection.PageSize, modelCollection.TotalItemCount); 
     } 

     /// <summary> 
     /// Converts the view to model. 
     /// </summary> 
     /// <param name="viewCollection">The view collection.</param> 
     /// <returns>The collection of views.</returns> 
     public virtual IPagedCollection<TModel> ConvertViewToModel(IPagedCollection<TView> viewCollection) 
     { 
      var list = viewCollection.ToList(); 
      var resultList = ConvertViewToModel(list); 

      return new PagedCollection<TModel>(resultList, viewCollection.PageIndex, viewCollection.PageSize, viewCollection.TotalItemCount); 
     } 
    }