Hanalei, Hawaii Tuesday, February 09, 2010

SubSonic MVC Scaffold Addin

Over the last month or so I've been working on an Add-in for Visual Studio 2008 which will generate scaffolded Views and a Controller using Linq To Sql. I took my time with it as I wanted to leverage all of the goodness that I've learned from the MVC Storefront, and as of today I think it's ready to see the light of day.

Over the last month or so I've been working on an Add-in for Visual Studio 2008 which will generate scaffolded Views and a Controller using Linq To Sql. I took my time with it as I wanted to leverage all of the goodness that I've learned from the MVC Storefront, and as of today I think it's ready to see the light of day.

This is a Prototype
Visual Studio may crash when you use this (hopefully not - I think I have it fairly stable). I've really tried to focus on "robustness" here but I'm not an "Addin Guy" per se. These bits are prototypes but "they work on my machine" so hopefully you will find this useful.

The goal of releasing this right now is to get your feedback. I'd like to see if I can answer some questions right up front, however:

  1. I didn't use SubSonic because IQueryable isn't ready to go. In addition, a LOT of people use Linq To Sql and the goal here is to help as many people as I can. But I WILL be plugging in an option to use SubSonic.
  2. You can change the code templates with your own bits - they are in the install folder in the "Templates" directory.
  3. I want to generate tests as well, but need more time to figure it out. I know this is important - and any help anyone wants to provide this way is great.
  4. This is Preview 5 only and I will update it with the next push (please be ready to uninstall it.
  5. This is way pre-release, so if you find a bug email me about it (robcon).

You can download the Addin Here (MSI Installer). I will be updating this a lot - so you may want to check back from time to time.

UPDATE: Scott Hanselman called to let me know that my installer is assuming that Addins go in [PersonalFolder]\Visual Studio 2008\Addins and for him, that's not the case. So I've uploaded a "binaries only" zip that you can get here - just unzip and drop into your Addins folder, wherever you put it.

UPDATE 2: My MSI Installer, for some reason, was not including a required DLL. This was fixed as of 10/7/2008 2:40 PDT and reloaded.

Many thanks to anyone who wants to test-drive this for me. And if you'd like to know what it does and what it is, here's (as usual) a video walkthrough (double-click for full screen, or download the video here)

Points to anyone who knows the soundtrack :).


Trevor - October 10, 2008 - I've been using a prefix on my tables like "rtk_". It doesn't seem to generate the correct code in this instance and assume for example that my primary key is also prefixed with "rtk_". Also the controllers get the ugly rtk_ prefix as well. Any ideas on how to handle this situation? Perhaps an "ignore prefix" and a place to enter what the prefix is.
Tim Hardy - October 14, 2008 - What is the difference between List and Index in your views? The "Golden 7" from Rails does not include List. Just curious because we, like many others, are trying to settle on our MVC action names. I'm good with New, Create, Edit, Update, Show, Delete. Now we're just trying to pick between List and Index. While RoR tends to use Index, I think List is much more descriptive. I was wondering what your take on these two action names was.
robconery - October 14, 2008 - Hey Tim - good question :). It's just me using an old Rails standard of "List" - I like the idea of List and Index being separate as they do different things (mostly - not always). I like both!
robconery - October 10, 2008 - Hi Trevor- the addin will generate whatever you set in your designer for your classname. In this case I'd recommend renaming the classname that's generated for you to remove the "rtk_" part.
Wayde - October 10, 2008 - Very nice!



Not being a LinqToSql fan ... I look forward to your SubSonic implementation. Hopefully that is coming sooner than later. Any ideas on when we could expect it would be nice.



That brings a question to mind: Can this be extended by folks to create an implementation for whatever ORM they want? For example, can I create a plug in NHibernate?



Thanks again
robconery - October 10, 2008 - Hi Wayde - I'm going to redo the templates to be a bit more "workable" so you can alter as needed. That's the next step here, as soon as I can figure out why people's browsers are crashing :).
kcoleman - October 14, 2008 - Whenever I have a column named SomethingId that is the primary key and an identity column, the Subsonic scaffolding generator creates a method to get a record by ID that contains a statement that uses a property with ID capitalized. For example: items.SomethingID.
robconery - October 14, 2008 - Yep - dumb copy/replace bug I let creep in. This is fixed for the next drop, coming in a day or so.
chrisntr - October 7, 2008 - Why does the video look really familiar? Was something similar not posted right after ASP.NET MVC had been officially announced? :)
dimitris - October 7, 2008 - Kittens / Underworld?



oh, and nice work!
James - October 7, 2008 - Can't find a Linq To Sql schema file in the solution :(



I have it in a diff project, but its def. in here.



any ideas?
Kevin Dente - October 7, 2008 - Very cool. Love the presentation style too - probably your best screencast to date.
robconery - October 7, 2008 - Hi James - it should find the DBML in another project; i'll take a look here.
robconery - October 7, 2008 - A Chicken Dinner for you! Yep - Underworld it is...
robconery - October 7, 2008 - The thing I made before was a bit different - and I decided to wait a bit to have the driver be an actual application rather than what I thought was neat :).
eibrahim - October 7, 2008 - Is it Kittens by Underworld?
eibrahim - October 7, 2008 - never mind, I just saw that someone already replied with the correct answer... And by the way I used an iPhone app called Shazam that listens to 10 seconds of any song and tells you what it is. I wasn't cheating but I was using the right tools :)
eibrahim - October 7, 2008 - I just finished the video... This is *profanity* awesome... I love the use IEntityRepository and IEntityService patterns. It will make TDD easy to do. I would love to help with your testing but unfortunately, I will be traveling for the next 2 weeks. Keep it up.
Martin Nyborg - October 7, 2008 - You are f... amazing



Have been watching all the MVC web casts and have learned a lot. But I am a worried for the future of L2SQL, are you to?
Dave Neeley - October 7, 2008 - "Add in Guy", "they work on my machine"--hilarious. still laughing. :)
Trevor - October 7, 2008 - Looks good. Just wondering how this scaffolding might compare to the Dynamic Data MVC scaffolding.



Thanks,



Trevor
Paul Kohler - October 7, 2008 - On using the Rails structure - I agree, why change it (at the same time not really that fussed, the main structure is there with controllers, models etc). I noticed "DataAccess" as opposed to "Models"...

Great idea with using IAuthenticationService in the accounts controller.

Big +1 on the Mailer - I find most sites send emails so why not include it.

+1 on the logging too - I have not coded a site that does not have logging.

I like that DI is in there by default but is MSFT going let that fly (as opposed to using Unity etc).

I usually find that I end up with at least 2 base controllers - one for public (PublicController) stuff and one for "members" (MemberController) with the main difference being authentication etc. Not a big thing but if we include authentication by default...

One biff - I hate seeing the "aspnetdb.mdf" db in the app_data folder - it's fine for development land but a long way from deployment. I would prefer to see setup scripts etc (I know there is aspnet_regsql.exe but again there's a bit of a disconnect with the sites code).

All other bits I passively agree with!

BTW, nice job ;-)



(PS, check line 11 of Site.Master and the MasterPageFile path in Error.aspx)
Bruno Caimar - October 7, 2008 - Wonderful job, Rob! The screencast is awesome too!

I will start to use it tomorrow morning.
firefly - October 7, 2008 - Wow... I like this one! Thanks Rob
RichMercer - October 7, 2008 - Oh, this is awesome Rob! I've been wanting something like this for MVC. Thanks!
Kevin - October 7, 2008 - Rob, it made Visual Studio vanish. No error message or anything, just went away. Pretty neat trick, I wish VS would close that fast normally.
brandyr - October 8, 2008 - Same issue in Germany :-)

Very cool stuff
brandyr - October 8, 2008 - Same issue in Germany



very cool stuff Rob.
Rick Hoskins - October 8, 2008 - To anyone having a problem with this addin causing VS to die when you press "Go" :



To resolve the script problem, we need to create a folder in the "content" folder, called Scripts.

If this was a fresh solution, all will be well. If we have run the "bad" addin code against the solution tho, there would now be a problem, because the code will have created the "app" folder on the disk, but not added it to the solution.

To resolve the Repository problem, check to see if the "app" folder exists off the root of the solution (show all files button in the Solution window taskbar in VS2008), and if it does, include the "App" folder in the solution and save the solution.

Run the scaffold generator from the context menu, it should now work.



Hope this helps.
OmegaSupreme - October 8, 2008 - This is big, nice work ! I use one list and one form for my RESTful admin forms though.
James - October 8, 2008 - Hey rob, just to update, I put the binaries in the addins folder, and re-installed the MSI. Still tells me it cant find the dbml, so if i put one in there,

I see the subsonic dialog pop up for a split second, then it crashes the IDE.
Rick Hoskins - October 8, 2008 - Having investigated this further, it seems to be an issue with the ICSharpCode.SharpZipLib.DLL.

Copy the above file from C:\Program Files\SubSonic\SubSonic Makai folder, into your Addins folder, mine was in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Addins ... once it's there, the whole process should work and you will get the scripts folder populated with the right script files also.

Hopefully Rob will put a version up that puts this file in a suitable place, my guess is this is an OS-version based problem, only happening to folks on just the right (wrong) OS release.
robconery - October 8, 2008 - Well now that's weird. I've changed the installer to explicitly add the ICSharp stuff. The installer is supposed to do this - I don't get it.



I've uploaded the changed bits.
robconery - October 8, 2008 - Hi Kevin - any info you can give me would help here. I've updated the installer so if you want to try again...
tasarim - October 8, 2008 - 1-)In IService.aspx template you use using #DATANAMESPACE#; this line TWICE remove one.

2-)Like mentioned aboce ICSharpCode.SharpZipLib.dll is not copied to addins folder (in my case under Vista)

3-)Also mentioned above you need to create Scripts folder manually before using it
fschwiet - November 9, 2008 - OMG I LOVE THE TECHNO
Wayde - October 13, 2008 - Rob,



I have a couple of questions regarding how, if at all, a couple routing features available in Rails are available in ASP.NET MVC ... and if so, are they supported by your custom ViewEngine in the template.



* Nested Routes

In Rails we can, for example, nest comment resources inside a post resource simply by specifying something like "map.resources :posts, :has_many => :comments" in our routes.rb. Is this feature supported by ASP.NET MVC and if so, is it supported by your template?



* Namespaced Routes

In Rails we can create namespaced routes by running something like "./script/generate controller Admin::Posts" and modifying our routes.rb file to look like this:



map.namespace :admin do |admin|

admin.resources :posts

end



This results in the following directory structure ... /views/admin/posts giving us the ability to lock down anything in admin to the "admin" role and allowing "admins" to work with Post resources differently than regular users. Is this possible with ASP.NET MVC and supported in your template as well?





If nested routes and namespaced routes aren't supported ... I'd be curious to know what is the recommend practice for producing the same funtionality in ASP.NET MVC.



Thanks again!

Chris Slee - October 23, 2008 - Great tool.. it really has helped out a lot.. keep up the great work!
kcoleman - October 16, 2008 - Another Question: Why are the partial pages in the views/shared folder aspx files vs. ascx? The code gen creates these pages so they inherit from ascx.cs. However, these files don't exist. I updated the reference to point to the .aspx.cs file and all seems to be OK. Either the code gen needs to create ascx files or the code gen needs to be updated so the aspx pages reference aspx.cs.



Also, there's something funky about your css (a guess) that causes field labels of drop down boxes on edit and new pages to appear and disappear depending where they fall on the page. Scrolling the page or shrinking the height of the browser causes them to appear or disappear.
robert - December 1, 2008 - Hi, excellent tool! It does seem to have some quirks yet, so I always have to edit the code manually afterwards, but still, tremendously helpful!

Any chance of updating it to the latest MVC version?
mafioso710 - June 12, 2009 - Hi, would you like to explain me how is the easy and simple way to change the HeaderText of the GridView control using the SubSonic Scaffolding ? Also, I would like to rename the text of every label control in the Add and Edit Form that is generated with the SubSonic Scaffold. I got my Scaffolding Form in English idiom but I want to translate into Spanish idiom. I use SubSonic version 2.1 (Final) Thanks.
Andrew Lewis - November 16, 2009 - I would love to see an update to the latest version. Especial with the changes to forms (Html.Form vs Html.BeginForm)
Hector Minaya - November 18, 2009 - Are you planning to update this?
Gecko