To give you an example that you all should be familiar with, imagine that you are building a Facebook wall page in Asp.Net MVC. On the wall, there are different kinds of posts (ie status updates, photos, videos, links, whatever. They all display down the wall and they all render differently, depending on what kind of post they are. I am building something similar to this, and it seems to me that the most elegant way to do this is with polymorphism. I foreach through the Post types and call a rendering method that each subtype implements. I did something like this in the code-behind of Web Forms, but I cannot figure out how to do this in MVC without mixing concerns, short of having a giant list of if else blocks.
@foreach(Post post in Model.Posts)
{
if(post is A)
{
<div>Different Content</div>
}
else if(post is B)
{
<div>Different Content</div>
}
else if(post is C)
{
<div>Different Content</div>
}
}
instead of just
@foreach(Post post in Model.Posts)
{
post.render();
}
How do I get something more maintanable like the second part?