mvc ideas for version 4 – the ‘Model’

Phil Haack from the mvc team mentioned features for mvc 4.0 on twitter today, and I followed up saying we need some command line ‘scaffolding’ capabilities, ie. Rails 3 style via a ‘nuget’ like interface in Visual Studio.

Scott Hanselman tweeted back about his blog post from PDC: in which he touches on a mvc scaffolding concept.

That said, I wanted to add to that and express some more details after working a bit with Rails 3.

First, a problem I see, which is ironic, since Microsoft has always been fairly ‘opinionated’ on how things should be built, that in this case, mvc is – in my opinion – not very opinionated when it comes to critical items like the model and dependency injection, etc…  They have provided hooks, but the beauty, imo, of Rails is that we want more opinionated and a straightforward approach, with the ability to plug your own.

What do I mean?  In Rails 3, they have the ‘Gemfile’ concept – where you can customize the defaults of what you get with Rails 3 – everything from what type of ORM, to what javascript library (ie jQuery instead of prototype), etc…

That said, I’m actually suggesting, to go along with the scaffolding, that we get some ‘opinionated’ options here in mvc 4 – and certainly if the community wants to come in an define their own they can, but we want to concentrate on building the apps, not be tied all up into the infrastructure – ie. how to set the controller factory, etc..

So, in the example from Scott’s blog – which is a start – there is a ‘DbContext’ being created and a model being handcrafted.  This is exactly the weak spot in mvc – we have 4 (or more) view engines, a brand new one – we have tons of controller- results, metadata attributes, json inputs, etc.. but the model part… is like the red headed stepchild – where is it!!!!???  And that irony to me is we are dealing with a company that invests a ton in database, modelling, ORM’s like linq to sql and EF, P&P data guidance, etc… so where the … is my opinionated model???

So, I think mvc needs to take that step.

Scaffold-Controller -m Post title:string, body:string

Generate my DbContext, read from my connection string in the web.config – generate my model.  (heck, at this point I’ll even take a partial class Post : Model class and you can stuff all your validation, mapping) attributes somewhere.

Get the idea ?  Throw your Entity Framework in, make it the default – if NHibernate or L2S want their own, provide some sort of ‘Gemfile’ hooks and others can create their own ORM plug-ins – but start with Entity Framework and get the model solid.

Next, the DI pieces.  This should be more automatic.  Gemfile your default, make it Unity, I don’t really care, and guys like Castle, NInject, Spring.NET, they can create the plugins so when you create your app and run your bundle script against the Gemfile, we can at least get some opinionated default DI happening.

So, just a few ideas.  I don’t want to say I was ‘disappointed’ with Razor, as nothing was wrong with it – at the same time, I think the core pieces of what mvc is to get these critical pieces in vs. just adding yet another new view engine to learn.

I’ve worked on three considerable mvc apps after a career in webforms, and mvc knocks the socks off webforms.  But the pain points were setting up the models, the repository patterns, the DI – and using ‘add new view’ dialogues were painful.  So I would want this scaffolding to create the model, create a context if one doesn’t exist.  My model’s should allow me to then add a query to post:

public partial class Post

[required] //adding validation to the property
public string Title{get;set;}

public List<Post> GetAllPostsTitle(string title){
return Post.Where(c.Title == title).ToList();

I don’t really honestly want to be wondering the ‘how’ here – sure, you can give me hooks to it, but let’s get the DbContext out of the picture – this Post class should have all the normal CRUD in the generated partial Post class, and allow the developer to extend it and add the specific queries and metadata.

This is one of the parts of Rails that just shines really bright – why not pull together these infrastructure pieces and let us the developers get back to writing that domain logic we need without being so tied to things like ‘DbContexts’.

One last comment – don’t let the actual full scaffolding be the focus.  Most devs see that as ‘nice to have’  – and might use it on some admin type pages, but typically that is good ‘demo’ code.  The command line needs to be focusing primarily on the model and controller.

In summary, I see a good start from PDC talk by Scott.  It looked very rough.  It needs to be well thought out and the mvc team should, imo – start to think about adding some opinionated builder capabilities to help the mvc devs be more productive, less ‘GUI’ based, and build on the VS command line capability we see in nuget.  Concentrate on the ‘M’ part, as the ‘V’ is saturated right now.

There you have it, an opinionated blog post on wanting a more opinionated mvc framework  🙂


One thought on “ mvc ideas for version 4 – the ‘Model’

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s