[英]NHibernate Projection to DTO
I am unfamiliar with NHibernate projection. 我不熟悉NHibernate投影。 I am attempting to use it so I can return a List<> rather that an IList<>.
我正在尝试使用它,因此我可以返回List <>而不是IList <>。 I am not having much luck with projecting to the DTO as of yet.
到目前为止,我对DTO的投射还不够运气。 I have the following query and Domain objects.
我有以下查询和域对象。 So to begin I am just trying to get a list of Orders given an EmployeeID.
因此,首先,我只是尝试获取给定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?
谁能告诉我与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.
基本上..我只想创建DTO的列表。
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: 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; }
}
In nHibernate 2.1, the .List() method actually already returns a List type, so you can just cast: 在nHibernate 2.1中,.List()方法实际上已经返回了List类型,因此您可以进行强制转换:
var list = (List<EmployeeOrder>)criteriaSelect.List<EmployeeOrder>();
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: 但是,如果您希望将来更加安全,并且不依赖于基于当前nHibernate的实现的假设,则可以编写一个接受ICriteria的扩展方法:
public static List<T> ToList<T>(this ICriteria criteria)
{
return criteria.List<T>().ToList();
}
Change 更改
criteriaSelect.List<EmployeeOrder>();
to 至
List<EmployeeOrder> employeeOrders = criteriaSelect.List<EmployeeOrder>() as List<EmployeeOrder>;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.