假设我有一个界面。
public interface IBlogRepository
{
IList<Blog> Blogs(int pageNo, int pageSize);
int TotalPosts();
}
现在我创建了一个实现它的类并使用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();
}
}
以上代码来自网络for creating a blog engine上的某个地方。但是我根本不了解NHibernate,我使用Entity Framework来完成我的工作。
如何在不使用NHiberate的情况下重写代码?
答案 0 :(得分:3)
实体模型
假设我们有这样的实体模型:
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; }
}
<强>上下文强>
我们的上下文类非常简单。构造函数接受web.config
中连接字符串的名称,我们定义了三个DbSet
:
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);
}
}
<强>存储库强>
我们博客存储库的界面非常简单且不会发生太大变化:
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
部分,因为这里不需要它们。
答案 1 :(得分:0)
一种简单的方法是使用Linq to Entities从数据库映射模型。
首先,在数据库中创建与您链接的帖子中定义的对象完全相同的表格,记住添加关系。
然后添加一个ADO实体数据模型并从数据库中选择这些表填充它,您将以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();
}
}