Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

Typed DataSet and Business Objects


:P
On this page:

I've been giving some thought to the typed DataSet debate recently. For the most part my preference has been to use untyped DataSets because frankly it is easier to not have to worry about synching database and code every time a change is made to the database.

 

For a long time I've completely ignored typed DataSets, but last week I dug in and took a closer look at what is really involved to make this work in applications.

 

There's no doubt that typed DataSet provide a good amount of functionality and make some things easier. However, what I found is that If you are using business objects that provide a generic data abstraction layer you end up having to some work to have typed DataSet work within that framework.

 

Particularly you have to make sure that you pre-create your DataSet before letting the framework code create one for you so that framework knows to use your typed DataSet. The biggest problem is that you can't recast a typed DataSet from a regular DataSet, but the data must be loaded into it from the start. This is no big deal if you do this in normal front end data code but requires some setup in a business object environment.

 

For my business objects this means something like:

 

public class busCustomer : wwBusiness

{

public busCustomer()

{

this.Tablename = "wws_customers";

this.ConnectionString = App.Configuration.ConnectionString;

 

this.DataSet = new dsCustomers();

}

}

 

Not a big thing, but remembering this and enforcing it at all business objects is an issue.

 

Probably the biggest issue I see is related to work flow with typed DataSets. You generate typed DataSets by dropping tables onto the XSD designer or manually building XSD definitions. The process is not difficult but if you have a lot of tables that change frequently synchronizing the DataBase and the schema for the typed DataSet becomes a big issue.

 

You have to manually drop the tables onto the XSD designer to get the generation to occur. This means you have to manually manage the schema from which the DataSet is generated. Make a change to the database and you have to make sure you remember that the XSD is also updated. To make things worse in my environment every business object uses its own DataSet, so I would tend to require many separate XSD files (essentially one or more for each table that a business object 'owns').

 

The other issue I have with the typed DataSet is that if you look at the code generated there's a fair amount of overhead. Specifically the first time run a query and fill a table all the tables are created along with their column definitions. Although this provides a good typed interface there's a fair amount of overhead here that is incurred everytime you instantiate and load data into the typed DataSet. While this may be OK in Windows Forms apps in a Web app the constant reloading will occur a fair amount of overhead.

 

As a review: To get a DataRow then you have to do something like this:

 

// *** Noramlly you'd do this in the bus obj

// *** for demo do raw Execute here

int Result = Customer.Execute("select pk,Address from wws_customers","wws_customers");

 

// *** Standard DataRow access

Customer.DataRow = Customer.DataSet.Tables["wws_customers"].Rows[0];

string Company = (string) Customer.DataRow["Company"];

Customer.DataRow["Company"] = "13 Hanalou Road";

 

// *** Typed Data Row access

dsCustomers.wws_customersRow CustRow = (dsCustomers.wws_customersRow) Customer.DataRow;

Company = CustRow.Company;

CustRow.Address = "32 Elm Street";

 

It seems to me that the key feature of a typed DataSet is the typed DataRow. The rest of the functionality that the typed DataSet is useful but not crucial and frankly not worth the overhead IMHO.

 

One way to approach this might be to build something a bit simpler - something like a wrapper for a DataRow that can be assigned on an as needed basis.

 

I'll have more on this topic in the next entry...


The Voices of Reason


 

JG
January 17, 2004

# re: Typed DataSet and Business Objects

One of the advantages of using typed datasets that you are missing is that you will catch typos in table and column names at compile time not run time as with un-typed data sets. In fact using intellisense eliminates those typos all together. May not be a big deal on a small system, but on a large system with many business objects the typos may not be found until deployment.

Rick Strahl
January 17, 2004

# re: Typed DataSet and Business Objects


No, not missing actually. I agree the compile time functionality and Intellisense is a big bonus, but if you look at the next post you see that there's an easy way to address this without having deal with all of typed dataset. The problem I see is that there is so much initial overhead for a typed dataset which is a major problem if you have a lot of stuff in there especially in Web applications where you have to recreate on every hit. That plus the hassle of creating it manually.

Damon Allison
January 27, 2004

# re: Typed DataSet and Business Objects

If you are using non-typed datasets (large or small system) and not catching typos in column names until deployment, I think you may need to test your code a bit better. Catching typos should be caught right away, I don't think intellisense advantage even comes close in comparison to having better performance.

Rick Strahl
January 27, 2004

# re: Typed DataSet and Business Objects

Damon, not sure what you are saying here. What do you mean by catching typos should be caught right away? If you're using untyped datasets you will catch those only at runtime, which leaves testing and hopefully not deployment as catching these issues.
Performance two is a double edged sword. Depends on how many rows you access - untyped data access is likely to be faster on single row access, but typed row access (with column access) will be faster for lots of iterations. It's a trade off.


APTExan
July 15, 2004

# re: Typed DataSet and Business Objects

The flexibility of dynamically(at runtime) creating the datatset is not possible with Typed Dataset.
What you all think about sacrificing this flexibility?

Rick Strahl
July 15, 2004

# re: Typed DataSet and Business Objects

This is one of the big problems with a Typed DataSet. I think you can get around this to some extent by using a DataRow wrapper as described in the follow on entry, because with that you don't have to have a fixed structure in place. So if you run a dynamic query and return only two fields you can still use the wrapper assuming that the field names remain intact (ie. no AS clauses or foreign fields). Even that is limiting.
Ultimately I see no way to do away entirely with untyped DataSets/Tables. There will always be some situations where your data will need to be dynamically accessed at runtime.

Rick Strahl
October 04, 2006

# Typed DataRow Generation - Rick Strahl

Yesterday I talked about some of the things that I don’t like about typed DataSets, so I decided to do something about it. My problem with the DataSet generator is that it generates A LOT of code. And a lot of that code runs even if you don't use a table in the database for example. The generated code does things like create new datatables for all the tables even if you don't ask for all of them. This is relatively slow because the table fields are parsed into Column collections for all fields.

Rick Strahl's Web Log
February 11, 2007

# Typed DataRow Generation - Rick Strahl's Web Log

Yesterday I talked about some of the things that I don’t like about typed DataSets, so I decided to do something about it. My problem with the DataSet generator is that it generates A LOT of code. And a lot of that code runs even if you don't use a table in the database for example. The generated code does things like create new datatables for all the tables even if you don't ask for all of them. This is relatively slow because the table fields are parsed into Column collections for all fields.

ASP.NET Forums
June 28, 2007

# Architecture-BLL - ASP.NET Forums


West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024