<p>First off, I understand the reasons why an interface or abstract class (in the .NET/C# terminology) cannot have abstract static methods.  My question is then more focused on the best design solution.</p>

<p>What I want is a set of "helper" classes that all have their own static methods such that if I get objects A, B, and C from a third party vendor, I can have helper classes with methods such as</p>

<pre>
AHelper.RetrieveByID(string id);
AHelper.RetrieveByName(string name);
AHelper.DumpToDatabase();
</pre>

<p>Since my AHelper, BHelper, and CHelper classes will all basically have the same methods, it seems to makes sense to move these methods to an interface that these classes then derive from.  However, wanting these methods to be static precludes me from having a generic interface or abstract class for all of them to derive from.</p>

<p>I could always make these methods non-static and then instantiate the objects first such as</p>

<pre>
AHelper a = new AHelper();
a.DumpToDatabase();
</pre>

<p>However, this code doesn't seem as intuitive to me.  What are your suggestions?  Should I abandon using an interface or abstract class altogether (the situation I'm in now) or can this possibly be refactored to accomplish the design I'm looking for?</p>

[{'answer_id': 14622, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 1, 'selected': False, 'text': '<p>In C# 3.0, static methods can be used on interfaces as if they were a part of them by using extension methods, as with DumpToDatabase() below:</p>\n\n<pre><code>static class HelperMethods\n {  //IHelper h = new HeleperA();\n    //h.DumpToDatabase() \n    public static void DumpToDatabase(this IHelper helper) { /* ... */ }\n\n    //IHelper h = a.RetrieveByID(5)\n    public static IHelper RetrieveByID(this ObjectA a, int id) \n     { \n          return new HelperA(a.GetByID(id));\n     }\n\n    //Ihelper h = b.RetrieveByID(5)       \n    public static IHelper RetrieveByID(this ObjectB b, int id)\n     { \n          return new HelperB(b.GetById(id.ToString())); \n     }\n }\n</code></pre>\n'}, {'answer_id': 14633, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 2, 'selected': False, 'text': '<p>I personally would perhaps question why each of the types need to have a static method before even thinking further..</p>\n\n<p>Why not create a utlity class with the static methods that they need to share? (e.g. <code>ClassHelper.RetrieveByID(string id)</code> or <code>ClassHelper&lt;ClassA&gt;.RetrieveByID(string id)</code></p>\n\n<p>In my experience with these sort of "roadblocks" the problem is not the limitations of the language, but the limitations of my design..</p>\n'}, {'answer_id': 14641, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': 2, 'selected': False, 'text': '<p>How are ObjectA and AHelper related? Is <code>AHelper.RetrieveByID()</code> the same logic as <code>BHelper.RetrieveByID()</code></p>\n\n<p>If Yes, how about a Utility class based approach (class with public static methods only and no state)</p>\n\n<pre><code>static [return type] Helper.RetrieveByID(ObjectX x) \n</code></pre>\n'}, {'answer_id': 14655, 'author': 'jerhinesmith', 'author_id': 1108, 'author_profile': 'https://Stackoverflow.com/users/1108', 'pm_score': 0, 'selected': False, 'text': '<p>How do I post feedback on Stack Overflow?  Edit my original post or post an "answer"?  Anyway, I thought it might help to give an example of what is going on in AHelper.RetrieveByID() and BHelper.RetreiveByID()</p>\n\n<p>Basically, both of these methods are going up against a third party webservice that returns various a generic (castable) object using a Query method that takes in a pseudo-SQL string as its only parameters.</p>\n\n<p>So, AHelper.RetrieveByID(string ID) might look like</p>\n\n<pre>\npublic static AObject RetrieveByID(string ID)\n{\n  QueryResult qr = webservice.query("SELECT Id,Name FROM AObject WHERE Id = \'" + ID + "\'");\n\n  return (AObject)qr.records[0];\n}\n\npublic static BObject RetrieveByID(string ID)\n{\n  QueryResult qr = webservice.query("SELECT Id,Name,Company FROM BObject WHERE Id = \'" + ID + "\'");\n\n  return (BObject)qr.records[0];\n}\n</pre>\n\n<p>Hopefully that helps.  As you can see, the two methods are similar, but the query can be quite a bit different based on the different object type being returned.</p>\n\n<p>Oh, and Rob, I completely agree -- this is more than likely a limitation of my design and not the language. :)</p>\n'}, {'answer_id': 14657, 'author': 'DancesWithBamboo', 'author_id': 1334, 'author_profile': 'https://Stackoverflow.com/users/1334', 'pm_score': 0, 'selected': False, 'text': '<p>Are you looking for polymorphic behavior?  Then you\'ll want the interface and normal constructor.  What is unintuitive about calling a constructor?  If you don\'t need polymorphism (sounds like you don\'t use it now), then you can stick with your static methods.  If these are all wrappers around a vendor component, then maybe you might try to use a factory method to create them like VendorBuilder.GetVendorThing("A") which could return an object of type IVendorWrapper.</p>\n'}, {'answer_id': 14672, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 2, 'selected': False, 'text': "<p>You can't overload methods by varying just the return type.</p>\n\n<p>You can use different names: </p>\n\n<pre><code>static AObject GetAObject(string id);\nstatic BObject GetBObject(string id);\n</code></pre>\n\n<p>Or you can create a class with casting operators:</p>\n\n<pre><code>class AOrBObject\n{ \n   string id;\n   AOrBObject(string id) {this.id = id;}\n\n   static public AOrBObject RetrieveByID(string id)\n   {\n        return new AOrBObject(id);\n   }\n\n   public static AObject explicit operator(AOrBObject ab) \n    { \n        return AObjectQuery(ab.id);\n    }\n\n   public static BObject explicit operator(AOrBObject ab)\n    { \n        return BObjectQuery(ab.id);\n    } \n}\n</code></pre>\n\n<p>Then you can call it like so:</p>\n\n<pre><code> var a = (AObject) AOrBObject.RetrieveByID(5);\n var b = (BObject) AOrBObject.RetrieveByID(5); \n</code></pre>\n"}, {'answer_id': 14683, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 3, 'selected': True, 'text': '<p>Looking at <a href="https://stackoverflow.com/questions/14614/static-methods-in-an-interfaceabstract-class#14655">your response</a> I am thinking along the following lines:</p>\n\n<ul>\n<li>You could just have a static method that takes a type parameter and performs the expected logic based on the type.</li>\n<li>You could create a virtual method in your abstract base, where you specify the SQL in the concrete class. So that contains all the common code that is required by both (e.g. exectuting the command and returning the object) while encapsulating the "specialist" bits (e.g. the SQL) in the sub classes.</li>\n</ul>\n\n<p>I prefer the second option, although its of course down to you. If you need me to go into further detail, please let me know and I will be happy to edit/update :)</p>\n'}, {'answer_id': 14692, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 2, 'selected': False, 'text': '<p>For a generic solution to your example, you can do this:</p>\n\n<pre><code>public static T RetrieveByID&lt;T&gt;(string ID)\n{\n     var fieldNames = getFieldNamesBasedOnType(typeof(T));\n     QueryResult qr = webservice.query("SELECT "+fieldNames + " FROM "\n                                     + tyepof(T).Name\n                                     +" WHERE Id = \'" + ID + "\'");\n     return (T) qr.records[0];\n}\n</code></pre>\n'}, {'answer_id': 14693, 'author': 'rptony', 'author_id': 1781, 'author_profile': 'https://Stackoverflow.com/users/1781', 'pm_score': 3, 'selected': False, 'text': '<p>If I were you I would try to avoid any statics. IMHO I always ended up with some sort of synchronization issues down the road with statics. That being said you are presenting a classic example of generic programming using templates. I will adopt the template based solution of Rob Copper presented in one of the posts above. </p>\n'}, {'answer_id': 14720, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 0, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/users/1659/marxidad">marxidad</a> Just a quick point to note, Justin has already said that the SQL varies a lot dependant on the type, so I have worked on the basis that it could be something <em>completely</em> different dependant on the type, hence delegating it to the subclasses in question. Whereas your solution couples the SQL <strong>VERY</strong> tightly to the Type (i.e.  it <em>is</em> the SQL).</p>\n\n<p><a href="https://stackoverflow.com/users/1781/rptony">rptony</a> Good point on the possible sync issues with statics, one I failed to mention, so thank you :) Also, its Rob <strong>Cooper</strong> (not Copper) BTW ;) :D ( <em>EDIT:</em> Just thought I would mention that in case it <em>wasn\'t</em> a typo, I expect it is, so no problem!)</p>\n'}]
['https://Stackoverflow.com/questions/14614', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1108/']