views:

31

answers:

2

Hello,

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... 
    }
}
+1  A: 

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.

Andomar
A: 

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.

Jamie Ide
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 Guid`s in the code ... terrible... or the designer support is very bad. Stuff like this makes me wanting using the pure sql where many good IDE`s exist and I have the full sql power, no restrictions by ORM tool.At the moment I think about using sqlite.
msfanboy
What has Extension methods to do with IEnumerable<Customer> ?
msfanboy