Hanalei, Hawaii 9/2/2010
438 Posts and Counting

SonicCast #3 Is Up: All About MVC

Tuesday, July 24, 2007 -

The 3rd installment in the SonicCast series is up, and I talk about the whole MVC "thing" and how we're trying to emulate that with our new SubSonic MVC templates. I also talk about how Rails implements it (a bit) and show you how you can customize our MVC templates to squeeze some tighter architecture out of your distributed app.

Your comments are very welcome!

Related


Gravatar
Damir - Tuesday, July 24, 2007 - Hi, it looks very neat... I've played a bit with Rails and I think it would be very nice if we could end up with using same programming model with different languages.
Good cast as always. Just, please fix the code so it works in Firefox too...
Have fun, Damir
PS. I couldn't post a comment in Firefox either... DS
Gravatar
kevin - Tuesday, July 24, 2007 - yea rob, not working for me on firefox 2.0.0.5 either :(
Gravatar
steve b - Tuesday, July 24, 2007 - +1 for the screencast problems with FF 2.0.0.5

First two are working fine but not the 3rd.
Gravatar
Member Blogs - Tuesday, July 24, 2007 - .NET Example: Inheritance vs Object Composition IP Address Validation .net 2.0 Snippet Waiting for a
Gravatar
josh - Tuesday, July 24, 2007 - not that it's in your demographics, but I don't see anything on ff2.0.0.5 on Fedora 7 linux.

I'll either watch it on my lappy or finally get that vmware windows image going on this box.
Gravatar
Rob Conery - Tuesday, July 24, 2007 - This is weird - it's the second time this has happened! FCK Editor hacks up the EMBED tag for some reason and ... well I was running out the door and should have checked it! My bad...

It's fixed - go have a look now :)
Gravatar
kevin - Tuesday, July 24, 2007 - F*ckeditor? I'm not a fan of any of those wysiwyg editors really. but I guess I'm old skool and enjoy the basic [markup /] myself. Google forums has a really simple and useful editor - wish they made it open source.
Gravatar
Chris Frazier - Tuesday, July 24, 2007 - Good intro, but stop apologizing that it's "not a true controller"...one or two of those is enough. It's a limitation, yeah, and kudos for pointing it out in the first place but eh you know what I mean ;)

Keep up the good work, Rob. XD
Gravatar
Rob Conery - Tuesday, July 24, 2007 - Not sure that I'm "aplogizing" for anything Chris - just pointing out the difference between a true Controller, and what we've come up with - which is a hybrid.
Gravatar
josh - Tuesday, July 24, 2007 - just checked and the demo now runs nicely on my fedora 7 box.

..now to fix vmware.
Gravatar
Rob Conery - Tuesday, July 24, 2007 - You know as I watch the webcast again I think I'm not being terribly clear. I'm going to redo it tomorrow...
Gravatar
Remmus - Tuesday, July 24, 2007 - Minor point but you spelt controller wrong too.
Gravatar
Damien Guard - Wednesday, July 25, 2007 - Towards the end it talks about how you can provide different behaviour by overriding the controllers static methods.

The problem is that these are partial classes and so putting "new" in the non-generated method signature just won't achieve that - it won't even compile but well emit the message "Type x already defines a member called y with the same parameter types"

The only solution is to generate the Controller as a non-partial and subclass it in the hand-written one.

Because they will both need different names or namespaces to co-exist if you start using the generated one direct but then choose to implement something by hand you will have to go and change all your references.

[)amien
Gravatar
DotNetKicks.com - Wednesday, July 25, 2007 - You've been kicked (a good thing) - Trackback from DotNetKicks.com
Gravatar
Rob Conery - Wednesday, July 25, 2007 - @Damien: I wouldn't throw code up in a webcast that 1) doesn't compile and 2) doesn't work. You can override/new a method in a partial just as if you inherited it - give the code sample a try.

Just to verify I ran the code in the sample at it brought back a NULL product.

I need to redo the webcast anyway, so I'll be sure to run the code so you can see that it works.
Gravatar
Ibleif - Wednesday, July 25, 2007 - I havent testet if the code worked or not, but perhaps it worked because you misspelled Controller as Conroller, and then the partial class didn't "merge up" with the actually class.

Im not with a .NET compiler right now, so cannot tell you if it is possible or not.
Gravatar
Damien Guard - Wednesday, July 25, 2007 - It's entirely possible I'm missing something but I did try a quick test before my original comment, like this:

class Program {
static void Main(string[] args) {
System.Console.WriteLine(Test.Testing());
System.Console.ReadKey();
}
}

public partial class Test {
public static new string Testing() {
return "Normal test class";
}
}

public partial class Test {
public static string Testing() {
return "Generated test class";
}
}

Which gives a compilation error that Testing() is defined twice.

The code in your sample has a underlined squiggle on the method name - it's possible it's the same error I get with this test.

I would love partials to have this behaviour but the 'new' keyword is currently for overriding inherited methods and partials are not a form of inheritance just a compile-time facility of splitting the code over multiple files/definitions.

[)amien
Gravatar
Rob Conery - Wednesday, July 25, 2007 - LOL yah you know what - I'm an idiot! I wrote up the sample before the webcast and all worked fine - Damien I owe you an apology for that one!

Doh!
Gravatar
marco (pomarc) - Thursday, July 26, 2007 - Hi rob.
I've watched the wcast (on IE7, on FF does not work for me either), and I do not get it. My fault, obviously.
The MVC pattern does not look so new to me, it just looks like a new way to call something very similar to the old three-tiered development, in the end.
when we made asp classic websites, we put all the logic in the logic tier, which in that old dark times was a clumsy activex DLL, that we called business logic.
on asp.net we put all the logic on separate classes, that we call, again, business logic classes.
accessing the data store has been made in a DAL on asp, and in a DAL (maybe made first with datasets and data adapters, then table adapters, then data entities like nHib or subsonic) on .NET.
so, all in all, what's new? It looks like this MVC thing is using an old clean and wise approach to approach to developing and giving it a new cool and shiny name. :)
where do I go wrong?

Gravatar
Rob Conery - Thursday, July 26, 2007 - @marco: Nothing new here in terms of what MVC is. The point of confusion is its implementation. In ASP (classic or otherwise), Business logic (aka controllers) are akin to a bucket that you put things in, then set alongside the other "buckets" in your app (DAL, rule sets, etc). The "view" is the application itself (web app, win forms, whatever) and when you code it up, you need to remember to use the business rules, and also use some discipline to put things in the proper buckets.

ASP and ASP.nET are not built to handle this stuff implicitly, like the Rails stack is.

A Controller is... well "in control" of your application. All events and requests are routed there to be handled - not back to the page. It plays much more of a role than just a place to put your business logic.

What we've done with these templates is go about 80% there - meaning you can't use the DAL directly, you need to use the controller classes to manipulate the object set. It's not complete, but architecturally it's preferable if you have a large system with lots of developers.
Gravatar
Rob Bazinet - Saturday, July 28, 2007 - Rob,

I am curious, where is the sample project for the screen cast? Could I get a copy of the project?

Thanks,
Rob Bazinet
Gravatar
Ibleif - Sunday, July 29, 2007 - @Rob Bazinet: I think we are waiting for Conery to update the webcast.
Gravatar
cmv - Monday, July 30, 2007 - Rob,

At my work (referenced above) we struggled with asp.net and webforms much the same way you mention in the webcast. we are a huge fan of the MVC2 pattern and thought long and hard on how we could get asp.net to play nice. the front controller pattern was making it too easy to put business logic in our views. Outside of work i live in RoR, TextMate and OSX. and i desperately wanted an MVC2 framework within asp.net. so, we created one. Controllers can only be instantiated via a Request, Actions are mapped to pages and button clicks call Actions without having to wire up to the controller or an event (no more btn.Click += new blahblahblah(click_me) ) the framework handles this for you. we enforce convention, exactly the same as rails : http://mysite.com/controller/action. and we provide a nice barrier between the view and controller, you can't instantiate a controller from a view, it can only be handled via the request. we are hoping to get this framework open sourced, i'd be more than happy to share all the details and let you look at the source code if you are interested (email me if so). btw, love subsonic and this blog!
Gravatar
Andrew Stopford's Weblog - Monday, July 30, 2007 - Rob has been busy, creating two SonicCasts :) MVC and using SubSonic MVC templates . Rails from a ASP
Gravatar
WPF Community Bloggers - Monday, July 30, 2007 - Rob has been busy, creating two SonicCasts :) MVC and using SubSonic MVC templates . Rails from a ASP.NET
Gravatar
Rob Bazinet - Tuesday, July 31, 2007 - @cvm,

I would like to see more about your implementation. Since you didn't leave your email address I can't contact you.

Please contact me at rbazinet at gmail dot com.

-Rob
Gravatar
Rob Bazinet - Tuesday, July 31, 2007 - I wonder how the implementation the Monorail guys are doing compares to this?

I have only used Monorail for exploring, so I am not an expert. Would it be reasonable to use their approach but replace their implementation using NHibernate with SubSonic?

-Rob
Gravatar
Damien Guard - Thursday, August 02, 2007 - Method hooks from generated code are available in .NET 3.5 via partial methods:

http://www.damieng.com/blog/archive/2007/08/02/partial-methods-in-.net-3.5-overview-and-evolution.aspx

[)amien
Gravatar
Haider - Monday, August 13, 2007 - Rob,

I have downloaded the templates from your previous post and after trying several times, I don't see any 'Get' methods on the controllers. There are insert, update, and mapping related methods (where available), but no 'Get' or 'Delete' method at all.

On the Sonic Cast I see you are calling 'Get(1)' on product controller.

I also opened up the ODSController template and don't see the implementation of the 'Get' methods there.

Do I have the wrong copy of these templates?

Thanks.
-Haider.
Gravatar
Rob Conery - Monday, August 13, 2007 - If you used the VB templates that might be the issue - these are C# only for now...
Gravatar
Haider - Monday, August 13, 2007 - Ok, that was quick!

Actually, it is quite interesting. I was using the C# templates. When I looked at the VB templates, they seem to have implementation of all Fetch/get/list as well as insert/update/delete/destroy but no mapping related methods.

The C# implementation only has insert/update and mapping related methods (if available), no delete/destroy/Fetch/list/get methods at all.

Just downloaded again to make sure I have the latest copy!

Thanks a lot, Rob