PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 27, 2022

[FIXED] How can I mark generated methods obsolete?

 April 27, 2022     .net, ado.net, attributes, warnings     No comments   

Issue

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?


Solution

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



Answered By - Jérôme Laban
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing