views:

214

answers:

2

Assume I have a form that has say 5 text boxes and I need to pull some data from a database then set those 5 text boxes with that data.

So assume I have 3 basic layers:

  • the code behind for a default.aspx page

  • a BLL class that contacts the DAL

  • and a data access layer (DAL)

But I've heard and read that the default.aspx page should not know about sqldatareader, in fact you probably dont want to add the sqlclient namesspace to the default.aspx code behind page.

So I used to do something like this:

protected sub DisplayCustomerData()

Dim s as SqlDataReader
Dim b as BLL

b=new BLL();
s = b.GetCustomerData();

//then take s and simply set each text field
if s.read()
TextBox1.Value = s("MyField1")
end if
end sub

Then the BLL would simply call the DAL to call a stored procedure in SQL Server and do a CommandObject.ExecuteReader, get the results pass it back to BLL and BLL would then send it to the code behind page of default.aspx.

I can provide full source code if that doesn't make sense. My question is if its bad that default.aspx actually declares and knows about an SQL Data Reader then what is the better way to do this? What datatype should I be using to avoid having an sqldatareader in my interface code ?

+4  A: 

You've introduced multiple layers, without actually separating concerns. By using SqlDataReader in your front end, it needs too much knowledge of how the data is retrieved.

Typically you'd have a Customer Business Object or Data Transfer Object that contains the retrieved data. Your DAL would returns objects of the Customer type, rather than a reference to a SqlDataReader.

protected sub DisplayCustomerData()

    Dim customer as Customer
    Dim b as BLL

    b=new BLL();
    customer = b.GetCustomerData();

    If Not customer is Nothing Then
        TextBox1.Value = customer.MyField
    End If
end sub

Update:

What you call the BLL in your example, wouldn't really do anything. There is just data access and presentation of that data. The DAL itself builds the Customer object.

The way you're using b, it really is a Data Access Object. It doesn't really have anything to do with business logic. Business logic would be something like determining the discount percentage this customer receives, based on past orders.

Thorarin
Ok I am sort of getting it, but then is it possible you could add to the answer how the b object can call GetCustomerData, is this object accessing the DAL ? I thought that only the BLL should be accessing the BLL?
Typically, layers only access layers directly below them. The UI Layer would access an Application Logic Layer, which would access the Data Access Layer.
Thorarin
Right but are you saying that the Application Logic Layer would then call the DAL and the DAL in return would be returning the customer object? Or is the DAL passing an SQLDataReader to the application logic layer and setting a customer up there? I would think the DAL would call the stored procedure. Get the data in an SQLDataReader and then after it gets the data from the SqlDataReader it would create a New Customer object, set its fields of the customer object with the fields returned by the sqldatareader and then return the customer object to the BLL in turn returning it to interface?
The data access layer should return an object. It should not leak any information about *where* it gets the data from, otherwise it isn't actually a layers. Its job is to *encapsulate* the data access entirely.
Greg Beech
@Greg I understand that, but given I am pulling the data from a database do you agree with what I said in my comment. That it would call the sproc, get the data, store it in a sql data reader and then create the customer object initialize its values with the data from the sqldatareader and then return back to the BLL which in turn returns the customer object back to the interface?
@unknown: Yes, that is correct. I've slightly updated the answer again.
Thorarin
@Thorarin - you are awesome thank you! I hate to say this but I am a lone developer working at a company not only developing the app but also the db, training, db admin, network admin..but I want to become a better programmer and the only thing I am having problems with is fully understanding layers and where stuff needs to take place. My code always looks like functional code going down...I may throw in some objects / layers but I end up using the layers inappropriately. I just bought Code 2 Complete to help me out..can you recommend anything else that would help me become better?
@Thorarin - also regarding your comment, but I thought you said the DAL should not talk to the Interface? In that case it is...I thought the interface should pass it to BLL and BLL passes to DAL...Even if the BLL is just retreiving the data from the DAL isn't that better then directly calling the DAL from the front end ?
A: 

Layers are about encapsulation. The idea of a multi-tier architecture is to separate concerns into separate encapsulated layers, e.g.

Data Access Layer

Encapsulates all information about how data access is performed. Does not accept or return any types that indicate how data access is performed (i.e. it does not accept or return data readers, data sets, or anything like that). Its job is to persist and retrieve objects. You should ideally be able to swap out your data access layer with one that has the same contract but uses an entirely different persistence mechanism.

Business Logic Layer

Performs business logic functions, whatever they may be for your domain. Does not care about how it gets data, and does not know whether the caller is a web site, web service, windows forms application, etc. (i.e. it does not deal with any UI objects).

Presentation Layer(s)

Deals with presentation of stuff, whether that's in the form of an API or a web site or whatever. You should be able to have multiple parallel presentation layers that can use your business logic layer if you've written it in the right way. This layer does not call the data access layer.


Note that there is no particular requirement for this to be the actual layer structure you use, it's just a reasonably conventional one. We, for example, insert additional layers for things like cache management.

Greg Beech
@Greg thanks, I understand these thigns completly, its just hard for me to understand them via code...I know about tiers vs layers, I have read on all these topics. Its only when i sit down and code it is where the trouble begins...