Reputation: 7494
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
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
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
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
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 :
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