)} ,System.String)} ,System.Type)} ,System.Type,System.String)}
GlobalGenericFirstDependencyResolver.Register(Func) method¶
Defined in
Type: GlobalGenericFirstDependencyResolver
Namespace: Splat
Assembly: Splat.Core.dll
Applies to
net10.0, net10.0-browserwasm1.0, net10.0-desktop1.0, net9.0, net9.0-browserwasm1.0, net9.0-desktop1.0, net8.0, net8.0-ios17.5, net8.0-maccatalyst17.5, net8.0-macos14.2, net8.0-macos14.5, net8.0-tvos17.2, netstandard2.1, net462, net481
Overloads¶
- 1.
public void Register<T>(Func<T?> factory) - 2.
public void Register<T>(Func<T?> factory, string? contract) - 3.
public void Register(Func<object?> factory, Type? serviceType) - 4.
public void Register(Func<object?> factory, Type? serviceType, string? contract) - 5.
public void Register<TService, TImplementation>() where TService : class where TImplementation : class, TService, new() - 6.
public void Register<TService, TImplementation>(string? contract) where TService : class where TImplementation : class, TService, new()
1. Overload¶
public void Register<T>(Func<T?> factory)
Inherited documentation
These docs were inherited from IMutableDependencyResolver. The member doesn't override them on this type.
Summary: Register a function with the resolver which will generate an object for the specified service type. Most implementations will use a stack based approach to allow for multiple items to be registered.
Type parameters
| Name | Description |
|---|---|
T | The type which is used for the registration. |
Parameters
| Name | Type | Description |
|---|---|---|
factory | Func | The factory function which generates our object. |
Remarks
This generic method is preferred over the non-generic Register method for better performance and type safety. It enables optimizations in resolvers like GlobalGenericFirstDependencyResolver which use static generic containers for zero-cost service resolution.
2. Overload¶
public void Register<T>(Func<T?> factory, string? contract)
Inherited documentation
These docs were inherited from IMutableDependencyResolver. The member doesn't override them on this type.
Summary: Register a function with the resolver which will generate an object for the specified service type. Optionally a contract can be registered which will indicate that registration will only work with that contract. Most implementations will use a stack based approach to allow for multiple items to be registered.
Type parameters
| Name | Description |
|---|---|
T | The type which is used for the registration. |
Parameters
| Name | Type | Description |
|---|---|---|
factory | Func | The factory function which generates our object. |
contract | string? | A contract value which will indicates to only generate the value if this contract is specified. |
Remarks
This generic method is preferred over the non-generic Register method for better performance and type safety. It enables optimizations in resolvers like GlobalGenericFirstDependencyResolver which use static generic containers for zero-cost service resolution.
3. Overload¶
public void Register(Func<object?> factory, Type? serviceType)
Inherited documentation
These docs were inherited from IMutableDependencyResolver. The member doesn't override them on this type.
Summary: Registers a factory method for creating instances of the specified service type.
Parameters
| Name | Type | Description |
|---|---|---|
factory | Func | A delegate that returns an instance of the service. The delegate may return null if appropriate for the service. |
serviceType | Type? | The type of the service to register. If null, the type may be inferred from the factory's return type. |
Remarks
Use this method to provide custom logic for creating service instances. If serviceType is null, the registration mechanism may attempt to infer the service type from the factory delegate's return type.
4. Overload¶
public void Register(Func<object?> factory, Type? serviceType, string? contract)
Inherited documentation
These docs were inherited from IMutableDependencyResolver. The member doesn't override them on this type.
Summary: Registers a factory method for creating instances of a specified service type and contract.
Parameters
| Name | Type | Description |
|---|---|---|
factory | Func | A delegate that returns an instance of the service to register. Cannot be null. |
serviceType | Type? | The type of the service to register. If null, the type is inferred from the factory's return value. |
contract | string? | An optional contract name that distinguishes this registration from others of the same service type. Can be null or empty for the default contract. |
Remarks
Use this method to register services with custom creation logic, such as when dependencies or configuration are required at instantiation. If multiple registrations exist for the same service type and contract, the most recent registration may take precedence, depending on the container's behavior.
5. Overload¶
public void Register<TService, TImplementation>() where TService : class where TImplementation : class, TService, new()
Inherited documentation
These docs were inherited from IMutableDependencyResolver. The member doesn't override them on this type.
Summary: Registers a service type and its implementation for dependency resolution.
Type parameters
| Name | Description |
|---|---|
TService | The interface or base class type to register as a service. Must be a reference type. |
TImplementation | The concrete implementation type to instantiate when resolving the service. Must be a reference type, implement TService, and have a public parameterless constructor. |
Remarks
Subsequent requests for TService will resolve to instances of TImplementation. If the service type is already registered, this method may overwrite the existing registration depending on the implementation.
6. Overload¶
public void Register<TService, TImplementation>(string? contract) where TService : class where TImplementation : class, TService, new()
Inherited documentation
These docs were inherited from IMutableDependencyResolver. The member doesn't override them on this type.
Summary: Registers a service implementation with an optional contract name for dependency resolution.
Type parameters
| Name | Description |
|---|---|
TService | The type of the service to register. Must be a reference type. |
TImplementation | The concrete implementation type to register for the service. Must be a reference type with a public parameterless constructor. |
Parameters
| Name | Type | Description |
|---|---|---|
contract | string? | An optional contract name that distinguishes this registration from others of the same service type. Specify null to register the implementation without a contract. |
Remarks
Use this method to associate a service interface or base class with a specific implementation, optionally under a contract name. This enables resolving different implementations of the same service type by contract. If multiple implementations are registered for the same service and contract, the behavior may depend on the container's resolution strategy.