Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP - Where to put the calls to the Data Access Layer?

Tags:

oop

vb.net

theory

I am implementing a Data Access Layer (DAL), which is basically a set of classes with (VB.NET) Shared functions to actually execute the database (CRUD) calls. I am trying to figure out the best place to place the calls to the DAL within the class hierarchy. Let me give an example.

Suppose I have a class Customer, with only standard ID, Name, Address1, etc. properties and maybe an overridden ToString function or so. I also have a DAL class with Shared methods, such as:

(pseudocode)

Namespace Dal

Public Class Customer

Public Shared Function Read(id As Integer) As Customer

Public Shared Function ReadList() As List(Of Customer)

Public Shared Sub Create(c As Customer)

'etc.

Now, I could call the Dal from the presentation layer like so:

Me.DataGridView1.Datasource = Dal.Customer.ReadList

However, is it not a good practice to have the presentation layer aware of the Dal at all? Should I instead put methods in the Customer object and call the Dal, like this?

Public Function ReadList() As List(Of Customer)
    Return Dal.Customer.ReadList()
End Sub

Public Sub Create()
    Dal.Customer.Create(Me)
End Sub

Would this be "cleaner" OOP? Or is it acceptable practice to let the presentation call the Dal, passing the business objects like my previous example:

Me.DataGridView1.Datasource = Dal.Customer.ReadList

Dim c As New Customer
c.Name = "Alpha Corporation"
c.Address1 = "123 Main Street"
Dal.Customer.Create(c)

Thanks for your feedback.

like image 930
HardCode Avatar asked Feb 20 '09 19:02

HardCode


1 Answers

The less your application knows about your DAL the better. There are several ways to do this and I think you are on the right track. I think you might want to look into the factory pattern for this implementation as you will be able to hide the DAL implementation behind the factory and return entities and collections of entities from the factory.

like image 93
Andrew Hare Avatar answered Sep 22 '22 03:09

Andrew Hare