Using FubuMVC - Day1

There is a change request coming which requires some software to fake an external system. It should be usable by testers in order to exercise the use cases. A nice situation to finally test fubu mvc, the swiss army knife (factory) for friends of MVC Web frameworks. The following is partly backed up from my twitter timeline, partly backed up from my memory and stitched up with some good will.

image

[UrlPattern("gna/{HelpId}")]
public HelpViewModel SomethingElse(UrlHelpInput input)
{
    return new HelpViewModel { Title = "Hello from Controller! " + input.HelpId };
}

public class UrlHelpInput
{
      [RouteInput]
      public string HelpId { get; set; }
}

Now that worked!

The following was just hacked into the UrlPolicy I had defined to dice routes out of the application’s actions:

public class TheAppUrlPolicy : IUrlPolicy
{
    public bool Matches(ActionCall call, IConfigurationObserver log)
    {
        return call.HandlerType.Name.EndsWith("Controller");
    }

    public IRouteDefinition Build(ActionCall call)
    {
        var route = call.ToRouteDefinition();
        route.Append(call.HandlerType.Name.RemoveSuffix("Controller"));
        route.Append(call.Method.Name);

        if (call.HasInput && call.InputType().Name.StartsWith("Url"))
            AddUrlPattern(route, call.InputType());

        return route;
    }

    private void AddUrlPattern(IRouteDefinition route, Type inputType)
    {
        var props = from p in inputType.GetProperties()
                    where p.CanWrite && p.CanRead && p.GetCustomAttributes(false).Any(o => o is RouteInputAttribute)
                    select p;
        foreach (var p in props)
            route.Input.AddRouteInput(new RouteParameter(new SingleProperty(p)), true);
    }
}

The IUrlPolicy is a fubu mvc interface and (an) implementator(s) can be registered at bootstrap-time. The basic code is copied form the HelloSpark-App, line 14 introduces a check whether the action has an input model defined and whether it starts with “Url”. If that’s the case, we iterate over all properties with getter and setter that are attributed with RouteInput and we add it to the defined route through a method available on the route definition. And you know what? That works. With that I concluded my first day with fubu.

Conlusion

Looking forward now to getting an app done with fubumvc. I’ve already spotted a couple of interfaces that tickled my fancy!