R – Is it possible to add an interface to a strongly typed DataSet in .NET

netstrongly-typed-dataset

I've got a bunch of strongly typed DataSet that each have two tables. One table is unique for each DataSet, but the second, "MetaData", has the same schema for each DataSet.

At runtime, I determine which DataSet I want to use, and populate the data table in the appropriate way from a DB.

I then want to populate the MetaData table. This would be done in the same way for each DataSet, so I'd like to use the same code. One obvious way to do this would be to have each of the DataSets implement an interface that would do the work.

The problem comes when I want to declare this interface (IMyInterface) for these DataSets.

Each strongly typed DataSet comes in a multitude of files. The first, and critical, file is the MyDataSet.Designer.cs file…this is a file that is auto-generated. There's a line near the beginning that reads:

public partial class MyDataSet : global::System.Data.DataSet

I could add my interface after this, but I have every reason to believe that it could/would be wiped out when this file is regenerated.

If I tell VS that I want to edit the code for the DataSet, it creates a new file for me called MyDataSet.cs. But the declaration in there looks like this:

partial class MyDataSet

If I tried to add an interface to this like this:

partial class MyDataSet : IMyInterface

it would look like I was trying to add a subclass.

What's the proper way to handle this? Change the designer file, and make sure VS doesn't ever regenerate it? Add both the superclass and the interface declaration to the other file? Something else entirely?

Best Answer

Wow. Nevermind. Despite not finding this initially when searching, upon retrying this query (after typing this long question), I found the answer quickly.

Apparently it's smart enough that it can mix and match them correctly, depending on whether it is a class to be inherited from, or an interface to be implemented.

MSDN says:

For example, the following declarations:

partial class Earth : Planet, IRotate { }
partial class Earth : IRevolve { }

are equivalent to:

class Earth : Planet, IRotate, IRevolve { }