Designing NHibernate, AutoMapping, IPagedList, how?

I have these entities and models:

ENTITIES:

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-MODELS:

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; }
}

      

Now I want to load the dumped list of blog posts with their keywords and comments and map them using the AutoMapper library on IPagedList<BlogPostModel>

. could you help me? I am using Mvc IPagedList

for 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; }
}

      

I am testing many ways and looking for the problem, but I cannot find any solution. Thanks for any suggestion.

+3


source to share


1 answer


I had the same problem and found a way to fix it, maybe this approach will help you too.



    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


source







All Articles