tags:

views:

516

answers:

7

Hi everyone,

i read in one article that its not a good practice to pass dataset between different layers of .net web application.(DAL->BAL->Pages vice versa).Is that correct?

please give your suggestions.

Thanks SNA

A: 

As far as I've understood, the DataSet requires the db connection to be open, for as long as it is used, which will reduce performance in your application as it keeps the connection open until the content is rendered.

Instead, I recommend using generic collections, such as IEnumerable<myType> or IQueryable<myType>, where myType is a custom type which you fill with your data.

Tomas Lycken
You might be thinking of DataReader. DataSets are stored locally in memory and do not require the db connection to be open.
Chris Pebble
A: 

There's nothing wrong about passing DataSets from your DAL to your BAL.

I think this stackoverflow question on DAL best practices sums up the two schools of thought pretty well.

I am in the middle of a "discussion" with a colleague about the best way to implement the data layer in a new application.

One viewpoint is that the data layer should be aware of business objects (our own classes that represent an entity), and be able to work with that object natively.

The opposing viewpoint is that the data layer should be object-agnostic, and purely handle simple data types (strings, bools, dates, etc.)

Chris Pebble
A: 

Hi Swapna,

There is no problem with passing dataset across layers. If you observe, you will notice that passing dataset is by reference and not by value.So there is no issue of performance here. Now what you read is also right, but you have to understand the context. If you are passing the dataset across remote boundaries, that is not a recommended practice.

rAm
Just because the data isn't copied doesn't mean there's no performance concern. Using a dataset means keeping entire result sets in memory, and that can cause problems. Datareaders and IEnumerables are often a better choice.
Joel Coehoorn
Also, the specific concern has more to do with coupling than with performance. The normal way you end up with a dataset in the first place is as the result of sql query. You can use them for other things, but that is less common. Pass the dataset version of those query results around couples implementation details of the data layer to other layers.
Joel Coehoorn
Though a lot of data might exist in memory, the advantages of the disconnected architecture are hard to ignore.I don't deny the coupling issue. However with proper consideration to design we can address the issue of normal of ending up with the SQL query.We also have the added advantage of the dataset features at our use (constraints etc), when compared to normal business entities. I agree it may not suit all kinds of applications, but on a case by case basis datasets offer definite advantage over business entities.
rAm
A: 

There's nothing fundamentally wrong with that doing that. Although the basic idea of having a DAL, BLL and UI layer is so that each layer can abstract what's beneath it. E.g. the BLL shouldn't have any knowledge of how the database is structured because the DAL abstracts that away. If a dataset is being loaded in the DAL then passed straight through the BLL to the pages, it kind of sounds like the BLL is pointless.

d4nt
A: 

On the one hand, the problem with datasets and datatables is that they expose database implementation details like column names and types outside of your data access layer. Change a column name in your database or query and odds are that change is propogated to your dataset as well, forcing a re-compile of any tier that uses the dataset. So if you retrieve data into a dataset you should convert it to use strongly-typed business objects before passing it on.

On the other hand, a dataset doesn't care what kind of database it belongs to. You can use them with access, oracle, sql server, mysql, anything. So there is some generic-ness there that can make them useful when passing data between tiers. And just like the business layer shouldn't care about database details the data layer shouldn't really need to know what the the business objects are, so there's a good argument that you should use them for data interchange at that level.

My normal procedure is to have a sort of one-way "translation" tier between the business and data access layers, so that the business layer only deals with business objects and the data layer only returns generic data. This currently takes one of two forms:

  1. I'll write my data access methods to return datatables or datareaders, the the translation tier will use a factory pattern to convert those rows into the desired strongly-typed business objects.
    or
  2. I'll use C# iterator blocks to convert a datareader into an IEnumerable<IDataRecord> in the data access layer and the translation tier will use them to change that IEnumerable<IDataRecor> into an IEnumerable<MyBusinessObject>, such that the code only ever iterates over the result set one time.
Joel Coehoorn
Why does a dataset have to be a replica of database. It design of dataset can be driven by the UI and the business logic needs. The logic to put this in db can be handled in DAL. To summarize why can a dataset(or typed dataset) not be used as a business entity?
rAm
A dataset doesn't _have_ to mimic the database. That's the whole point of my 2nd paragraph. But if you look at code out there, I think you'll find that this is what generally happens.
Joel Coehoorn
A: 

The strongest statements often seen about DataSet is not to pass it into or out of a web service. That goes beyond exposing implementation details, and includes exposing details of the platform (.NET).

Although it's possible to change "table" and "column" names in a DataSet from those in the underlying database, you're still largely stuck with the underlying structure of the database. To abstract that, I would use Entity Framework. It allows you, for instance, to define a "Customer" entity which takes data from multiple tables and puts it into a single entity. Code using the entity doesn't need to know whether it is implemented as one table, two tables, or whatever.

Even there, you should not pass these entities outside of a web service boundary. They still pass implementation details outside of the implementation. For instance, properties of the base classes get serialized, even though these are just implementation details.

John Saunders
A: 

There is nothing wrong with passing around datasets but it's not a great practice.

Pros:

Easy to pass around and use in .NET apps

No having to code wrapper classes

Lots of functionality built into DataSets

Cons:

Data type that is not really type safe.

Your data field names can change all parts of your app will compile fine until they blow up at runtime.

Heavy object. Dataset does a ton of stuff and you probably don't need 90% of it.

Having non .NET apps talk to your DAL or BAL is going to be very clean.

Kelsey