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:
- Searches for any assemblies from the application's bin directory
that are marked with the
[FubuModule]
assembly - FubuMVC scans those assemblies for any public, concrete
IFubuRegistryExtension
class - Each
IFubuRegistryExtension
class is applied to the mainFubuRegistry
of the application being built up
Slices/Areas with FubuPackageRegistry
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");
});
}
}