Q:

Return a list without using NHibernate object

Suppose I have an interface.
public interface IBlogRepository
{
    IList<Blog> Blogs(int pageNo, int pageSize);
    int TotalPosts();
}

Now I created a class to implement it and use 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 from somewhere on the web for creating a blog engine. However I don't know NHibernate at all, I use Entity Framework to do my job.
How can I rewrite the code without using NHiberate?

A:

Entity Models
Assuming we have our entity models like this:
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 in the name of the connection string in web.config and 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
The interface for our blog repository is nice and easy and doesn't change much:
public interface IBlogRepository
{
    IEnumerable<Post> Posts(int pageNo, int pageSize);
    int TotalPosts();
}

And it's the blog repository itself where things get interesting!
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();
    }
}

Next Steps
OK, so now we have all that set up, and a nice connection string is set up in your web.config, what do we do with it? Well let's get some blogs!
var context = new BlogContext();
var repository = new BlogRepository(context);
var posts = repository.Posts(0, 10);

Then we can do some stuff with those blogs:
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());
}

Notes
I didn't implement the FetchMany/ToFuture parts as they're not needed here.

