首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NHibernate投影,AutoMapping,IPagedList,怎么做?

NHibernate投影,AutoMapping,IPagedList,怎么做?
EN

Stack Overflow用户
提问于 2012-03-12 11:31:07
回答 1查看 436关注 0票数 3

我有这些实体和模型:

实体:

代码语言:javascript
复制
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; }
}

视图-模型:

代码语言:javascript
复制
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>。你能帮帮我吗?我正在使用nuget可用的Mvc IPagedList

代码语言:javascript
复制
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; }
}

我测试了很多方法并搜索了这个问题,但是我找不到任何解决方案。谢谢你的建议。

EN

回答 1

Stack Overflow用户

发布于 2013-09-10 20:24:33

我也遇到了同样的问题,并且找到了解决问题的方法,也许这种方法也会对你有所帮助。

代码语言:javascript
复制
    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);
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9666229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档