Erik Porter's Blog

Life and Development at Microsoft and Other Technology Discussions

News

    FrameworkUtilities

    Recently, I chatted through e-mail with a long time friend's (Joe Heller, who is now engaged [Congratulations!]) co-worker, Brian Wortman of RDS about some programming concepts and workarounds for a few things lacking in the framework.  We chatted, swapped opinions, etc.  Very smart and cool guy.  One of the things we talked about was how he had been working on a project that contained custom attributes to use as parameter method validators to ensure the data coming in fits the bill, saving developers having to write code themselves to check it.  I thought it was such a fantastic idea that I went ahead and made my own.  Another thing he had mentioned was the problem of Value Types not being able to be Empty (big problem across the board for sure), so I went ahead and wrote an Empty Class to have a standard way to “recognize” empty value type variables.

    These two things were related and one uses the other, but also could be used seperately, so I got to thinkin', “Why not create a utilities project with bunches of 'stuff' that could be used throughout all my projects?”.  Kind of like the new My Classes in VB.NET Whidbey.  So I did...I started a new GDN Workspace called Framework Utilities and also added a couple other (hopefully) useful classes for people to use.

    I know I'm usually a VB.NET guy, but this time I decided to do the project in C#.  Periodically I do that to keep my skills fresh.  Especially for when those “my language is better” arguments come up, I can actually know what I'm talking about because I use both languages.

    Current Features

    • Method Parameter Validation Attributes
      • Attribute for all Value Types and includes range validation and required validation
      • Required Attribute to make sure a value is passed in another than null for Reference Types
      • If Value Type Attributes are attached to a string, they will make sure the incoming value is Valid
    • TryParse Class
      • Contains Methods to see if a string value is valid for the particular value type
      • Returns the Type requested from a string
    • Empty Class
      • Standard Methods for Value Types for checking for empty and getting empty values
      • Has Database Methods to check for Empty as well
    • File Class
      • Contains Methods for Retrieving and Writing to files easily (string and byte array)

    Method Parameter Validation Attribute Usage

    Private Sub CallMethod(<StringParameterValidation(2, 5, True)> ByVal Param1 As String, <RequiredParameterValidation()> ByVal Param2 As XmlDocument, <DateTimeParameterValidation(True)> ByVal Param3 As String)
        
    Try
             
    ParameterValidation.Validate(Param1, Param2, Param3)

             
    'TODO: Code here

             
    MessageBox.Show("Method Called")
        
    Catch Ex As Exception
             
    MessageBox.Show(Ex.Message)
        
    End
    Try
    End Sub

    CallMethod("a", Nothing, "3/26/2004")

    I did this in the evenings over the last couple days, so it's still rough and the performance needs a lot of work on the Validation Classes.  If you have ideas for new features you'd like to see, please post them in the messageboard on the Workspace.  Also, I'd love to get some more people in to work on this, tweak what's already there (please oh please any Regex experts...it needs help), test it (I haven't done much testing yet), etc.

    Framework Utilities GDN Workspace
    Intial Source Code Release v 0.1.0.0

    UPDATE: I also wanted to thank Jason Bock for helping me get my head around some stuff with Attributes!

    Comments

    Johnny Hall said:

    You might want to take a look at NullableTypes...

    http://nullabletypes.sourceforge.net

    Which do the same thing as your EmptyClass. Probably significantly larger and more complex though.

    I'm evaluating them at the moment and I'm pretty impressed. I'll be interested to take a look at your approach too.
    # March 26, 2004 7:55 PM

    HumanCompiler said:

    Thanks for the link, I hadn't seen that...it's hard to compare, because mine's quite a bit different. With mine, you're still using regular types, it's just that it checks them to see if they're "empty", using meaning the type.MinValue or something that nobody ever uses. They're way does seem pretty cool though, just different.
    # March 26, 2004 7:57 PM

    Johnny Hall said:

    You could take a look at a recent post of Eric Gunnerson where he created an EmptyDateTime class which could be used in place of a DateTime but understood the concept of "empty". You could extend that concept to all the types, which would eliminate the need for the static class.

    http://blogs.msdn.com/ericgu/archive/2004/03/23/95011.aspx

    Good work.
    # March 26, 2004 8:04 PM

    HumanCompiler said:

    Very true...I see that's what the NullableTypes project does as well. I guess I'd rather make both available though, because when interacting with a database, it's not going to return your new class anyway, so you'd have to write some static class to turn it into your new type anyway, as well as when going back into the DB, that's why i took the simpler, less cool approach to that, but having Emptyxxxx would be great too, maybe I'll add that soon...you could always join and help out too ;)
    # March 26, 2004 8:09 PM

    Johnny Hall said:

    That's a good point. NullableTypes has a helper class(es) which manage the translation to and from the db, and for providing formatting (of the empty value).

    Thanks for the invite. I'm a bit snowed under at the moment but my current client needs something similar so I may take a look soon.

    Take care.
    # March 26, 2004 8:14 PM

    Richard Tallent said:

    EmptyClass: cool concept! Like every developer worth their salt, I also have a collection of utility classes that I've amassed over the years. While source-control seems like a great way to share, I can't stand GDN (SourceGear man myself), but good luck on the project!

    The next two features that stick out in my mind would be:

    1. A custom string parser method that converts the string equivalents of "empty" to the internal "empty" value (it could use your TryParse class for the non-special values).

    2. Custom formatters (decorator-like pattern) that takes in a value type and optional formatting info, and spits out a formatted string, but handles "empty" values specially by outputting "" or some other "magic" value string. Example:

    Public Function SmartToString(ByVal datetime As DateTime, Optional ByVal format As String = "", Optional ByVal EmptyValue As String = "") As String
    # March 26, 2004 9:09 PM

    TrackBack said:

    Take Outs for 26 March 2004.
    # March 27, 2004 5:09 AM

    HumanCompiler said:

    Richard,

    I loved # 2 and added it...you can now call GetDateTimeString(MyDateTime, "m/d/yyyy", "n/a") or not pass in the optional empty format (it's an overload)...thanks for that great idea! :)

    As for # 1, I'm really not sure what you're talking about! :| Me.DumbToday = True
    # March 31, 2004 1:05 AM

    John said:

    I know this code is old, but is it possible to post it on your site as the link to GDN no longer works?

    Thanks

    # September 4, 2007 2:28 PM

    HumanCompiler said:

    Sorry, John, I no longer have the code.  If you're using .NET 2.0, pretty much all the features I had are in there now.  Good luck.  :)

    # September 4, 2007 2:41 PM