Extending a Typed DataSet Using Partial Classes


Extending a Typed DataSet Using Partial Classes

After you have created your typed DataSet and configured all of your TableAdapters to run all of the queries that you need and annotated it so that all of the members have appropriate names, you still may find that you want to extend the functionality of your typed DataSet. For example, you may decide that you have several operations that will be performed frequently on the data, such as summing the total price of all orders within the DataSet. Rather than creating a separate utility class that takes a ConnectedOrderDataSet as a parameter, you can actually extend the class itself through the use of partial classes in C#.

To do this, you can either right-click the DataSet in the Solution Explorer and choose View Code, or you can right-click the design surface of the DataSet designer and choose View Code. If this is the first time you have done this, you'll see an empty partial class implementation like the one that follows:

namespace ConnectedDataSet {     partial class ConnectedOrderDataSet     {     } } 


To add your own custom functionality, all you have to do is add the methods you want, such as the method shown in the following code:

namespace ConnectedDataSet { partial class ConnectedOrderDataSet { public decimal SumAllOrders() {     decimal totalValue = 0.0M;     foreach (OrdersRow order in Orders)     {         foreach (OrderItemsRow orderItem in order.GetOrderItemsRows())         {             totalValue += orderItem.Price;         }     }     return totalValue; } } } 


With this method in place, you can call this method on the DataSet just like any other method:

Console.WriteLine("Total sum of all purchases in DataSet: ${0:00.00}",     orderData.SumAllOrders()); 




Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

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