Rethinking Models in MVC

by Jonathan Allen on Jun 09, 2011 |

Jon Galloway is researching dynamic ASP.NET MVC 3 models using Mono’s Compiler as a Service. Meanwhile Karsten Januszewski is looking into deserialized JSON in lieu of statically typed models.

Dynamic Models using Mono’s Compiler as a Service

Since we are still two versions away from having compiler-as-a-service support from Microsoft, Jon Galloway’s research started with the recently updated Mono Compiler Service. Mono’s compiler is currently under active development as they lay the foundation for adopting C# 5’s asynchronous language constructs.

The fundamental class here is Mono.CSharp.Evaluator. Calling the Run method on this class will compile whatever string has been passed in. The Evaluate method, which also accepts a string, will process and return whatever expression is given to it. In this sense it is very much like the experience you would get from the Dynamic Language Runtime or the venerable VBScript hosting APIs.

JsonObjects as Models

Karsten Januszewski’s research has taken a decidedly different tact. He starts with JSON-formatted strings that were exposed from the service tier using WCF JSON classes. These are parsed into a JsonObject which acts as the model. The downside here is that there is no place to hang business logic; the model is pure data.

While working on this Karsten ran into an unfortunately common problem for MVC users and an elegant solution. Simply put, the anonymous types that LINQ normally generates are incompatible with the RuntimeBinder used by MVC. This can be generalized to “anonymous types are incompatible with dynamic types”. Fortunately a solution is provided. Simple create new JsonObject’s instead of anonymous types in the LINQ expression.

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Just learn F#! by john zabroski

Wow. If LINQ has had a negative effect on programmers, then this article demonstrates it.

Trying to shove everything into LINQ does not solve real application design problems, especially problems where the types cannot be compiled prior to deployment and must be reified dynamically in response to user input (a WCF request is a good example of how there can be infinitely many possible types).

As for Karsten's, actually his root problem is not that there is no place to hang business logic. Rather, it is that he is not actually doing anything "dynamic". The number of types he wants to model is finite. He just wants to build a way to define objects in terms of web services. Visual Studio has had this feature forever. It is called WSDL.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

1 Discuss
General Feedback
Marketing and all content copyright © 2006-2016 C4Media Inc. hosted at Contegix, the best ISP we've ever worked with.
Privacy policy

We notice you're using an ad blocker

We understand why you use ad blockers. However to keep InfoQ free we need your support. InfoQ will not provide your data to third parties without individual opt-in consent. We only work with advertisers relevant to our readers. Please consider whitelisting us.