Redakt uses dependency injection throughout the Redakt system. Services are registered with the built-in .NET Core IoC container in the
Registering Redakt services
To make Redakt service registration simpler, you use the
IRedaktBuilder interface in the
Microsoft.Extensions.DependencyInjection namespace. All Redakt service registrations are done through extension methods on this interface. Every module contains an
AddSomeRedaktModule() type extension method that will add all required services for that module to the container.
You can obtain a reference to the
IRedaktBuilder interface through the
AddRedakt() extension method on the
IServiceCollection interface. This is normally done in the
ConfigureServices() method of your project's
Startup.cs file. If you've followed the project setup guide, your
ConfigureServices() method looks similar to this:
public void ConfigureServices(IServiceCollection services)
// ... framework and other services
var redaktBuilder = services.AddRedakt(Configuration);
In this scenario, the Redakt Back Office application, Identity Server, Content Management, and Lite DB database modules have been registered. You can add other modules in the same way, with a
redaktBuilder.AddSomeRedaktModule() call. The order of adding modules here is not important. All Redakt builder extension methods are in the same namespace, so Intellisense will show you the available Redakt extension methods if you have installed the corresponding NuGet packages.
You can inject Redakt services into your own code at any place where services may be injected, like controllers, attributes, filters, etc. All Redakt services and components are registered with the IoC container, so you can inject any service you need. You will usually want to inject one of the following services.
This is the interface for the main Redakt application class, part of the
Redakt.Core namespace. It contains the services collection and global options. If you only need a single Redakt service, you can also inject just that service interface instead of the whole Redakt application instance.
For request-bound code, you can inject the
IRedaktContext interface from the
Redakt.Web namespace. This contains the current site object, page object, HTTP context, and helper methods. This is the interface you will normally want to inject into custom controllers, attributes, and tag helpers. It is also passed as the model into all Razor page views.
Please note that modifying properties of the Redakt context during a request can lead to unexpected results and is generally not recommended.
Replacing the built-in container
The built-in IoC container is normally adequate, but if you prefer a different (faster or more feature-rich) container, a number of mainstream containers can be configured to replace the built-in .NET Core container. See the respective container's documentation for how to do this (some linked below).