Fork me on GitHub

Bootstrapping FubuMVC Applications Edit on GitHub


While the basic patterns shown here are likely to continue into Jasper, we will strive to improve the configuration syntax inside of Jasper's equivalent to FubuRegistry.

See also Streamlining FubuMVC Bootstrapping & the Design Patterns Used from Jeremy's blog for more background.

The 3.0 release made a lot of changes from older versions to attempt to streamline the complexity and performance of bootstrapping applications. At this point, the entire configuration for your application is configured by a FubuRegistry -- either a custom class of your own or by directly working with a FubuRegistry object. The runtime model and facade to a running FubuMVC application is the FubuRuntime class.

Boostrapping Simple Applications

If all you want to do is start up a new FubuMVC application with nothing but the default policies, services, and endpoint discovery, use this simple call:


// Start a simple FubuMVC application
var runtime = FubuRuntime.Basic();

// When you want to shut it down
runtime.Dispose();

The sample above enables the HTTP service support without any actual http server hosting, but doesn't enable any of the service bus or job execution features. To spin up a simple application using the service bus and job execution, use this syntax instead:


var runtime = FubuRuntime.BasicBus();

Finally, if you want to make a few alterations to the application setup but not enough to make you want to use the FubuRegistry mechanism explained in the next section, you can use a nested closure in FubuRuntime.Basic() to apply your customizations before the application is bootstrapped:


var runtime = FubuRuntime.Basic(_ =>
{
    _.Features.Diagnostics.Enable(TraceLevel.Verbose);
});

Using FubuRegistry for More Complicated Applications

I strongly recommend using a custom FubuRegistry class in your application so that you can easily reuse the same configuration in production hosting, development hosting, and in various automated testing harnesses.

Any complex application is probably going to warrant a separate FubuRegistry class to describe your application and the overrides, extensions, and settings for your application.

A custom FubuRegistry will look something like this one below:


public class MyApplication : FubuRegistry
{
    public MyApplication()
    {
        // If you don't like fubu's "Endpoint" naming
        // convention, use "Controller" to find http
        // action candidates instead
        Actions.DisableDefaultActionSource();
        Actions.IncludeClassesSuffixedWithController();

        Services.AddService<IActivator, MyCustomActivator>();
    }
}

All the configuration is done inside of the constructor function of the class (or helper methods called from the constructor if you want). FubuRegistry is an example of object scoping where you attempt to simplify the syntax of an internal DSL by shortening the calling chain.

To bootstrap a FubuRuntime from your custom FubuRegistry, you have these options:


// Bootstrap your application as is
var runtime = FubuRuntime.For<MyApplication>();

// or bootstrap your application as defined in
// your FubuRegistry, but override some settings
var runtime2 = FubuRuntime.For<MyApplication>(_ =>
{
    _.Features.Diagnostics.Enable(TraceLevel.Production);
});

// This is an alternative syntax for the sample above
// you may prefer instead
var registry = new MyApplication();
registry.Features.Diagnostics.Enable(TraceLevel.Production);
var runtime3 = registry.ToRuntime();