Class AppBuilder
Provides a builder for configuring and constructing application dependency resolution and service registration using Splat patterns.
public class AppBuilder : IAppBuilder, IAppInstance
- Inheritance
-
AppBuilder
- Implements
- Extension Methods
Remarks
The AppBuilder class enables fluent configuration of dependency resolvers, service modules, and custom registrations for applications using Splat. It supports chaining of registration methods and ensures that core services are registered before building the application instance. AppBuilder is typically used during application startup to configure dependency injection and service location. Thread safety is not guaranteed; configuration should be completed before the application is accessed from multiple threads.
Constructors
AppBuilder(IMutableDependencyResolver, IReadonlyDependencyResolver?)
Initializes a new instance of the AppBuilder class with the specified dependency resolver and an optional. current resolver.
public AppBuilder(IMutableDependencyResolver resolver, IReadonlyDependencyResolver? current = null)
Parameters
resolverIMutableDependencyResolverThe dependency resolver to use for registering and resolving services. Cannot be null.
currentIReadonlyDependencyResolverAn optional read-only dependency resolver to use as the current resolver. If null, only the mutable resolver is used.
Properties
Current
Gets the current dependency resolver in use by the application.
public IReadonlyDependencyResolver? Current { get; }
Property Value
Remarks
The dependency resolver is responsible for providing service instances throughout the application's lifetime. If no resolver has been set, this property may return null.
CurrentMutable
Gets the current mutable dependency resolver used for registering and resolving services at runtime.
public IMutableDependencyResolver CurrentMutable { get; }
Property Value
Remarks
Use this property to add or override service registrations dynamically. Changes made to the resolver affect dependency resolution for subsequent requests.
HasBeenBuilt
Gets a value indicating whether this instance has been built.
public static bool HasBeenBuilt { get; }
Property Value
- bool
trueif this instance has been built; otherwise,false.
UsingBuilder
Gets a value indicating whether the application builder is being used.
public static bool UsingBuilder { get; }
Property Value
- bool
trueif using the application builder; otherwise,false.
Methods
Build()
Finalizes the configuration and builds the application instance, making it ready for use.
public IAppInstance Build()
Returns
- IAppInstance
The current application instance with all configured services and registrations applied.
Remarks
Subsequent calls to this method after the initial build have no effect and return the same instance. After building, further modifications to the builder's configuration are not applied.
CreateSplatBuilder()
Creates a new instance of the application builder using the current mutable and immutable service locators.
public static AppBuilder CreateSplatBuilder()
Returns
- AppBuilder
An AppBuilder initialized with the current service locator configuration.
Remarks
Use this method to configure dependency injection and service registration for the application using the Splat framework. The returned builder is preconfigured with the application's current service locators.
ResetBuilderStateForTests()
Resets the internal builder state to its initial values for use in unit tests.
public static void ResetBuilderStateForTests()
Remarks
This method is intended for test scenarios only. It should not be used in production code, as it may affect the global state shared by other components.
UseCurrentSplatLocator()
Configures the application to use the current Splat service locator (AppLocator.CurrentMutable) for dependency resolution.
public IAppBuilder UseCurrentSplatLocator()
Returns
- IAppBuilder
The current IAppBuilder instance. This enables method chaining.
Remarks
This method sets the resolver and service provider delegates to use the current Splat AppLocator. Use this method when you want the application to resolve dependencies using the global Splat locator, which may be shared across different parts of the application. This is useful when configuring an external container (e.g., Autofac, DryIoc, Microsoft.Extensions.DependencyInjection) as the Splat dependency resolver prior to applying Splat registrations.
UsingModule<T>(T)
Registers a module for application configuration using the specified module instance.
public IAppBuilder UsingModule<T>(T registrationModule) where T : IModule
Parameters
registrationModuleTThe module instance to register. Cannot be null.
Returns
- IAppBuilder
The current IAppBuilder instance to allow method chaining.
Type Parameters
TThe type of the module to register. Must implement the IModule interface.
WithCoreServices()
Adds the core framework services to the application builder.
public virtual IAppBuilder WithCoreServices()
Returns
- IAppBuilder
The current IAppBuilder instance with core services registered.
Remarks
This method is typically called during application startup to ensure that essential services required by the framework are available. It can be used in a fluent configuration chain.
WithCustomRegistration(Action<IMutableDependencyResolver>)
Adds a custom dependency registration action to the application builder.
public IAppBuilder WithCustomRegistration(Action<IMutableDependencyResolver> configureAction)
Parameters
configureActionAction<IMutableDependencyResolver>An action that receives an IMutableDependencyResolver and performs custom service registrations. Cannot be null.
Returns
- IAppBuilder
The current IAppBuilder instance, enabling method chaining.
Remarks
Use this method to register additional services or override default registrations in the application's dependency resolver before the application is built.