为我的存储库选择正确的集合/列表
我有一个存储库:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我个人会返回
IEnumerable
或IList
而不是 ObservableCollection。很多时候,您可能不需要ObservableCollection
的完整行为,在这种情况下,您会投入不必要的资源。我喜欢你的第二个实现 - 但是,请注意有一个潜在的缺点。有些人不喜欢从存储库返回延迟执行
IEnumerable
,因为它会将执行延迟到使用为止。虽然它具有潜在节省资源的优点,特别是如果您最终不使用部分或全部可枚举,这可能会导致稍后发生异常(当实际使用IEnumerable
时) ,而不是自己发生在您的存储库中。如果这让您烦恼,您可以强制执行(即:调用
ToList()
或类似方法)。I would personally return an
IEnumerable<T>
orIList<T>
instead of ObservableCollection. There are many times when you may not need the full behavior ofObservableCollection<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 theIEnumerable<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).就个人而言,如果您只是使用它来填充 UI,我会返回
IEnumerable
。如果您不打算从中添加/删除项目,则无需返回List
。Personally I'd return an
IEnumerable<T>
if you're simply using it to populate the UI. No need to return aList<T>
if you aren't going to be adding/removing items from it.