FubuMVC — My Starting Conventions

FubuMVC — My Starting Conventions

I finally had to scratch the itch and put my other toy projects on hold to have a play with RavenDB and FubuMVC. Immediately, I found my first point of interest — and one of FubuMVC’s selling points — pluggable conventions.

In a minute I’ll show you the conventions I have chosen as a starting point — I’ve taken inspiration from elsewhere, and myself imagined what works theoretically. These conventions relate to defining actions — which methods in code are accessible to the web; and basic url patterns. Finally I’ll show my starting convention for view-location as well.Action Per-Controller

It seems as fashionable as coding in Node.js at the moment, but actually, when you’ve worked on an ASP.NET MVC project with bloated controllers — a ridiculous amount of actions, and insane number of injected dependencies — having one action per-controller is rather practical. Because we define our own url policy, we’re not constrained by the framework where code has to live.

My implementation will mean the name of the controller is the name of action, while the name of the method is the http verb it responds to. Here’s an example:

public class PiggyBankEndpoint
{
public PiggyBankViewModel Get()
{
return new PiggyBankViewModel();
}
public String Post(DepositInputModel model)
{
// do some stuff with the model
return "Pennies in the piggy";
}
}

Oh, and by the way, I think I’m cool, so I don’t use the word controller — I copy the cool kids and call them endpoints (although WCF isn’t cool).Here’s where you’ll Find All the Action

Inside the constructor of my Fubu Registry, where I declare my basic conventions for deciding what an action is, I have this little snippet:

Actions

.IncludeTypes(t => t.Namespace.Contains(“Web.Endpoints”) && t.Name.EndsWith(“Endpoint”));

No, I won’t patronise you — I’m not a Microsoft MVP (joke).I Bet Those Url’s Look Messy

Before I applied my conventions — yes, yes they rather did. After my convention, though, now I’m cool again.

Routes
.IgnoreNamespaceText("Endpoints")
.IgnoreClassSuffix("Endpoint")
.IgnoreMethodsNamed("get")
.IgnoreMethodsNamed("post")
.ConstrainToHttpMethod(x => x.Method.Name.Equals("Get"), "Get")
.ConstrainToHttpMethod(x => x.Method.Name.Equals("Post"), "Post")
.RootAtAssemblyNamespace();

This time I will patronise you — because I want to be an MVP now.

So FubuMVC is going to use the namespace, the type of the controller and the name of the action. I’ve decided that doesn’t fit with my desired url conventions. So instead, I’m saying:

·  Do attempt that Mr FubuMVC

·  But strip out the word Endpoints from the url if it appears in a type’s namespace

·  Again, if a type’s name ends with Endpoint (which my convention says they all will), then don’t put this in the url either

·  If there is a method called get or post — remove the name of the method from the url and only allow access for the http method with the same nameShut Up and Show Me

Here are my endpoints:

Their type names ends with “Endpoint” and their namespace contains “Web.EndPoints” (oops — I patronised you J), so public methods for these types are my actions.

Using another selling point of Fubu, the basic diagnostics, we can now check what actions are available and the constraints that are applied to them:

Excellent — yes, FubuMVC is using the namespaces and type names to determine url structure. But with a few simple fluent configuration options, I’ve tweaked it to my liking.Take Me to Your Views

Views
.TryToAttach(x => x.by_ViewModel());
this.UseSpark();

Simple again — look for the view whose view model matches the return type of the action — they can live anywhere (for now). Below that I’ve said I’m using the spark view engine — NuGet: Install-Package FubuMVC.Spark.What Am I Thinking?

I’m thinking it can seem overwhelming to have to define your own conventions…but actually, it’s not. With the diagnostics to fall back on, at this early stage (approx. 30 lines of my own code), everything is definitely under control — jus make one small change at a time and observe how things change in the diagnostics.

Have I seen anything here that gives me confidence FubuMVC is an alternative — maybe even a better one — to ASP.NET MVC? Why yes, yes I have. “FubuMVC — the web framework that get’s out of your way” — it actually does.

It’s very early days, though, and I’m a bit of a fan boy – take me with a pinch of salt.