SubSonic: MVC Templates Available And A Security Update
First things first: I managed to do a really boneheaded maneuver for the starter site and left New Page creation open to anyone who can guess the URL. No excuses on my part – that was plain stupid. Anyway:
if a user goes to /view/newpage.aspx on the starter site they can add pages to your site (if you’re using the Starter Site).
You can fix this by changing lines 55 through 60 in PageView.aspx.cs to:
if (SiteUtility.UserCanEdit()) { SetupNewPage(); this.Title = "Create a New Page"; } else { Response.Redirect("~/login.aspx"); }
I’ve fixed this issue and am now loading the new site (2.0.3a) up to CodePlex. Feel free to call me names – I deserve it.
The Good News
For a long time people have been asking about architectural approaches to using SubSonic, and I usually always try to champion the MVC patter since I like it so much. The problem comes in when you consider how ASP.NET is built (with the WebForm/CodeBehind model) and how ActiveRecord can fall flat on its face in this model.
For instance – it’s all too tempting to spread your logic out through an application (in the code behind) and not use a central controller (aka “Business Facade”) when you’re using ActiveRecord since it’s really simple to do something like:
MyGridView.DataSource=Product.FetchAll(); MyGridView.DataBind();
This approach is handy for rapid development, but in some cases it can lead to sloppiness.
Enter The MVC Template Set
So this last week I had at our Code Generator and our templates, and I rolled out a nice MVC-ready set of templates that will “dumb down” the objects and collections, and force you to push everything through a Controller class (which is generated for you).
So now instead of calling “product.Save(”me”)” you have to do this:
Product product = ProductController.Get(newID);
product.ReorderLevel = 100;
ProductController.Save(product,"unit test");
Collections won’t load themselves either, instead you need to use the new “List()” command:
ProductCollection coll = ProductController.List();
You can also pass in a Query to the controller, which will load the list using the passed-in Query:
ProductCollection coll = ProductController.List(ProductController.Query() .WHERE(Product.Columns.CategoryID,5));
This makes the loading of the collections a lot more flexible
.
You can also pass in a Reader if you like to do the same thing:
IDataReader rdr = new Query(Product.Schema).ExecuteReader();
ProductCollection coll = ProductController.List(rdr);
The general idea here is that just about all data-related interaction has been pushed to the controller class (which are based on our current ODSControllers) and there is almost no logic in any of the generated objects.
All of the templates are partials, and the idea here is that you can add on to them and keep all your logic in one place (you don’t have a choice really).
I want to stress that we are NOT changing anything with the default stuff here – if you like ActiveRecord and want to keep using SubSonic like you always have been, it will be there for you. If you want to have a bit of the MVC architecture done up for you up front – check out these templates!
You can download them from here.
Setting Them Up
It’s not very difficult to switch over if you want to use these guys. I should mention first off that I haven’t committed anything to source yet, so you won’t see the changes in the trunk. I have added my dev build of sonic.exe and SubSonic.dll to the download file, so you’ll need to make sure you use those to power up the new templates (sonic.exe if you generate them).
To use the new templates, just change the templateDirectory setting in the SubSonicService to point to the new template set (I put mine in the SubSonic install directory):
<SubSonicService defaultProvider="Northwind" enableTrace="true" templateDirectory="C:\Program Files\SubSonic\SubSonic 2.0.3\Templates\MVC"> <providers> <clear/> <add name="Northwind" type="SubSonic.SqlDataProvider, SubSonic" connectionStringName="Northwind" generatedNamespace="Northwind"/> </providers> </SubSonicService>
And that’s it. Your new templates will kick up for you! I’m anxious to hear feedback and improvement ideas – so do let me know what you think!







