views:

192

answers:

2

Hi,

I am fairly new to c# and am trying to write a n-tiered web application. To make sure that I put the logic and code in the right place I just have a question about where to put my code.

I have three main section:

  1. DataAccess code - inside a folder named "BusinessLogic" inside my App_Code folder.

  2. Business Logic code - inside a folder named "DataAccess" inside my App_Code folder.

  3. The presentation layer - All the UI's

If for instance I need to write a SqlDataReader to retrieve records from my database where where would I physically write the code? In the BLL or the DAL?

IE From the presentation layer I call the BLL code.

ContentBLL content = new ContentBLL();
//some code to call the BLL layer...

This is where I start to get confused. In the Business layer Logic layer that I am calling do I write the SqlDataReader code here or do I create one more step and write the SQlDataReader code in the Data Access Level.

IE In the BLL should I add a method that called the DAL? E.G

public static ContentBLL GetPageContent(intID)
{
return ContentDAL.GetItem(ID)
}

and then in my DAL I have a method to perform the actual SqlDataReader E.G

public static ContentBLL GetItem(int id)
{
//return the SqlDataReader code...
}

I have been trying to learn from the tutorials on the asp.net website however for the DAL in the tutorial they use datasets instead. Any help would be greatly appreciated.

+2  A: 

My typical approach is what I jokingly call the 2.5 tier method.

In this method, I use the following approach:

                Presentation Layer

                Businesss Object Layer / Data Serialization

                Database Service Layer

Each Business Object in the Business Layer has a constructor that accepts a IDataReader. This reader is then read to populate the object.

The database tier is wraps up all DB access requests and returns readers.

While this not as pure as some people would like, the alternative is making dumb container classes to marshal the data between tiers, and I prefer to just use an IDataReader.

Additionally, by using a IDataReader and not a SqlDataReader, I'm still loosely coupled with my DAL and can implement any form of persistance, not just SQLServer.

FlySwat
Hi FlySwat, thanks for the prompt response. I like the sound of your approach. Would you have a simple example you could show me to demonstrate?
Jason
A: 

This sounds to me like a classic case of over-engineering.

I wont argue the fact that a little over-engineering is required during the learning phase but I think if at any point it generates more confusion you need to step back and reconsider your approach.

Try to learn a little bit more about ASP.NET and don't be so stressed out about the semantics. If you keep an open mind and allow for refactoring your code, you will likely be able to give the answer to your question yourself.

Miky Dinescu