Fork me on GitHub

Modularity and Extensibility Edit on GitHub


What's described here is what's remaining of the original Bottles extensibility framework after being merged into FubuMVC.Core. If you hear experienced FubuMVC users say the word "Bottle," that just refers to a FubuMVC extension.

FubuMVC has strong mechanisms for extensibility and modularity that go well beyond most other web or service bus frameworks. You can:

  • Build extension assemblies that will be automatically pulled into the application at start up time
  • Create reusable conventions or policies that can be imported into a FubuMVC application
  • Author modular applications that implement features, "areas", or "slices" in separate projects

Extensions can include custom policies, new HTTP endpoints or message handlers, override or add application service registrations to StructureMap, and every other possible configuration you can do with FubuRegistry.

See Conventions and Policies for more information on custom policies.

Extensions

To build a reusable extension for FubuMVC, utilize the IFubuRegistryExtension interface to express the desired additions or changes to the application. Using our chain logging example from the previous section, we could build this extension to both add the logging policy and override the built in logging with this:


public class LoggingExtension : IFubuRegistryExtension
{
    public void Configure(FubuRegistry registry)
    {
        registry.Policies.Global.Add<MyLoggingPolicy>();

        registry.Services.ReplaceService<ILogger, MySpecialLogger>();
    }
}

and add it to your application like this:


public class MyFubuApp : FubuRegistry
{
    public MyFubuApp()
    {
        // Explicitly apply LoggingExtension
        Import<LoggingExtension>();
    }
}

Extension Assemblies

To mark an assembly as a valid FubuMVC extension, add an assembly level attribute called FubuModule to your assembly's AssemblyInfo.cs file (it doesn't have to be there, but that's idiomatic .Net).


[assembly: FubuModule]

When a FubuMVC application is bootstrapped, it:

  1. Searches for any assemblies from the application's bin directory that are marked with the [FubuModule] assembly
  2. FubuMVC scans those assemblies for any public, concrete IFubuRegistryExtension class
  3. Each IFubuRegistryExtension class is applied to the main FubuRegistry of the application being built up

Slices/Areas with FubuPackageRegistry

Please don't think of this feature as an equivalent to ASP.Net MVC's portable areas. There's no coupling from the main application to the extension and FubuMVC's extensibility model shown here is far more powerful.
Make sure the constructor function of your package registry is public.

FubuMVC has a built in mechanism to easily create vertical application slices using a built in form of IFubuRegistryExtension called FubuPackageRegistry. To build your own, just write a public subclass like this in your extension assembly:


public class MyExtensionRegistry : FubuPackageRegistry
{
    public MyExtensionRegistry() : base("extension")
    {
        // Will locate any handlers or endpoints from this
        // Assembly

        // Apply the MyLoggingPolicy to only the
        // chains from this extension
        Policies.Local.Add<MyLoggingPolicy>();

        // Can override services
        Services.ReplaceService<ILogger, MySpecialLogger>();

        // Can alter settings to the main app too
        AlterSettings<AssetSettings>(_ =>
        {
            _.AllowableExtensions.Add(".xls");
        });
    }
}