C#:服务访问运行sql语句的dataprovider类-正确的方法?这是一种常见的和/或好的方法吗?在我的ViewModel(Wpf)或Presenter(WinForms)中,我这样做:ICustomerServicecustomerService=MyService.GetService();ICustomerListcustomerList=customerService.GetCustomers();CustomerService类如下所示:}}publicclassCustomerDataProvider(){publicICustomerListGetCustomers(){//打开SQL连接,//取回SqlDataReader并迭代它//在循环中将所有数据写入ICustomer对象//将ICustomer对象添加到ICustomerList//返回ICustomerList对象...}}从数据库中检索数据是应用程序的管道。您自己编写的管道越少,您的工作效率就会越高。我一般直接在客户端访问LINQ:sp_GetCustomersResultcustomers;使用(vardb=newDbDataContext(ConnectionString))customers=db.sp_GetCustomers();这非常有效,让我专注于增加客户价值而不是数据库访问层。由于扩展方法可用,我看不出为业务类或自定义集合声明接口有多大价值。我会让GetCustomers返回IEnumerable。如果您计划广泛使用业务对象,那么您应该考虑使用对象/关系映射器,例如NHibernate。或者使用LINQ2SQL或EntityFramework来减少您必须编写的重复管道代码的数量。以上是C#学习教程:C#:Servicesaccessthedataproviderclassthatrunsthesqlstatement-thecorrectway?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
