Returning a List Without Using an NHibernate Object

Suppose I have an interface.

public interface IBlogRepository
{
    IList<Blog> Blogs(int pageNo, int pageSize);
    int TotalPosts();
}

Now I have created a class to implement it and am using NHibernate.

using NHibernate;
using NHibernate.Criterion;
using NHibernate.Linq;
using NHibernate.Transform;
using System.Collections.Generic;
using System.Linq;

namespace JustBlog.Core
{
    public class BlogRepository: IBlogRepository
    {
        // NHibernate object
        private readonly ISession _session;

        public BlogRepository(ISession session)
        {
            _session = session;
        }

        public IList<Post> Posts(int pageNo, int pageSize)
        {
            var query = _session.Query<Post>()
                        .Where(p => p.Published)
                        .OrderByDescending(p => p.PostedOn)
                        .Skip(pageNo * pageSize)
                        .Take(pageSize)
                        .Fetch(p => p.Category);

            query.FetchMany(p => p.Tags).ToFuture();

            return query.ToFuture().ToList();
        }

        public int TotalPosts()
        {
            return _session.Query<Post>().Where(p => p.Published).Count();
        }
    }

The above code is somewhere on the Internet to create a blog engine . However, I don’t know NHibernate at all, I use the Entity Framework to do my job.

How can I rewrite code without using NHiberate?

+4
source share
2 answers

Entity Models

Assuming we have entity models:

public class Post
{
    public Post() { Tags = new List<Tag>(); }

    public int Id{ get; set; }
    public string Title{ get; set; }
    public string ShortDescription{ get; set; }
    public string Description{ get; set; }
    public string Meta{ get; set; }
    public string UrlSlug{ get; set; }
    public bool Published{ get; set; }
    public DateTime PostedOn{ get; set; }
    public DateTime? Modified{ get; set; }

    public int CategoryId { get; set; }
    public virtual Category Category{ get; set; }

    public virtual IList<Tag> Tags{ get; set; }
}

public class Tag
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string UrlSlug { get; set; }
    public string Description { get; set; }

    public virtual IList<Post> Posts { get; set; }
}

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string UrlSlug { get; set; }
    public string Description { get; set; }

    public virtual IList<Post> Posts { get; set; }
}

Context

Our context class is pretty simple. The constructor takes the name of the connection string in web.configand we define three DbSets:

public class BlogContext : DbContext
{
    public BlogContext() : base("BlogContextConnectionStringName") { }

    public DbSet<Category> Categories { get; set; }
    public DbSet<Post> Posts { get; set; }
    public DbSet<Tag> Tags { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

Repository

:

public interface IBlogRepository
{
    IEnumerable<Post> Posts(int pageNo, int pageSize);
    int TotalPosts();
}

-, !

public class BlogRepository : IBlogRepository
{
    // NHibernate object replace with our context
    private readonly BlogContext _blogContext;

    public BlogRepository(BlogContext blogContext)
    {
        _blogContext = blogContext;
    }

    //Function to get a list of blogs
    public IEnumerable<Post> Posts(int pageNo, int pageSize)
    {
        //We start with the blogs db set:
        var query = _blogContext.Posts
            //Filter by Published=true
            .Where(p => p.Published)
            //Order by date they were posted
            .OrderByDescending(p => p.PostedOn)
            //Jump through the list
            .Skip(pageNo * pageSize)
            //Get the required number of blogs
            .Take(pageSize)
            //Make sure the query include all the categories
            .Include(b => b.Category);

        //Just return what we have!
        return query;
    }

    //Much simpler function, should be pretty self explanatory
    public int TotalPosts()
    {
        return _blogContext.Posts.Where(p => p.Published).Count();
    }
}

, , , web.config , ? , !

var context = new BlogContext();
var repository = new BlogRepository(context);
var posts = repository.Posts(0, 10);

:

foreach(var blog in blogs)
{
    Console.WriteLine("Blog Id {0} was posted on {1} and has {2} categories", blog.Id, blog.PostedOn, blog.Categories.Count());
}

FetchMany/ToFuture, .

+3

Linq Entities, .

, , , , , .

ADO Entity , , XXXEntities, ObjectContext .

, (, BlogEntities):

public IEnumerable<Blog> Posts(int pageNo, int pageSize)
{

    using(BlogEntities con = new BlogEntities())
    {

        var query = con.Post.Include("Category") //<-- If you set "Pluralize fields and properties" when creating your model, it will be "Posts" and "Categories"
                    .Where(p => p.Published)
                    .OrderByDescending(p => p.PostedOn)
                    .Skip(pageNo * pageSize)
                    .Take(pageSize)
                    .ToList();

        return query;

    }

}

public int TotalPosts()
{
    using(BlogEntities con = new BlogEntities())
    {

          return con.Post.Where(p => p.Published).Count();

    }

}
0

Source: https://habr.com/ru/post/1541292/


All Articles