Garth Kidd
Garth Kidd

Reputation: 7494

How can I mark generated methods obsolete?

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?

Upvotes: 4

Views: 1292

Answers (4)

Gael Fraiteur
Gael Fraiteur

Reputation: 6857

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.

Upvotes: 1

emragins
emragins

Reputation: 5157

Use the new keyword in your non-generated partial class:

public partial interface ICaseRepository
    : IRepository<Case>
{
    void Delete(int id);

    [Obsolete("Use Delete(int id) instead.")]
    new void Delete(Case entity);
}

This will allow all current usages of the generated method to generate compile time warnings.

Upvotes: 1

Joshua
Joshua

Reputation: 43268

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.

Upvotes: -1

J&#233;r&#244;me Laban
J&#233;r&#244;me Laban

Reputation: 5282

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".

Upvotes: 1

Related Questions