Reputation: 15807
In my experience the application layer should reference the business layer and the business layer should reference the data layer. I want to make a change to an app so that the application layer references the data layer directly as shown below:
Imports System.Data.SqlClient
Public Class ApplicationLayerClass
Public Function ProcessAllPersons()
Dim data As New DataLayerClass
Dim list As List(Of Person) = data.getAllPersons()
For Each person In list
'Call this function from the application client. Do some complex work on the person here.
Next
End Function
End Class
Public Class DataLayerClass
Public Function getAllPersons() As List(Of Person)
Dim list As List(Of Person) = New List(Of Person)
Dim p As New Person
Dim objCommand As New SqlCommand
Dim strConString As String = "Data Source=IANSCOMPUTER;Initial Catalog=Test;Integrated Security=True"
Dim objCon As New SqlConnection
objCon.ConnectionString = strConString
objCon.Open()
objCommand.Connection = objCon
objCommand.CommandText = "select * from person "
Dim objDR As SqlDataReader = objCommand.ExecuteReader
If objDR.HasRows Then
Using objCon
Do While objDR.Read
p.Name = objDR("Name")
p.age = objDR("Age")
list.Add(p)
Loop
End Using
End If
Return list
End Function
End Class
Public Class Person
Public Name As String
Public age As String
End Class
Alternatively I could create a class in the business layer that uses the adaptor pattern (http://en.wikipedia.org/wiki/Adapter_pattern
) i.e. a function called BusinessLayerClass.ProcessAllPersons, which is called by ApplicationLayerClass.ProcessAllPersons and calls DataLayerClass.getAllPersons. Which option is more appropriate? I suppose it depends to some extent on the problem domain.
Upvotes: 1
Views: 516
Reputation: 6791
If you have no intention of changing your database provider and you have the ADO.net skills that a business layer would protect developers from, then you don't need a business layer.
However, where are you going to put your business logic?
Upvotes: 0
Reputation: 14064
the business layer should reference the data layer
This is one approach but arguably not the best if you're aiming at maximum decoupling between your modules and being able to change your data access layer easily without touching your business layer (this is called Persistence Ignorance).
Have a look at Onion Architecture and/or Hexagonal Architecture. These architectural styles emphasize on your business layer being at the core of the system and independent from any other layer.
Concretely, how it works is you define abstractions for data access objects in your business core, which will be implemented in peripheral modules and used by the application layer.
http://jeffreypalermo.com/blog/the-onion-architecture-part-1/
http://alistair.cockburn.us/Hexagonal+architecture
http://blog.kamil.dworakowski.name/2010/08/from-layers-to-hexagon-architecture.html
Upvotes: 0
Reputation: 5224
If you have a valid reason for calling the data layer directly, then do it. If you add a pass-through function in the business layer, then all you've done is added more code for no apparent benefit.
Now, if your business layer is exposed via an interface, IBusinessLayer for example, then adding a ProcessAllPersons() function to it and having it pass the call directly to the data layer makes more sense and consistency. This is what I would recommend.
Upvotes: 1