为我的存储库选择正确的集合/列表

发布于 2024-09-08 09:29:17 字数 2031 浏览 4 评论 0原文

我有一个存储库:

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;
}

我想知道是否最好从我的存储库返回 IList 或 IEnumerable (而不是 ObservableCollection),因为我的视图模型最终可能会将其放入 ObservableCollection 或 List 中,具体取决于我的需要。例如,我可能会将数据从上面的存储库返回到只读数据网格或下拉列表,或者我可能希望在可编辑数据网格中使用相同的数据。

我在想(可能是错误的)我希望我的存储库返回一个准系统列表,然后将其转换为适合我在视图模型中的需求的列表。我的想法正确吗?这就是我的想法:

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;
}

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

英雄似剑 2024-09-15 09:29:17

我个人会返回 IEnumerableIList 而不是 ObservableCollection。很多时候,您可能不需要 ObservableCollection 的完整行为,在这种情况下,您会投入不必要的资源。

我喜欢你的第二个实现 - 但是,请注意有一个潜在的缺点。有些人不喜欢从存储库返回延迟执行 IEnumerable,因为它会将执行延迟到使用为止。虽然它具有潜在节省资源的优点,特别是如果您最终不使用部分或全部可枚举,这可能会导致稍后发生异常(当实际使用 IEnumerable 时) ,而不是自己发生在您的存储库中。

如果这让您烦恼,您可以强制执行(即:调用 ToList() 或类似方法)。

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).

烟织青萝梦 2024-09-15 09:29:17

就个人而言,如果您只是使用它来填充 UI,我会返回 IEnumerable。如果您不打算从中添加/删除项目,则无需返回 List

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.

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