views:

579

answers:

3

I'm using a strongly typed DataSet for which manually adding rows will be error prone. I'm providing factory methods to create rows properly. I'd like to guide consumers of my class away from the generated Add*Row methods on the *Table classes.

Adding Obsolete attributes to the generated methods would do the trick. Sadly, they would be removed the next time the code is generated.

I can't use partial methods in the non-generated code because the VS2008 DataSet designer doesn't use them.

MyType.Dataset.Designer.cs looks a little like this:

public partial class ThingyDataTable : global::System.Data.DataTable, global::System.Collections.IEnumerable {
    // I'd love an [Obsolete("Please use the factory method.")] here.
    // I can't use a partial method, as this method isn't partial. 
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    public ShelfRow NewShelfRow()  
        return ((ShelfRow)(this.NewRow()));
    }
 }

Is there some way I can add an Obsolete attribute from MyType.cs? Trying a C-style prototype doesn't work, as the member is already defined. Jamming in partial doesn't work because the generated member isn't partial.

// BROKEN EXAMPLE:
public partial class ThingyDataTable {
    // I'd love an [Obsolete("Please use the factory method.")] here.
    // I can't use a partial method, as this method isn't partial. 
    [Obsolete("Please use the factory method.")]
    public ShelfRow NewShelfRow(); // ERROR: member already defined.
}

Is there some other way I can mark the generated method Obsolete?

How else could I warn consumers away from the generated method?

+1  A: 

The custom tool used to generate the designer file from an XSD file is not that much configurable out of the box.

But you can either :

  • Remove it from the "Properties" dialog on the xsd file for your typed dataset, that is setting the field "Custom Tool" from "MSDataSetGenerator" to empty. This way, you control when your code is generated. But this is error prone, as you may forget to do this in the future...
  • You can create your own custom tool that will call the xsd.exe tool, and place the attribute you want at the appropriate places, but that implies you install the custom tool on all your dev stations and build servers. (See this post for pointers)
  • Or, at last, you can force the code generator to create an internal implementation of your DataSet, and make the classes implement a set of public interfaces that you can allow your clients to use. Your Dataset classes will not be usable directly anywhere from outside your assemblies, but your interfaces will.

The latter is a bit less "automagic", as you will need to reflect changes in the DataSet schema in your interfaces, but you control everything and more "factory-friendly".

Jerome Laban
Thanks for that! It all sounds more painful than training the users properly, though, so I'll implement the training rather than your fine suggestions. :)
Garth T Kidd
A: 

It's generated code, right. Nothing's stopping you from using the output of the generator as input to a further generator that adds the [Obsolete] attribute for you.

Joshua
+1  A: 

Another option is to use PostSharp 1.5 and the new CustomAttributeInjector aspect (see online documentation).

Basically, make a CompoundAspect, and add a CustomAttributeInjectorAspect to anything you want. This should work.

-gael

Gael Fraiteur
Thanks, Gael! Some example code would make that my chosen answer. Sadly, there's none in the PostSharp documentation, and the code on the PostSharp blog isn't that clear. Soon, perhaps.
Garth T Kidd