views:

21

answers:

2

I have a repository:

public ObservableCollection<ProjectExpenseBO> GetProjectExpenses()
{
    //Get by query
    IQueryable<ProjectExpenseBO> projectExpenseQuery =
       from p in _service.project_expense
       from e in _service.vw_employee 
       where p.employee_id == e.employee_id 
       select new ProjectExpenseBO()
       {
           ProjectExpenseID = p.project_expense_id
           , EmployeeID = p.employee_id
           , ProjectNumber = p.project_number
           , PurchaseTypeID = p.purchase_type_id
           , BuyerEmployeeID = p.buyer_employee_id
           , PurchaseOrderNumber = p.purchase_order_number
           , DeliveryDate = p.delivery_date
           , EmployeeName = e.first_name + " " + e.last_name
       };

    ObservableCollection<ProjectExpenseBO> projectExpenseCollection = new ObservableCollection<ProjectExpenseBO>(projectExpenseQuery);
    return projectExpenseCollection;
}

I am wondering if it is better to return an IList or IEnumerable (instead of an ObservableCollection) from my repository since my viewmodel may end up putting it in either an ObservableCollection or List, depending on my need. For instance, I may return data from the repository above to a read-only datagrid or dropdown list, or I may want the same data in an editable datagrid.

I am thinking (and could be wrong) that I want my repository to return a barebones list, then convert it to what suites my needs in the viewmodel. Is my thinking correct? Here is what I was thinking:

public IEnumerable<ProjectExpenseBO> GetProjectExpenses()
{
    //Get by query
    IQueryable<ProjectExpenseBO> projectExpenseQuery =
       from p in _service.project_expense
       from e in _service.vw_employee 
       where p.employee_id == e.employee_id 
       select new ProjectExpenseBO()
       {
           ProjectExpenseID = p.project_expense_id
           , EmployeeID = p.employee_id
           , ProjectNumber = p.project_number
           , PurchaseTypeID = p.purchase_type_id
           , BuyerEmployeeID = p.buyer_employee_id
           , PurchaseOrderNumber = p.purchase_order_number
           , DeliveryDate = p.delivery_date
           , EmployeeName = e.first_name + " " + e.last_name
       };
    return projectExpenseQuery;
}

Thanks.

A: 

Personally I'd return an IEnumerable<T> if you're simply using it to populate the UI. No need to return a List<T> if you aren't going to be adding/removing items from it.

David Neale
+2  A: 

I would personally return an IEnumerable<T> or IList<T> instead of ObservableCollection. There are many times when you may not need the full behavior of ObservableCollection<T>, in which case you're putting more resources than necessary.

I like your second implementation - however, be aware that there is one potential downside. Some people don't like returning deferred execution IEnumerable<T> from a repository, since it defers execution until usage. While it has the upside of potentially saving resources, especially if you end up not using some or all of the enumerable, this can lead to an exception occuring later (when the IEnumerable<T> is actually used), instead of occuring within your repository yourself.

If this bothers you, you could just force the execution to occur (ie: call ToList() or similar).

Reed Copsey
Thanks! I will have to see how deferred execution will affect my application. I found this thread and need to read it a little better:http://stackoverflow.com/questions/1168944/how-to-tell-if-an-ienumerablet-is-subject-to-deferred-execution
steveareeno