I am used to Classic Asp, where I never used DAL/BLL concept, and now I am learning MVC and trying to stay away from BAD habits (Such as writing SQL queries within ASP page itself). I read about Data Access Layer and Business Logic Layer....they make sense, but i am trying to figure out how to put them into my current application.
Its a Shopping Cart Application.
Currently I am NOT using EF or SQL to Entities, plain old ADO.NET, where my Functions return DataTable.
Let me give you an example.
1 - I need to Return Products From SQL Table
2 - My Products Model Class will hold the SQL Table output
3 - and then I will show the output to View
Query involved to Bring Products
Select * From Products Where title = 'Bluh'
ProductsModelView.vb
Class ProductsModelView
Public title as string
Public sku as string
....etc
End Class
Now my View will simply Render the Result (of List(ProductsModelView))
Now my question is...how should I structure above steps into DAL & BAL layers.
A basic way to start is to create 3 projects:
In your DAL project you should create a repository
-class. What this class does is execute a query on the database and transform the DataTable into your model.
Your BLL project should have a service
-class. This class has a reference to the DAL and calls the method to obtain the list of objects you need (DAL handles DB-code). In this class you can apply logic.
NOTE: At the moment you don't seem to have any real logic in your app. That's OK, your service can just return the list from the DAL immediately. It will provide you with a place where you can add logic safely later, without affecting the data access code.
In the UI, your controller will call the service and pass the result to the view, which is then in charge of rendering the result.
This is a basic starting point. You could go further with this and completely make it loosely coupled. At the moment you still have a hard dependency from UI => BLL => DAL.
I recently wrote an article on how you can make sure you don't create hard dependencies: http://www.kenneth-truyers.net/2013/05/12/the-n-layer-myth-and-basic-dependency-injection/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With