Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Typing the RenderView method in ASP.Net MVC

Since the first minutes I started playing around with the MVC framework something has bothered me about the use of magic strings in regards to the RendorView methods. 

With the current implementation of RendorView there is NO compile time type checking to ensure that view even exists (evil magic strings).  Also, there is no way to find all usages of a view without doing a 'global find' on the project.

If you can strongly type the view to be used you will benefit from compile time type checking, you can use ReSharpers 'Find Usages' and it is just less evil.

Here are 2 ways to do this:

1 - Generics

public virtual void RenderView<T>( object viewData )
{
    RenderView(typeof(T).Name, string.Empty, viewData);
}

2 - Type provided

public virtual void RenderView(Type viewType, object viewData)
{
    RenderView(viewType.Name, string.Empty, viewData);
}

Here are a few examples of code calling the new RendorView methods

1 - Generics

RenderView<SportsListing>(sports);

2 - Type provided

RenderView(typeof(SportsEdit), new SportsEditViewModel(sports));

Now if I change the name of my views, I will get a compile time error letting me know ALL the places the view was being used.  This is a GOOD thing.

What is everyone else's thoughts on this???

Till next time,


Posted 03-10-2008 10:22 AM by Derik Whittaker
Filed under:

[Advertisement]

Comments

Alan Stevens wrote re: Typing the RenderView method in ASP.Net MVC
on 03-10-2008 12:10 PM

Derik,

I prefer the generics method for all the reasons you state. Did you contact Phil about this?

++Alan

Derik Whittaker wrote re: Typing the RenderView method in ASP.Net MVC
on 03-10-2008 12:14 PM

@Alan,

No, but was planning to.

Matt Hinze wrote re: Typing the RenderView method in ASP.Net MVC
on 03-10-2008 1:36 PM

What if you have a base ViewPage like CodeCampServer ...

code.google.com/.../ViewBase.cs

Derik Whittaker wrote re: Typing the RenderView method in ASP.Net MVC
on 03-10-2008 1:43 PM

@Matt,

Using the ViewBase as CodeCamp does assumes i want to use the SmartBag as my Viewdata.  As of now, i am leaning more towards using typed objects for each view.  a so called ViewModel if you will.

Matt Hinze wrote re: Typing the RenderView method in ASP.Net MVC
on 03-10-2008 1:52 PM

Of course I like it and this is better than magic strings.  I am still struggling with the problems that SmartBag attempts to fix, specifically global data for ViewUserControls, but I haven't delved into the latest bits so I may be lagging a bit.

Haacked wrote re: Typing the RenderView method in ASP.Net MVC
on 03-11-2008 1:31 AM

Interesting idea! One possible problem is that the view name doesn't have to match the type name. So I'm not sure that would always work.

Also, alternative view engines won't have a "type" for the view template. I wouldn't want to bake something in that doesn't work for the generic case. After all, we want to "play nice". ;)

Haacked wrote re: Typing the RenderView method in ASP.Net MVC
on 03-11-2008 1:32 AM

However, I will still bring it up with my team to brainstorm a bit. Thanks!

Derik Whittaker wrote re: Typing the RenderView method in ASP.Net MVC
on 03-11-2008 6:08 AM

@Phil

I understand your point about the other view engines.  The "typed" way could simply be an overload?

Thanks for resonding.

Ben Scheirman wrote re: Typing the RenderView method in ASP.Net MVC
on 03-14-2008 1:52 PM

This would directly prevent me from rendering multiple views depending on the request data (unless of course I baked that functionality into your typed method).  This is something I'll do often.

Derik Whittaker wrote re: Typing the RenderView method in ASP.Net MVC
on 03-14-2008 1:58 PM

@Ben,

So just so i understand, you have need to 'swap' the name of the view to render based on some logic in your controller?

If this is correct, then this would NOT work for you.  But in cases where this is not the case, it WOULD work, correct?

Grant Elliott wrote re: Typing the RenderView method in ASP.Net MVC
on 05-01-2008 5:46 AM

An idea...

  public class TypedViewController : Controller

  {

     public void RenderView<TView>()

     {

        string viewName = typeof( TView ).UnderlyingSystemType.Name;

        RenderView( viewName );

     }

     public void RenderView<TView>( object viewData )

     {

        string viewName = typeof( TView ).UnderlyingSystemType.Name;

        RenderView( viewName, viewData );

     }

  }

Then invoke it from your controller:

  public class YourController : TypedViewController

  {

     public void About()

     {

        RenderView<About>();

     }

     public void Index()

     {

        RenderView<Index>( dbContext.GetData() );

     }

  }

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)