tags:

views:

184

answers:

2

I'm using .NET typed datasets on a project, and I often get into situations where I prefetch data from several tables into a dataset and then pass that dataset to several methods for processing. It seems cleaner to let each method decide exactly which data it needs and then load the data itself. However, several of the methods work with the same data, and I want the performance benefit of loading data in the beginning only once.

My problem is that I don't know of a good way or pattern to use for managing dependencies (I want to be sure I load all the data that I'm going to need for each class/method that will use the dataset). Currently, I just end up looking through the code for the various classes that will use the dataset to make sure I'm loading everything appropriately.

What are good approaches or patterns to use in this situation? Am I doing something fundamentally wrong? Although I'm using typed datasets, this seems like it would be a common situation where prefetching data is used.

Thanks!

+1  A: 

I think the "pattern" you're looking for is probably just lazy loading, or deferred loading. Your typed dataset can have the structure all formed that all your methods or classes are going to need, but the data isn't filled until some method/class tries to access that certain portion of the data.

A good example is names and addresses. You may have a list of names and each of those names may have multiple addresses. The methods/classes that don't need to access the addresses don't cause the addresses to load. But as soon as any method or class tries to access the addresses for a given name, the addresses for that name are loaded. Any subsequent uses of the addresses for that name will find the data already loaded.

Implementing this in typed datasets will be a rather manual process. (And every time I work with typed datasets I end up hating them more and more.) You may want to look at LINQ to SQL as this functionality is built in. There are other similar frameworks that have this built in as well.

sliderhouserules
+1  A: 

You could use something similar to a Composite pattern where you would have each class add some data requirement, then pre-fetch the data, then process the data.

for each Class in UsedClasses

DataSet.AddRequirement(Class.Requirements)

DataSet.Prefetch

for each Class in UsedClasses

Class.Process(DataSet)

Jim