I have a struct which I want to add static methods to. Yes, you guessed right I'm talking about Datetime
.
It's a pretty typical requirement to add MyCustomParse. It happens to return Datetime?
, so it's not a constructor, but it's in that area, a static method that returns an instance. The content of this method has already been written and currently happy lives as a instance method of a class that uses it. I want to bring it out of that class so I can use it elsewhere.
Traditionally I would put it in a helper class (which is what I've done here), but apparently they're evil.
What I really want to do is tie Datetime.MyCustomParse(mystring)
. But the only way I can see is to make a new struct that composes Datetime
. I then add in my method, write the implicit conversions and then put in all the other Datetime
methods etc. This would be a pain to code (Being verbose also causes problems wrt xml docs and naive code coverage metrics, if that counts for anything).
I have considered extension methods, but I don't think the methods make sense on instances.
What is the strict OO position? Do I get a pass on writing methods that should belong to classes in separate helper classes when I want to extend structs in this fashion?
What is the pragmatic position (I'm not saying OO isn't pragmatic), is this code you would write, or have I missed a trick?
internal static class DateTimeHelper
{
public static DateTime? MyCustomParse(string utcDateTimeText, string[] formats)
{
DateTime utcDateTime;
if (DateTime.TryParseExact(utcDateTimeText, formats, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out utcDateTime))
{
return utcDateTime;
}
return null;
}
}
PS I know there are more nuanced views on helper classes, but I still feel I need to justify my use here.
DateTime
already has aParse
...