This project is a source generator which produces Splat based registrations for both constructor and property injection.
Make sure your project is using the newer PackageReference
inside your CSPROJ. The older style is buggy and should be moved away from regardless. See here for discussions how to upgrade.
Install the following packages:
Name | Platform | NuGet |
---|---|---|
Splat.DependencyInjection.SourceGenerator | Core - Libary |
ObservableEvents generator registrations for Splat based on your constructors and properties. It will not use reflection and instead uses Source Generation. You should get full native speed.
Include the following in your .csproj file
<PackageReference Include="Splat.DependencyInjection.SourceGenerator" Version="{latest version}" PrivateAssets="all" />
The PrivateAssets
will prevent the Source generator from being inherited into other projects.
Register your dependencies using the SplatRegistrations
class.
There are two methods.
Register()
will generate a new instance each time. Use generic parameters, first for the interface type, second for the concrete type.
SplatRegistrations.Register<IMenuUseCase, MenuUseCase>();
SplatRegistrations.Register<IOtherDependency, OtherDependency>();
RegisterLazySingleton()
will have a lazy instance. Use generic parameters, first for the interface type, second for the concrete type.
SplatRegistrations.RegisterLazySingleton<IMessagesSqlDataSource, MessagesSqlDataSource>();
You must call either SplatRegistrations.SetupIOC()
or with the specialisation SplatRegistrations.SetupIOC(resolver)
once during your application start. This must be done in each assembly where you use SplatRegistrations.
The resolver version of SetupIOC
is used mainly for unit tests.
If there are more than one constructor use the [DependencyInjectionConstructor]
attribute to signify which one should be used.
[DependencyInjectionConstructor]
public AuthApi(
Lazy<IJsonService> jsonService,
: base(jsonService)
{
}
You don't need to decorate when there is only one constructor.
Use the [DependencyInjectionProperty]
above a property to be initialized. It must be public
or internal
setter.
public class MySpecialClass
{
[DependencyInjectionProperty]
public IService MyService { get; set; }
}