Etienne Charland
Etienne Charland

Reputation: 4024

ReactiveUI/Splat Dependency Registration Pattern

I'm looking at ReactiveUI Dependency Injection and there's something that is bugging me.

Normally I would register classes like this

Locator.Register<IToaster, Toaster>();

With Splat, it asks for registration like this

Locator.CurrentMutable.Register(() => new Toaster(), typeof(IToaster));

The problem is that, in a real application, Toaster will contain 5 dependencies, each of which may have 2 to 4 dependencies each, some of which also have dependencies, and there's a whole tree to initialize. That's the whole point of Dependency Injection, to manage that.

Am I to do that work manually, or am I missing something? What are my options?

EDIT: I found part of the answer. This is what I need: Splat.DI.SourceGenerator

BUT I can't find how to call SplatRegistrations.Register. It doesn't seem to be a popular library at all! Is going with ReactiveUI's default IoC the way to go or not?

EDIT2: It seems easier to integrate another DI container than to use SplatRegistrations...

Using Microsoft.Extensions.DependencyInjection

var services = new ServiceCollection();
services.AddTransient<MainWindowViewModel>();
services.AddTransient<CurrentTimeDialogViewModel>();
services.AddSingleton<IDialogService>(_ => new DialogService());
services.UseMicrosoftDependencyResolver();

but this is giving me thread exceptions all over the place.

How can I struggle with something so simple?

Upvotes: 0

Views: 1495

Answers (2)

iRumba
iRumba

Reputation: 99

Try it

RxApp.MainThreadScheduler = AvaloniaScheduler.Instance;

after it

services.UseMicrosoftDependencyResolver();

Upvotes: -1

Etienne Charland
Etienne Charland

Reputation: 4024

Splat.DI.SourceGenerator is the answer.

SplatRegistrations.Register wasn't recognized due to a bug in JetBrains Rider; but after restarting it, then it works.

Upvotes: 3

Related Questions