I’m in the process of re-writing my own production project to use fubuMVC and I wanted to share how easy it was to get started with the framework and use the fubu power to get up and running with very little ceremony.
Step one:
Create a new 4.0 ASP.NET web application and reference the libraries I need to use fubuMVC:
Step two:
In the Global.asax.cs start wiring up the FubuApplication.
To do this, in my application start method I hook up my own (currently empty) implementation of FubuRegistry, with a new structure map container and Bootstrap using the routes from the route table.
protected void Application_Start(object sender, EventArgs e) { FubuApplication .For<XercesFubuRegistry>() .StructureMap(() => new Container()) .Bootstrap(RouteTable.Routes); } public class XercesFubuRegistry : FubuRegistry { }
Step three:
Declare the setup of my FubuRegistry
public class XercesFubuRegistry : FubuRegistry { public XercesFubuRegistry() { IncludeDiagnostics(true); Applies.ToThisAssembly(); Actions.IncludeClassesSuffixedWithController(); Routes.HomeIs<HomeController>(c => c.FrontPage()) .IgnoreControllerNamespaceEntirely(); this.UseSpark(); Views.TryToAttachWithDefaultConventions(); } }
I’m including diagnostics so that I can use fubuMVC’s excellent diagnostic features to help me visualise how the code in the site works for each url as I build it (more on this below).
I’ve decided to stick with a “controller actions” concept so I tell fubuMVC that actions are those classes that end with “Controller”.
I declare my home route as the HomeController (which I’ve yet to create) and declared a method on there called FrontPage.
I also tell fubuMVC to ignore the namespaces of my controller class when building the routes.
I’m using the spark view engine and I tell fubuMVC to wire up the views with default conventions – here it will use a number of ways to match the view based on the model returned by the FrontPage method – I’m essentially wanting it to look for the only concrete implementation of that type.
Step four:
Create the controller – it does not take any parameters in the front page method and it returns a HomeViewModel.
This is a zero model in, one model out concept that fubuMVC is very opinionated on (along with the one model in, one model out concept).
public class HomeController { public HomeViewModel FrontPage() { return new HomeViewModel(); } } public class HomeViewModel { }
Step five:
Create my view.
I’m actually importing the code from my existing home page but now I can call this view whatever I want rather than relying on naming conventions and location matching – one of the beautiful things about fubuMVC is that I am free to put my actions and views anywhere I like and it will hook them up based on the types concerned – a HomeController type for the action and a view that uses the HomeViewModel for my output.
My spark view, frontpage.spark, now uses the HomeViewModel:
<viewdata model="Xerces.Web.Core.Home.HomeViewModel" /> <div id="imageContainer"> ...etc
Here’s a picture of my web application illustrating that I can have the action and views where I want:
Step six:
Confirm that it all works!
I set the start page of my web application to “_fubu/actions” – this points to one of the diagnostics pages for my fubuMVC application – and I debug the website:
The fubuMVC diagnostics shows me that I have one registered action that is the HomeController I set up. The action returns a HomeViewModel and the view it has found that ties up with this output model is my frontpage.spark view.
I can click on the “(default)” route to take me to the website and my home page loads!
(note that using the diagnostics as my start page is a personal preference and isn’t required)
This was very easy and fubuMVC is doing a lot of the work for me so that out of the box it just makes my life simple.
I’m going to love working with this framework.
4 comments: