In which section should I place my SqlDataReader code?

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

I have three main sections:

  • The DataAccess code is inside a folder named "BusinessLogic" inside my App_Code folder.

  • The business logic code is inside a folder named "DataAccess" inside my App_Code folder.

  • Presentation layer - all user interfaces

If, for example, I need to write a SqlDataReader to retrieve records from my database, where would I physically write the code? BLL or 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 Logic layer of the Business layer that I call, I write the SqlDataReader code here, or create another step and write the SQlDataReader code in the data access layer.

IE Should I add a method to the BLL that called the DAL? for instance

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

      

and then in my DAL I have a method to execute the actual SqlDataReader For example

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

      

I tried to learn from the lessons on the asp.net website, however for the DAL in the tutorial, they use datasets instead. Any help would be greatly appreciated.

0


a source to share


2 answers


My typical approach is what I jokingly call the 2.5 level 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 an IDataReader. This reader is then read to populate the object.



The database layer completes all database access requests and returns readers.

While not as clean as some people would like, the alternative is making dumb container classes for marshaling data between tiers, and I prefer to just use IDataReader.

Also, by using IDataReader rather than SqlDataReader, I am still loosely coupled to my DAL and can implement any form of resilience, not just SQLServer.

+2


a source


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

I won't argue that a little add-on is required in the learning phase, but I think if at some point it creates more confusion, you need to step back and rethink your approach.



Try to learn a little more about ASP.NET and don't be so tight on the semantics. If you keep an open mind and allow refactoring of your code, you can probably answer your own question.

0


a source







All Articles