views:

1832

answers:

2

Hello,

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; }
}
+2  A: 

Change

criteriaSelect.List<EmployeeOrder>();

to

List<EmployeeOrder> employeeOrders = criteriaSelect.List<EmployeeOrder>() as List<EmployeeOrder>;
Todd Brooks
+1  A: 

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();
  }
taoufik