NHibernate 投影到 DTO

发布于 2024-07-18 06:56:05 字数 2293 浏览 7 评论 0原文

我不熟悉 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自在安然 2024-07-25 06:56:05

在 nHibernate 2.1 中, .List() 方法实际上已经返回一个 List 类型,所以你可以直接转换:

var list = (List<EmployeeOrder>)criteriaSelect.List<EmployeeOrder>();

但是,如果你想保证未来的安全并且不依赖于基于当前 nHibernate 实现的假设,我会写接受 ICriteria 的扩展方法:

  public static List<T> ToList<T>(this ICriteria criteria)
  {
    return criteria.List<T>().ToList();
  }

In nHibernate 2.1, the .List() method actually already returns a List type, so you can just cast:

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:

  public static List<T> ToList<T>(this ICriteria criteria)
  {
    return criteria.List<T>().ToList();
  }
木有鱼丸 2024-07-25 06:56:05

改成

criteriaSelect.List<EmployeeOrder>();

List<EmployeeOrder> employeeOrders = criteriaSelect.List<EmployeeOrder>() as List<EmployeeOrder>;

Change

criteriaSelect.List<EmployeeOrder>();

to

List<EmployeeOrder> employeeOrders = criteriaSelect.List<EmployeeOrder>() as List<EmployeeOrder>;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文