[英]How to perform database queries using nhibernate and multiple repositories (under window service)
我知道这个问题问了很多(我认为),但是我在最聪明的程序员(谷歌)中找不到明确的答案。 我已经实现了一些存储库(不是通用存储库),但是实现了每个实体。
public class Person
{
public string Name { get; set; }
public int Id { get; set; }
}
public class Product
{
public int ProductId { get; set; }
}
public class PersonRepository
{
ISession Session { get; set; }
public Person GetById(int id)
{
//...
}
public IEnumerable<Person> GetAll()
{
//...
}
}
public class ProductRepository
{
ISession Session { get; set; }
public Product GetById(int id)
{
//...
}
public IEnumerable<Product> GetAll()
{
//...
}
}
public class PostOfficeService
{
ProductRepository _rep1 = new ProductRepository();
PersonRepository _rep2 = new PersonRepository();
public IEnumerable<Person> GetAllPersonWithSameIdAsProduct()
{
_rep1.GetAll().Where( ... )
// ??? i want it to perform the query in the DB and not two queries in app memory
}
}
我应该使用工作单位模式吗? 那里有很多数据和信息,但不能将我的手指放在“正确的”解决方案上,还是根本不是一个好的解决方案?
工作单元模式与事务有关-我看不到它与这个问题有何关系。
GetAllPersonWithSameIdAsProduct
应该是您的PersonRepository
之一或第三个更高级的存储库的方法,因为在存储库级别之上,您无权访问会话。 另一种方法是使您的存储库接受ICriteria
参数,并使用criteria-builder类创建复杂的查询。
但是,随着IQueryable实现成为您的存储库,使用LINQ-To-NHibernate将使解决这些问题变得更加容易,并且您的Service类可以使用LINQ进行查询,而无需知道它们正在与数据库进行通信。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.