Creating Strongly Typed DataSet Objects

Questions That Should Be Asked More Frequently

  1. Q. I need to get the best possible performance out of my middle-tier components, so I'm using untyped DataSet objects on the server. But strongly typed DataSet objects are so handy when I build the client portion of the application. Is there a way to get the best of both worlds?

  2. A. Yes. Have your middle tier return and accept untyped DataSet objects. Use instances of strongly typed DataSet objects in the client application, and use the Merge method to import the contents of the untyped DataSet objects returned by the middle tier.

  3. Q. DataSet objects offer limited validation features. I can't set properties on either an untyped or a strongly typed DataSet to ensure that the value of a column falls between certain limits. Can I add my own code to the class file for added validation?

  4. A. Of course. You can add validation code to the desired properties of your strongly typed classes, but this was not an intended use of the strongly typed DataSet. Your validation code will not be stored in the .xsd file for the strongly typed DataSet class. If you modify the contents of the .xsd file, Visual Studio .NET will regenerate the class for the strongly typed DataSet and you'll lose the code that you've written.

    You have another option: create a new class that inherits from the strongly typed DataSet class and add your validation code there.

  5. Q. What other options do I have when I use the XML Schema Definition Tool to generate a class?

  6. A. If you check the "Using Annotations with a Typed DataSet" topic in the .NET Framework SDK, you'll find various options for controlling the names of some of the strongly typed classes generated by the tool. You'll also discover how to control how the properties on your strongly typed DataRow classes react when they contain null values. The XML Schema Definition Tool checks your .xsd file for the annotations listed in the documentation.

    The XML Schema designer does not let you add annotations to your DataSet object's .xsd file through the designer's user interface. You can switch to XML view and add the annotations by hand. Or you can add annotations to your DataSet class in code and then save the DataSet object's schema to an .xsd file using the DataSet object's WriteXmlSchema method.

    Adding annotations to your DataSet in code is actually fairly simple if you use the ExtendedProperties collection of the DataTable and DataColumn objects. The following code snippet is an example:

    Visual Basic .NET

    Dim ds As New DataSet() ds.DataSetName = "NameForYourNewClass" Dim tbl As DataTable = ds.Tables.Add("Table1") Dim col As DataColumn 'Set the name of the strongly typed DataRow for the DataTable. tbl.ExtendedProperties.Add("typedName", "MyTable1Row") 'Set the name of the DataTable property of the DataSet. tbl.ExtendedProperties.Add("typedPlural", "MyTable1Rows") col = tbl.Columns.Add("StringColumn", GetType(String)) 'Have the class return "<Null>" if the column contains null. col.ExtendedProperties.Add("nullValue", "<Null>") col = tbl.Columns.Add("StringColumn2", GetType(String)) 'Have the class return String.Empty if the column contains null. col.ExtendedProperties.Add("nullValue", String.Empty) col = tbl.Columns.Add("IntegerColumn", GetType(Integer)) 'Have the class return 0 if the column contains null. col.ExtendedProperties.Add("nullValue", "0") ds.WriteXmlSchema("C:\Desired\Path\To\YourNew.XSD")

    Visual C# .NET

    DataSet ds = new DataSet(); ds.DataSetName = "NameForYourNewClass"; DataTable tbl = ds.Tables.Add("Table1"); DataColumn col; //Set the name of the strongly typed DataRow for the DataTable. tbl.ExtendedProperties.Add("typedName", "MyTable1Row"); //Set the name of the DataTable property of the DataSet. tbl.ExtendedProperties.Add("typedPlural", "MyTable1Rows"); col = tbl.Columns.Add("StringColumn", typeof(string)); //Have the class return "<Null>" if the column contains null. col.ExtendedProperties.Add("nullValue", "<Null>"); col = tbl.Columns.Add("StringColumn2", typeof(string)); //Have the class return String.Empty if the column contains null. col.ExtendedProperties.Add("nullValue", String.Empty); col = tbl.Columns.Add("IntegerColumn", typeof(int)); //Have the class return 0 if the column contains null. col.ExtendedProperties.Add("nullValue", "0"); ds.WriteXmlSchema("C:\\Desired\\Path\\To\\YourNew.XSD");



Microsoft ADO. NET Core Reference
Microsoft ADO.NET (Core Reference) (PRO-Developer)
ISBN: 0735614237
EAN: 2147483647
Year: 2002
Pages: 104
Authors: David Sceppa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net