C#: Services access the dataprovider class running the sql statements - correct approach?
Asked Answered
B

2

0

is this a common 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 looks like this:

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... 
    }
}
Bifocal answered 5/7, 2010 at 18:34 Comment(0)
L
1

Retrieving data from a database is the plumbing 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 fairly well, and lets me focus on adding customer value instead of database access layers.

Linneman answered 5/7, 2010 at 19:11 Comment(0)
R
0

I haven't found much value in declaring interfaces for business classes or for custom collections since extension methods became available. I would have GetCustomers return IEnumerable<Customer>.

If you plan on working extensively with business objects then you should look into using an object/relation mapper such as NHibernate. Or use LINQ2SQL or Entity Framework to reduce the amount of repetitive plumbing code you have to write.

Rom answered 5/7, 2010 at 22:12 Comment(2)
Well not extensively...about 10 different entities not more. Most of them are aggregated somehow into the other entity. If I would go for a ORM I would choose EF 4 because I used learned it for 8 months but I wasnt satisfied by the sql ce support bundled with EF 4 for example I do not have identity columns instead I have to fight around with Guids in the code ... terrible... or the designer support is very bad. Stuff like this makes me wanting using the pure sql where many good IDEs exist and I have the full sql power, no restrictions by ORM tool. At the moment I think about using sqlite.Bifocal
What has Extension methods to do with IEnumerable<Customer> ?Bifocal

© 2022 - 2024 — McMap. All rights reserved.