C #: Access services to the dataprovider class working with sql statements - the right approach?

Is this a general and / or good approach?

In my ViewModel (Wpf) or Presenter (WinForms) I do this:

ICustomerService customerService = MyService.GetService<ICustomerService>(); ICustomerList customerList = customerService.GetCustomers(); 

The CustomerService class is as follows:

 public class CustomerService : ICustomerService { public ICustomerList GetCustomers() { return _customerDataProvider.GetCustomers(); } } public class CustomerDataProvider() { public ICustomerList GetCustomers() { // Open SQL connection, // get back a SqlDataReader and iterate it // in the loop write all data into a ICustomer object // add the ICustomer object to the ICustomerList // return ICustomerList object... } } 
0
c # sql service data-access-layer dataprovider
source share
2 answers

Retrieving data from a database is the plumbing fixture of your application. And the less plumbing you have to write yourself, the more productive you will be.

I usually go to LINQ directly in the client:

  sp_GetCustomersResult customers; using (var db = new DbDataContext(ConnectionString)) customers = db.sp_GetCustomers(); 

This works quite well and allows me to focus on adding customer value instead of database access layers.

+1
source share

I did not find much importance in declaring interfaces for business classes or for custom collections, as extension methods became available. I would get GetCustomers return IEnumerable<Customer> .

If you plan to work actively with business objects, you should consider using object / relationship mapping, such as NHibernate . Or use LINQ2SQL or Entity Framework to reduce the number of duplicate plumbing code you need to write.

0
source share

All Articles