NHibernate 投影到 DTO
我不熟悉 NHibernate 投影。 我正在尝试使用它,以便我可以返回一个 List<> 而不是 IList<>。 到目前为止,我在向 DTO 进行投影方面还不太顺利。 我有以下查询和域对象。 因此,一开始我只是想获取给定 EmployeeID 的订单列表。 我循环遍历结果列表并将其添加到列表中,因为我希望能够序列化该列表。 谁能告诉我我离投影还有多远? 我搜索了一下,发现了一些与我自己的不相似的例子。 基本上..我只想创建一个 DTO 列表。
谢谢!
public List<EmployeeOrder> GetEmployessOrdersDTO(int empid)
{
var emporders = new List<EmployeeOrder>();
ICriteria criteriaSelect = NHibernateSessionManager.Instance.GetSession().CreateCriteria(typeof (Orders))
.CreateCriteria("Employees")
.Add(Expression.Eq("EmployeeID", empid));
criteriaSelect.SetProjection(
Projections.ProjectionList()
.Add(Projections.Property("Products"), "OrderedProducts"));
criteriaSelect.SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(EmployeeOrder)));
criteriaSelect.List<EmployeeOrder>();
foreach (var order in emporders)
{
emporders.Add(order);
}
return emporders;
}
订单:
public class Orders
{
public virtual int OrderID { get; private set;}
public virtual string CustomerID { get; set; }
public virtual DateTime OrderDate { get; set; }
public virtual DateTime RequiredDate { get; set; }
public virtual DateTime ShippedDate { get; set; }
public virtual Employees Employee { get; set; }
public virtual IList<Products> Products { get; private set; }
}
员工:
public class Employees
{
public virtual int EmployeeID { get; private set;}
public virtual string LastName { get; set;}
public virtual string FirstName { get; set;}
public virtual string City { get; set; }
public virtual DateTime HireDate { get; set; }
public virtual string Title { get; set; }
public virtual IList<Orders> Orders { get; private set; }
}
EmployeeOrderDTO:
public class EmployeeOrder
{
public virtual string EmployeeName { get; set; }
public virtual string EmployeeTitle { get; set; }
public virtual DateTime RequiredDate { get; set; }
public virtual List<Products> OrderedProducts { get; set; }
}
I am unfamiliar with NHibernate projection. I am attempting to use it so I can return a List<> rather that an IList<>. I am not having much luck with projecting to the DTO as of yet. I have the following query and Domain objects. So to begin I am just trying to get a list of Orders given an EmployeeID. I am looping through the resulting list and adding it to a List because I wish to be able to serialize this list. Can anyone tell me how far off I am with the Projection? I have searched and found some examples that are not similar to my own. Basically.. I just want to create a List of of the DTOs.
Thanks!
public List<EmployeeOrder> GetEmployessOrdersDTO(int empid)
{
var emporders = new List<EmployeeOrder>();
ICriteria criteriaSelect = NHibernateSessionManager.Instance.GetSession().CreateCriteria(typeof (Orders))
.CreateCriteria("Employees")
.Add(Expression.Eq("EmployeeID", empid));
criteriaSelect.SetProjection(
Projections.ProjectionList()
.Add(Projections.Property("Products"), "OrderedProducts"));
criteriaSelect.SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(EmployeeOrder)));
criteriaSelect.List<EmployeeOrder>();
foreach (var order in emporders)
{
emporders.Add(order);
}
return emporders;
}
Orders:
public class Orders
{
public virtual int OrderID { get; private set;}
public virtual string CustomerID { get; set; }
public virtual DateTime OrderDate { get; set; }
public virtual DateTime RequiredDate { get; set; }
public virtual DateTime ShippedDate { get; set; }
public virtual Employees Employee { get; set; }
public virtual IList<Products> Products { get; private set; }
}
Employees:
public class Employees
{
public virtual int EmployeeID { get; private set;}
public virtual string LastName { get; set;}
public virtual string FirstName { get; set;}
public virtual string City { get; set; }
public virtual DateTime HireDate { get; set; }
public virtual string Title { get; set; }
public virtual IList<Orders> Orders { get; private set; }
}
EmployeeOrderDTO:
public class EmployeeOrder
{
public virtual string EmployeeName { get; set; }
public virtual string EmployeeTitle { get; set; }
public virtual DateTime RequiredDate { get; set; }
public virtual List<Products> OrderedProducts { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 nHibernate 2.1 中, .List() 方法实际上已经返回一个 List 类型,所以你可以直接转换:
但是,如果你想保证未来的安全并且不依赖于基于当前 nHibernate 实现的假设,我会写接受 ICriteria 的扩展方法:
In nHibernate 2.1, the .List() method actually already returns a List type, so you can just cast:
However, if you want to be future safe and not depend on assumptions based on the implementation of the current nHibernate, I'd write an extension method accepting ICriteria:
改成
Change
to