Blog Home  Home Feed your aggregator (RSS 2.0)  
.Net Jonesie - Partial Classes, Generics & DataSets
A simple programmers blog
 
# Thursday, September 01, 2005
I have a large dataset that is exposed to the client via a web service.  At the server end I have extended the dataset to include methods that fill it via TableAdapters, e.g:
 
namespace MyWebService.MyDataSet {
    public partial class MyDataSet : DataSet {
 
        public void FillEmployees() {
            EmployeeTableAdapter eta = new EmployeeTableAdapter();
                    eta.Fill(this.Employee);
            }
    }
}
 
On the client side I have extended the proxy version of the dataset to add methods to rows and tables to manipulated data contained therein.
 
namespace MyProxy.MyDataSet {
    public partial class MyDataSet : DataSet {
 
        public List<MyDataSet.SomeRow> GetListOfEmployeesByName(string name) {
        // blah
      }
    }
}
 
I have a few methods that I need at the client and server end.  It would be nice to have this code in a shared library and just wrap this in the dataset partial class, e.g:
 
// ws version
using MyDataSet.Shared;
namespace MyWebService.MyDataSet {
    public partial class MyDataSet : DataSet {
 
      public List<MyDataSet.SomeRow> GetListOfEmployeesByName(string name) {
          MyDataSetHelper helper = new MyDataSetHelper();
          return helper.GetListOfEmployeesByName(this, name); 
      }
    }
}
 
// proxy version
using MyDataSet.Shared;
namespace MyProxy.MyDataSet {
    public partial class MyDataSet : DataSet {
 
            public List<MyDataSet.SomeRow> GetListOfEmployeesByName(string name) {
          MyDataSetHelper helper = new MyDataSetHelper();
          return helper.GetListOfEmployeesByName(this, name);     
      }
        }
}
 
// shared code
namespace MyDataSet.Shared {
    public class MyDataSetHelper {
            public List<MyDataSet.SomeRow> GetListOfEmployeesByName(MyDataSet ds, string name) {
          // blah
      }
  }
}
 
 
But this wont work because of the the different name spaces.  What I really need it a generic helper:
 
 
// shared code
namespace MyDataSet.Shared {
    public class MyDataSetHelper {
            public List<R> GetListOfEmployeesByName<T,R>(T ds, string name) {
          // blah
      }
  }
}
 
And I use this in the proxy and server code thus:
 
 
// ws version
using MyDataSet.Shared;
namespace MyWebService.MyDataSet {
    public partial class MyDataSet : DataSet {
 
      public List<MyDataSet.SomeRow> GetListOfEmployeesByName(string name) {
          MyDataSetHelper helper = new MyDataSetHelper();
          return MyDataSetHelper.GetListOfEmployeesByName<MyDataSet, MyDataSet.SomeRow>(this, name);     
      }
    }
}
 
// proxy version
using MyDataSet.Shared;
namespace MyProxy.MyDataSet {
    public partial class MyDataSet : DataSet {
 
            public List<MyDataSet.SomeRow> GetListOfEmployeesByName(string name) {
          return MyDataSetHelper.GetListOfEmployeesByName<MyDataSet, MyDataSet.SomeRow>(this, name);     
      }
        }
}
 
In some simple scenarios this might actually work, but if MyDataSetHelper.GetListOfEmployeesByName() needs to manipulate MyDataSet properties then I need to have a constraint on the generic so it knows what members it has, e.g:
 
namespace MyDataSet.Shared {
    public class MyDataSetHelper {
            public List<R> GetListOfEmployeesByName<T,R>(T ds, string name) where T : MyDataSet {
          // blah
      }
  }
}
 
But then we are screwed again because MyDataSet is in 2 different namespace and you can only have a constraint on a single base class.  I could however set the constraint on DataSet, e.g:
 
namespace MyDataSet.Shared {
    public class MyDataSetHelper {
            public List<R> GetListOfEmployeesByName<T,R>(T ds, string name) where T : DataSet {
          // blah
      }
  }
}
 
And then I'd have to use untyped methods on the dataset within the helper method and I wouldn't be able to use typed methods.
 
So, basically, the only solution is to include all the methods on the server dataset and create my own proxy to used a shared library where this is implemented.
 
Or maybe you have another solution?  Or maybe I've just confused you more than I confused myself?

 

Thursday, September 01, 2005 3:26:19 PM (New Zealand Standard Time, UTC+12:00)  #    Comments [1]   General | Visual Studio  | 
Sunday, September 11, 2005 12:36:21 AM (New Zealand Standard Time, UTC+12:00)
Peter you definitely confused me... not sure if I am more confused than you though ;)

Hey this comment is bit late, have you worked out a solution?

Not sure how it applies to you (because I am confused) but something I have uncovered in my Generic experiments etc... is that putting a non Generic interface on a Generic class can be very useful because you can reference the GenericClass without needing to know &lt;T&gt; by simply using the interface.

Maybe this helps?
Comments are closed.
Copyright © 2010 Peter G Jones. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.
Pick a theme: