Extension Methods in NVelocity View Engine Helper

Last post 12-31-2007 12:59 AM by abombss. 9 replies.

Sort Posts:

  • Extension Methods in NVelocity View Engine Helper

    12-22-2007, 9:20 PM
    • Loading...
    • msanford00
    • Joined on 12-23-2007, 2:14 AM
    • Posts 2

    I'm using a NVelocity View Engine, but the extension methods provided by the MVCToolkit don't work in the view template, presumably because there is no compiler support there.

    Is there any way to force the extension methods to be available on the HtmlHelper?

    I've sort of hacked around the problem by writing a bunch of wrapper methods in a custom helper, but this is obviously less than elegant...

     

    Thanks!

  • Re: Extension Methods in NVelocity View Engine Helper

    12-22-2007, 11:20 PM
    • Loading...
    • abombss
    • Joined on 06-27-2006, 12:13 PM
    • Chicago, IL
    • Posts 164

     Can you post some of your error messages?

    I also noticed that many of the extension methods require your controller subclassing from Controller instead of IController.  So much for extensibility.

    Adam Tybor -- abombss.com
  • Re: Extension Methods in NVelocity View Engine Helper

    12-22-2007, 11:38 PM
    • Loading...
    • msanford00
    • Joined on 12-23-2007, 2:14 AM
    • Posts 2

    There's no error at all.  NVelocity just treats it like it couldn't find the method and prints the variable (unless you use silent notation):

    $Html.TextBox("fieldName")

    Other methods on the HtmlHelper work fine, so I know it's present...  Only the extension methods fail to work.

  • Re: Extension Methods in NVelocity View Engine Helper

    12-23-2007, 12:57 AM
    • Loading...
    • abombss
    • Joined on 06-27-2006, 12:13 PM
    • Chicago, IL
    • Posts 164

    Sounds like a patch is needed for nVelocity to incorporate namespaces into its context while its rendering the view.  Not sure how to do that?

    Adam Tybor -- abombss.com
  • Re: Extension Methods in NVelocity View Engine Helper

    12-23-2007, 11:17 AM

    I haven't used NVelocity extensively, just very simple email templates and other trivial stuff, but I remember someone saying that the NVelocity engine only resolves the objects passed it its context and the properties (maybe methods) of those objects. If that is true then it may not be possible to leverage extension methods.

    Do you know of a way to add assembly references and import namespaces into nvelocity templates? Is there a compilation step at all for the templates? 

     

    __________________________________________
    Sergio Pereira
    http://devlicio.us/blogs/sergio_pereira/
  • Re: Extension Methods in NVelocity View Engine Helper

    12-23-2007, 3:09 PM
    • Loading...
    • abombss
    • Joined on 06-27-2006, 12:13 PM
    • Chicago, IL
    • Posts 164

     No, the templates aren't compiled at all, just interpreted.  Is there no way to reflect an instance and pull in extension methods?

    Adam Tybor -- abombss.com
  • Re: Extension Methods in NVelocity View Engine Helper

    12-27-2007, 9:56 AM
    • Loading...
    • wcpierce
    • Joined on 09-02-2005, 9:45 PM
    • Colorado, US
    • Posts 16

    Adam is correct.  A patch is required for the nVelocity view engine to handle helpers.  My current thoughts are a wrapper for the existing helpers that implement IDuck.  The wrapper will need to be made aware of extension classes and then reflect over those classes to collect all of the extension methods to be able to call the functions by name.  Somthing like DuckHelper<HtmlHelper> DuckHelper<UrlHelper> etc.  It's on the list, just hasn't been done yet.  As a rant, I'm not very thrilled with extension methods, especially when the developer has control over the code for the original object i.e. HtmlHelper in the framework.  I hope all of the extension methods from the MVCToolkit end up as virtual methods on HtmlHelper in the next CTP.  Extension methods are just a compiler trick and thus are very difficult to work with at runtime via reflection.

  • Re: Extension Methods in NVelocity View Engine Helper

    12-27-2007, 4:31 PM
    • Loading...
    • abombss
    • Joined on 06-27-2006, 12:13 PM
    • Chicago, IL
    • Posts 164

     +1

    I agree, extension methods are bad when you own the code.  Unless maybe you are separating concerns and trying to get some mixin type functionality.  i.e. Adding presentation functions to a domain object in the presentation layer.  Still not sure how I feel about that yet.

    Adam Tybor -- abombss.com
  • Re: Extension Methods in NVelocity View Engine Helper

    12-30-2007, 11:36 PM
    • Loading...
    • wcpierce
    • Joined on 09-02-2005, 9:45 PM
    • Colorado, US
    • Posts 16

    Hey msanford00,

    The latest trunk of MVC Contrib (rev 90) now includes support for extension methods in NVelocity.  Some additional work will be needed to support overloads with 'object htmlAttributes' but this should get you going.

    -Bill
     

  • Re: Extension Methods in NVelocity View Engine Helper

    12-31-2007, 12:59 AM
    • Loading...
    • abombss
    • Joined on 06-27-2006, 12:13 PM
    • Chicago, IL
    • Posts 164

     Nice work Bill :)

    Adam Tybor -- abombss.com
Page 1 of 1 (10 items)
Microsoft Communities
Page view counter