Skip to content

reactiveui/Splat.DI.SourceGenerator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Pull Requests Issues License Size codecov

Splat Source Generator

This project is a source generator which produces Splat based registrations for both constructor and property injection.

Installation

NuGet Packages

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 CoreBadge

What does it do?

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.

Installation

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.

How to use

Registration

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.

Constructor Injection

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.

Property Injection

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; }
}