Switching on type without the Visitor pattern

Posts   
 
    
Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 16-Nov-2009 02:59:43   

Hi,

I often find that I want to have a different rendering of an entity to a web page for each type in an inheritance hierarchy. So for example, one type might require a Redirect where as another results in a user control being loaded. The question is how does one do this without the baggage of the Visitor pattern?

You could easily just put some rendering code in the actual entity except then you've just bound your business layer to the UI. So basically I'm always just left with conditional code such as 'if (obj is MyEntity)' etc which isn't very OO.

It strikes me as strange that C# does not support multi-dispatch because multi-dispatch seems so incredibly useful if not essential for a decent design. I'm looking forward to C# 4's dynamic keyword.

Ian.