3

What is the best practice to return data objects from Data Access Layers to Interface?

Currently, I have a layer that communicates with database and returns DataTable to business Layer and then Business Layer instatiates the Business Objects and returns to the interface. Isn't that pathetic or poor practise?

My questions are:

-What is the best way to return Data Objects from DAL?

-If the DAL shouldn't return Data Objects, then how do I get the data to interface for DataBinding?

This is not a problem but a best practice issue.

Thanks everyone in advance

Bilal
  • 31
  • 1

2 Answers2

2

Have you looked at the Repository Pattern?

http://msdn.microsoft.com/en-us/library/ff649690.aspx

http://martinfowler.com/eaaCatalog/repository.html

CharlesC
  • 350
  • 2
  • 8
0

I'm not sure that the DAL shouldn't return Data Objects - there are many personal preferences and it depends on factors such as the scale of the application. In the majority of the (smallish) applications I build, I used a datareader in the DAL rather than filling and passing around DataTables. The DAL uses the datareader to populate business objects that are then returned as a collection to the service layer.

In a more complicated scenario where tables don't always equate to business objects, I've used the datareader to populate DTOs (Data Transfer Objects - very simply classes that just contain variables representing the table structure and no logic) that are returned as a collection to the service layer. The Service Layer constructs the business objects from one or more types of DTO.

I'm not saying this is a definitive answer though (The problem with 'best practice' questions!) but hopefully my experience can give an insight (In what to do or what not to do is debatable!). I am interested myself to see what answers others can contribute to this. I suppose one answer you'll likely get is to investigate the use of an ORM!

Kate
  • 1,556
  • 1
  • 16
  • 33