Rethinking Models in MVC
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.
Just learn F#!
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.