You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi everyone, I created this discussion because I use both ReactiveUI.SourceGenerators and Splat.DependencyInjection.SourceGenerator in one of my personal projects, and I noticed a behavior that I can't quite wrap my head around, so I'm looking for answers to understand the pros and cons.
Please be aware that I'm quite junior on the subject 😁.
Summary
When I register a View/ViewModel pair via the [IViewFor<TViewModel>] attribute from ReactiveUI.SourceGenerators and I register the ViewModel's dependencies via Splat.DependencyInjection.SourceGenerator (SplatRegistrations.Register* + SetupIOC()), the ViewModel's constructor parameters are not injected when the VM is resolved through the generated registration.
The only way I have found to make it work is to fall back to a Service Locator pattern inside the ViewModel's constructor, which defeats most of the point of having constructor injection generated by Splat.DI.SourceGenerator.
Context / Environment
OS: Windows 11
App type: Avalonia UI 11.3.13
ReactiveUI.Avalonia 11.4.12
ReactiveUI.SourceGenerators 2.6.1
Splat.DependencyInjection.SourceGenerator 2.2.2
Repro
View (registered via the IViewFor source generator), as shown in the ReactiveUI.SourceGenerators README:
When the View is resolved (and therefore the VM is instantiated through the registration generated by [IViewFor]), IMyService is not injected into MyCustomViewModel, even though IMyService is correctly registered and SplatRegistrations.SetupIOC() is called.
With this Service Locator fallback in the constructor, everything works. But:
it bypasses the whole point of Splat.DI.SourceGenerator's generated constructor resolution.
it forces every VM to know about Locator.Current.
So I'm wondering: is this intended behaviour? If so, what's the reasoning behind it, and what would be the recommended way to combine both generators? Or did I just miss something in the setup?
Happy to provide a minimal repro repo if it helps. Thanks!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone, I created this discussion because I use both ReactiveUI.SourceGenerators and Splat.DependencyInjection.SourceGenerator in one of my personal projects, and I noticed a behavior that I can't quite wrap my head around, so I'm looking for answers to understand the pros and cons.
Please be aware that I'm quite junior on the subject 😁.
Summary
When I register a View/ViewModel pair via the
[IViewFor<TViewModel>]attribute from ReactiveUI.SourceGenerators and I register the ViewModel's dependencies via Splat.DependencyInjection.SourceGenerator (SplatRegistrations.Register*+SetupIOC()), the ViewModel's constructor parameters are not injected when the VM is resolved through the generated registration.The only way I have found to make it work is to fall back to a Service Locator pattern inside the ViewModel's constructor, which defeats most of the point of having constructor injection generated by Splat.DI.SourceGenerator.
Context / Environment
Repro
View (registered via the IViewFor source generator), as shown in the ReactiveUI.SourceGenerators README:
ViewModel with a constructor dependency:
Service registration via Splat.DI.SourceGenerator:
Observed behaviour
When the View is resolved (and therefore the VM is instantiated through the registration generated by
[IViewFor]),IMyServiceis not injected intoMyCustomViewModel, even thoughIMyServiceis correctly registered andSplatRegistrations.SetupIOC()is called.So I need to use a workaround:
With this Service Locator fallback in the constructor, everything works. But:
Locator.Current.So I'm wondering: is this intended behaviour? If so, what's the reasoning behind it, and what would be the recommended way to combine both generators? Or did I just miss something in the setup?
Happy to provide a minimal repro repo if it helps. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions