-
Notifications
You must be signed in to change notification settings - Fork 0
API Reference
Complete API documentation for SSDI.
The main entry point for SSDI. Inherits from ActivationBuilder.
public class DependencyInjectionContainer : ActivationBuilderRegisters services with the container. Can be called multiple times.
public void Configure(Action<ExportRegistration> registration)Example:
container.Configure(c =>
{
c.Export<MyService>();
c.Export<MyRepository>().As<IRepository>().Lifestyle.Singleton();
});Resolves a service by type.
public T Locate<T>()
public T Locate<T>(params DIParameter[] parameters)
public T Locate<T>(int position, object value)Examples:
var service = container.Locate<IMyService>();
var player = container.Locate<Player>(
DIParameter.Named("id", "player-1"));
var item = container.Locate<Item>(0, "sword");Resolves a service by runtime type.
public object Locate(Type type)Example:
Type serviceType = typeof(IMyService);
object service = container.Locate(serviceType);Resolves with positional parameters.
public T LocateWithPositionalParams<T>(params object[] parameters)Example:
var server = container.LocateWithPositionalParams<Server>("localhost", 8080);Resolves with named parameters.
public T LocateWithNamedParameters<T>(params (string name, object value)[] parameters)Example:
var server = container.LocateWithNamedParameters<Server>(
("host", "localhost"),
("port", 8080));Resolves with type-matched parameters.
public T LocateWithTypedParams<T>(params object[] parameters)Example:
var server = container.LocateWithTypedParams<Server>("localhost", 8080);Checks if a type is registered.
public bool IsRegistered<T>()
public bool IsRegistered(Type type)Example:
if (container.IsRegistered<ILogger>())
{
var logger = container.Locate<ILogger>();
}Removes a type registration. Disposes singleton instances if IDisposable.
public bool Unregister<T>(bool removeFromAliases = true)
public bool Unregister(Type type, bool removeFromAliases = true)Parameters:
-
removeFromAliases: Iftrue, also removes interface mappings. Default:true.
Returns: true if the type was registered and removed.
Example:
container.Unregister<OldService>();
container.Unregister<OldImpl>(removeFromAliases: false);Removes a type registration asynchronously. Properly awaits IAsyncDisposable.DisposeAsync() for singletons.
public Task<bool> UnregisterAsync<T>(bool removeFromAliases = true)
public Task<bool> UnregisterAsync(Type type, bool removeFromAliases = true)Parameters:
-
removeFromAliases: Iftrue, also removes interface mappings. Default:true.
Returns: Task containing true if the type was registered and removed.
Example:
await container.UnregisterAsync<AsyncDisposableService>();Removes all implementations of an interface.
public int UnregisterAll<TAlias>()
public int UnregisterAll(Type aliasType)Returns: Number of types unregistered.
Example:
int count = container.UnregisterAll<IPacketHandler>();Removes all implementations of an interface asynchronously.
public Task<int> UnregisterAllAsync<TAlias>()
public Task<int> UnregisterAllAsync(Type aliasType)Returns: Task containing the number of types unregistered.
Example:
int count = await container.UnregisterAllAsync<IPacketHandler>();Creates a new scope for scoped services.
public IScope CreateScope()Example:
using var scope = container.CreateScope();
var service = scope.Locate<IScopedService>();Controls when factories are compiled.
public bool EagerCompilation { get; set; }-
false(default): Lazy compilation. Factories are compiled on firstLocate<T>()call. Fast registration (~27μs), slower first-time resolution for each type. -
true: Eager compilation. Factories are pre-compiled duringConfigure()with singletons embedded as constants. Slower registration (~10ms), fastest resolution (~19ns, near-parity with MS.DI/Grace).
Example:
// Lazy mode (default) - good for hot-swapping
var container = new DependencyInjectionContainer();
// Eager mode - for maximum resolution performance
var container = new DependencyInjectionContainer { EagerCompilation = true };Fires when a service is registered.
public event EventHandler<RegisteredEventArgs>? RegisteredExample:
container.Registered += (sender, args) =>
{
Console.WriteLine($"Registered: {args.RegisteredType.Name}");
};Fires asynchronously (fire-and-forget) when a service is registered.
public event Func<object?, RegisteredEventArgs, Task>? RegisteredAsyncExample:
container.RegisteredAsync += async (sender, args) =>
{
await NotifyServiceDiscovery(args.RegisteredType);
};Fires when a service is unregistered.
public event EventHandler<UnregisteredEventArgs>? UnregisteredExample:
container.Unregistered += (sender, args) =>
{
Console.WriteLine($"Unregistered: {args.UnregisteredType.Name}");
};Fires asynchronously (fire-and-forget) when a service is unregistered.
public event Func<object?, UnregisteredEventArgs, Task>? UnregisteredAsyncExample:
container.UnregisteredAsync += async (sender, args) =>
{
await CleanupExternalResources(args.UnregisteredType);
};Used within Configure() for registering services.
Registers a type.
public FluentExportRegistration Export<T>()
public FluentExportRegistration Export(Type type)Example:
c.Export<MyService>();
c.Export(typeof(MyService));Registers an existing instance as a singleton.
public FluentExportRegistration ExportInstance<T>(T instance)Example:
c.ExportInstance(configuration).As<IConfiguration>();Fluent API for configuring a registration.
Registers the type as an interface.
public FluentExportRegistration As<TAlias>()
public FluentExportRegistration As(Type type)Example:
c.Export<SqlRepository>().As<IRepository>();
c.Export<UniversalService>().As<IReader>().As<IWriter>();Provides constructor parameters.
public FluentExportRegistration WithCtorParam<TParam>(TParam value)
public FluentExportRegistration WithCtorParam(string name, object value)
public FluentExportRegistration WithCtorParam(int position, object value)
public FluentExportRegistration WithCtorParam(IDIParameter parameter)Examples:
// By type
c.Export<Server>().WithCtorParam<int>(8080);
// By name
c.Export<Server>().WithCtorParam("port", 8080);
// By position
c.Export<Server>().WithCtorParam(0, "localhost");Provides multiple positional parameters.
public FluentExportRegistration WithCtorPositionalParams(params object[] parameters)Example:
c.Export<Server>().WithCtorPositionalParams("localhost", 8080, true);Access lifetime configuration.
public LifestyleScope Lifestyle { get; }Example:
c.Export<MyService>().Lifestyle.Singleton();Configures service lifetime.
Service lives for the application lifetime.
public FluentExportRegistration Singleton()New instance every resolution (default).
public FluentExportRegistration Transient()One instance per scope.
public FluentExportRegistration Scoped()Represents a scope for scoped services.
public interface IScope : IDisposable, IAsyncDisposableIndicates if the scope has been disposed.
bool IsDisposed { get; }All Locate methods from DependencyInjectionContainer are available:
T Locate<T>()
object Locate(Type type)
T Locate<T>(params DIParameter[] parameters)
T Locate<T>(int position, object value)
T LocateWithPositionalParams<T>(params object[] parameters)
T LocateWithNamedParameters<T>(params (string name, object value)[] parameters)
T LocateWithTypedParams<T>(params object[] parameters)High-performance struct for parameter matching.
public readonly struct DIParameterCreates a parameter matched by name.
public static DIParameter Named(string name, object value)Creates a parameter matched by position (0-based).
public static DIParameter Positional(int position, object value)Creates a parameter matched by type.
public static DIParameter Typed(object value)Checks if this parameter matches a constructor parameter.
public bool Matches(string parameterName, int parameterPosition, Type parameterType)Matches parameters by name.
public class NamedParameter : IDIParameter
{
public NamedParameter(string name, object value)
}Matches parameters by position.
public class PositionalParameter : IDIParameter
{
public PositionalParameter(int position, object value)
}Matches parameters by type.
public class TypedParameter : IDIParameter
{
public TypedParameter(object value)
}Interface for custom parameter matching.
public interface IDIParameter
{
object Value { get; }
bool GetParameterValue(string parameterName, int parameterPosition, Type parameterType);
}Event data for the Registered event.
public class RegisteredEventArgs : EventArgsThe type that was registered.
public Type RegisteredType { get; }The alias types this registration is exposed as.
public IReadOnlyList<Type> Aliases { get; }The lifestyle of the registration.
public LifestyleType Lifestyle { get; }Whether this is a singleton with a pre-existing instance.
public bool HasInstance { get; }Event data for the Unregistered event.
public class UnregisteredEventArgs : EventArgsThe type that was unregistered.
public Type UnregisteredType { get; }The singleton instance, if one existed. null for transient/scoped services.
public object? Instance { get; }Whether the instance was disposed during unregistration.
public bool WasDisposed { get; }Enum defining service lifetimes.
public enum LifestyleType
{
Transient, // New instance every resolution (default)
Singleton, // Single instance for app lifetime
Scoped // One instance per scope
}container.Configure(c =>
{
// Basic
c.Export<Service>();
// Interface
c.Export<Service>().As<IService>();
// Instance
c.ExportInstance(obj).As<IService>();
// Lifetime
c.Export<Service>().Lifestyle.Singleton();
c.Export<Service>().Lifestyle.Scoped();
// Parameters
c.Export<Service>().WithCtorParam("name", value);
c.Export<Service>().WithCtorParam(0, value);
c.Export<Service>().WithCtorParam<int>(value);
});// Basic
var s = container.Locate<IService>();
// With parameters
var s = container.Locate<Service>(DIParameter.Named("id", 1));
var s = container.LocateWithPositionalParams<Service>(1, "name");
var s = container.LocateWithNamedParameters<Service>(("id", 1));
var s = container.LocateWithTypedParams<Service>(1, "name");
// Multiple implementations
var all = container.Locate<IEnumerable<IPlugin>>();
// Scoped
using var scope = container.CreateScope();
var s = scope.Locate<IScopedService>();// Sync
container.Unregister<Service>();
container.UnregisterAll<IPlugin>();
bool exists = container.IsRegistered<IService>();
// Async (for IAsyncDisposable singletons)
await container.UnregisterAsync<AsyncService>();
await container.UnregisterAllAsync<IAsyncPlugin>();// Registration events
container.Registered += (sender, args) =>
{
Console.WriteLine($"Registered: {args.RegisteredType.Name}");
};
container.RegisteredAsync += async (sender, args) =>
{
await NotifyExternalSystem(args.RegisteredType);
};
// Unregistration events
container.Unregistered += (sender, args) =>
{
if (args.WasDisposed)
Console.WriteLine($"Disposed: {args.UnregisteredType.Name}");
};
container.UnregisteredAsync += async (sender, args) =>
{
await CleanupResources(args.UnregisteredType);
};// Lazy mode (default) - fast registration, slower first resolution
var container = new DependencyInjectionContainer();
// Eager mode - slower registration, fastest resolution
var container = new DependencyInjectionContainer { EagerCompilation = true };