gpt4 book ai didi

c# - 不使用 NHibernate 对象返回列表

转载 作者:太空狗 更新时间:2023-10-30 01:35:58 27 4
gpt4 key购买 nike

假设我有一个界面。

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 的情况下重写代码?

最佳答案

实体模型

假设我们有这样的实体模型:

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 部分,因为这里不需要它们。

关于c# - 不使用 NHibernate 对象返回列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23763502/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com