Infovark Underground

  • news
    • infoblog
    • underground
  • product
  • download
  • buy
  • support
  • about
    • ← One-Way Serialization
    • Write Big to Write Small →

    Switch By Type in C#

    09 Oct 2008 by Dean in .NET, Programming / No Comments

    The introduction of generics into C# 2.0 simplified many programming tasks. It especially helped in the creation of type-safe collections.

    During our reworking of the Infovark data access layer, we created several generic methods to return items from our database. This allowed us to eliminate many duplicated methods and eliminate a lot of type casting. For example, before we began using generics, we had methods with signatures like this:

    1.   public Entity GetEntity(long id, int revision) { … }
    2.   public Relationship GetRelationship(long id, int revision) { … }
    3.   public Resource GetResource(long id, int revision) { … }

    became this after our refactoring:

    1.   public T GetObject<T>(long id, int revision) where T : MetaInstance
    2.   {
    3.     Type type = typeof(T);
    4.  
    5.     if (type == typeof(Entity)) return _DatabaseHandler.GetEntity(id, revision) as T;
    6.     if (type == typeof(Relationship)) return _DatabaseHandler.GetRelationship(id, revision) as T;
    7.     if (type == typeof(Resource)) return _DatabaseHandler.GetResource(id, revision) as T;
    8.  
    9.     return null;
    10.   }

    Wait a minute. All we’re doing here is wrapping all our unique methods up in a generic method! Why bother? Good question. We abandoned that approach in favor of something more sensible later on.

    That’s actually not the point of this post. The point is that C# doesn’t have the ability to perform a switch on types. See all those if statements in our generic method? That’s how we worked around the lack of type switching. If anyone has a better approach, we’d like to hear it.

    Peter Hallam’s WebLog provides more information about why C# doesn’t have the ability to switch on a type. I’m not convinced by the reasons outlined there. As one commenter noted, there’s already a type-switching construct in C#: the catch statement.

    UPDATE: Switching by type is a common problem with MVC frameworks. There’s been quite a lot of discussion on Stack Overflow with regard to the best way to deal with this issue.

    • C# Switch Limitations
    • Is there a better alternative to switch on type?
    • MVC specific: Selecting the correct view for an object type

    Related posts

    1. Calling a Static Method from an Object Instance in C#
    2. Using WCF to return HTML
    3. Creating Dummy Targets for Configuration Objects
    • Tweet
    • Tags:
    • refactoring

    Leave a Comment

    Posting your comment...

    Subscribe to these comments via email

    • Categories

      • .NET (41)
      • AJAX (3)
      • Books (7)
      • HTML (9)
      • Infovark (8)
      • Programming (48)
      • REST (11)
      • SQL (3)
      • Testing (3)
      • Tools (13)
      • UI (3)
      • WCF (11)
      • Web Services (8)
      • WPF (4)
      • XML (4)
    • Archives

    • Get future articles


       

    • Blogroll

      • Ajaxian
      • Anne Van Kesteren
      • Brain.Save()
      • Coding Horror
      • Eric Sink
      • Joel Spolsky
      • John Resig
      • Mark Pilgrim
      • Raymond Chen
      • Scott Hansleman
      • Secret Geek
      • Steve Yegge
      • The Daily WTF
      • The Database Programmer
    • Meta

      • Log in
      • Entries RSS
      • Comments RSS
      • WordPress.org
  • Site map

    • News
    • Product
    • Download
    • Buy
    • Support
    • About
  • Recent Posts

    • Review: Brownfield Application Development in .NET
    • Using Modal Dialogs with a Splash Screen in WPF
    • Highlighting query terms in a WPF TextBlock
    • Getting XAML Hyperlink text to wrap
    • How to format the XAML Hyperlink NavigateUri
  • Twitter

    Copyright 2011 Infovark, Inc. All rights reserved.