reactiveui / splat.di.sourcegenerator Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
Using Splat for Dependency Injection, I'm noticing that if a class is initialized and there are missing registrations, it will pass null to the constructor and I won't know about it. Is there a way to make it "fail early" and tell me there's a missing registration to build the object?
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These updates await pending status checks. To force their creation now, click the checkbox below.
xunit
, xunit.runner.visualstudio
)These updates have all been created already. Click a checkbox below to force a retry/rebase of any.
.github/workflows/ci-build.yml
.github/workflows/lock.yml
dessant/lock-threads v5
.github/workflows/release.yml
src/Directory.Build.props
Roslynator.Analyzers 4.12.0
stylecop.analyzers 1.2.0-beta.556
Nerdbank.GitVersioning 3.6.133
Microsoft.SourceLink.GitHub 8.0.0
src/Splat.DependencyInjection.SourceGenerator.Tests/Splat.DependencyInjection.SourceGenerator.Tests.csproj
Verify.Xunit 22.11.3
ReactiveMarbles.SourceGenerator.TestNuGetHelper 1.3.1
Verify.SourceGenerators 2.1.0
coverlet.msbuild 6.0.2
xunit.runner.visualstudio 2.5.7
xunit 2.7.0
Microsoft.NET.Test.Sdk 17.9.0
Microsoft.CodeAnalysis 4.8.0
src/Splat.DependencyInjection.SourceGenerator/Splat.DependencyInjection.SourceGenerator.csproj
ILRepack 2.0.28
ReactiveMarbles.RoslynHelpers 1.2.1
Microsoft.CodeAnalysis.CSharp.Workspaces 4.0.1
Microsoft.CodeAnalysis.Common 4.0.1
Microsoft.CodeAnalysis.Analyzers 3.3.4
I'm using this code to register a ViewModel for runtime and design-time.
Opening the designer or running the code gives this error:
ViewModelLocator.cs(50, 13): [SPLATDI006] MainViewModel has been registered in multiple places
MainViewModelDesign shows some section that is hidden in the standard ViewModel. If I run the app as-is, the hidden section is shown. If I comment the line registering MainViewModelDesign, then the section is hidden. I've set a breakpoint and confirm that the 2nd registration isn't executed.
My guess is that there's a problem in the way SplatRegistrations.Register
injects IL that bypasses the condition.
public static class ViewModelLocator
{
static ViewModelLocator()
{
var container = Locator.CurrentMutable;
if (!Design.IsDesignMode)
{
SplatRegistrations.Register<MainViewModel>();
}
else
{
SplatRegistrations.Register<MainViewModel, MainViewModelDesign>();
}
SplatRegistrations.SetupIOC();
}
public static MainViewModel Main => Locator.Current.GetService<MainViewModel>()!;
public static void Cleanup()
{
}
}
Is it possible to use SplatRegistrations like this?
It says:
ViewModelLocator.cs(64, 9): [SPLATDI001] TConcrete has more than one constructor and one hasn't been marked with DependencyInjectionConstructorAttribute
public static void RegisterWithDesign<TInterface, TDesign>(this IMutableDependencyResolver resolver)
where TDesign : TInterface, new() => RegisterWithDesign<TInterface, TInterface, TDesign>(resolver);
public static void RegisterWithDesign<TInterface, TConcrete, TDesign>(this IMutableDependencyResolver resolver)
where TDesign : TInterface, new()
{
SplatRegistrations.Register<TInterface, TConcrete>("Init");
resolver.Register<TInterface>(() =>
Design.IsDesignMode ? new TDesign() : Locator.Current.GetService<TInterface>("Init"));
}
If not; it could be a neat feature to add.
public static void RegisterService<TConcrete>()
{
SplatRegistrations.Register<TConcrete>();
}
The above method results in a SPLATDI001 error (TConcrete has more than one constructor and one hasn't been marked with DependencyInjectionConstructorAttribute) that is not related to the type being used. The error appears even if the method is not called.
Avalonia comes with ReactiveUI and ReactiveUI comes with Splat. I'm trying to use those default tools.
What bugs me off is that Splat by default needs to manually create each object. Splat.DI.SourceGenerator is supposed to solve that problem.
Now in my project I have Avalonia.ReactiveUI and Splat.DI.SourceGenerator
SplatRegistrations is not recognized. I find no way to use this.
SplatRegistrations.Register<IMenuUseCase, MenuUseCase>();
SplatRegistrations.Register<IOtherDependency, OtherDependency>();
I spent several hours trying to get this to work. What am I missing?
Besides the troubles to get this setup; what are the benefits of going the Splat route vs some more established DI containers like Microsoft DI or Simple Injector?
My project file contains this
<ItemGroup>
<PackageReference Include="Avalonia" Version="0.10.10" />
<PackageReference Include="Avalonia.Desktop" Version="0.10.10" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="0.10.10" />
<PackageReference Include="Avalonia.ReactiveUI" Version="0.10.10" />
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="0.10.10.4" />
<PackageReference Include="Splat.DependencyInjection.SourceGenerator" Version="1.1.33">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
IEnumerables in constructor injection (and I assume, but have not tested, property injection) are currently not being initialized and left as null. They should be resolved by Locator.Current.GetServices().
Here is a helper method workaround in the meantime.
public static void ResolveIEnumerable<T>(ref IEnumerable<T> target)
{
// https://github.com/reactivemarbles/Splat.DI.SourceGenerator/issues/72
if (target != null)
{
throw new InvalidOperationException("This issue has been resolved. Remove this method.");
}
target = Locator.Current.GetServices<T>();
}
Describe the bug
This behavior happened on a more developed project, but I'm able to reproduce it on a new WPF project.
In a nutshell, I'm unable to build a project when Splat.DependencyInjection.SourceGenerator
is included in the project. When I do, I get the following errors:
Severity Code Description Project File Line Suppression State Priority
Error CS0111 Type 'SplatRegistrations' already defines a member called 'RegisterLazySingleton' with the same parameter types WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 28 Active Normal
Error CS0111 Type 'SplatRegistrations' already defines a member called 'Register' with the same parameter types WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 13 Active Normal
Error CS0111 Type 'SplatRegistrations' already defines a member called 'Register' with the same parameter types WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 21 Active Normal
Error CS0111 Type 'SplatRegistrations' already defines a member called 'RegisterLazySingleton' with the same parameter types WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 36 Active Normal
Error CS0111 Type 'SplatRegistrations' already defines a member called 'RegisterLazySingleton' with the same parameter types WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 44 Active Normal
Error CS0111 Type 'SplatRegistrations' already defines a member called 'RegisterLazySingleton' with the same parameter types WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 53 Active Normal
Error CS0111 Type 'SplatRegistrations' already defines a member called 'Register' with the same parameter types WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 60 Active Normal
Error CS0111 Type 'SplatRegistrations' already defines a member called 'Register' with the same parameter types WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 69 Active Normal
Error CS0111 Type 'SplatRegistrations' already defines a member called 'RegisterLazySingleton' with the same parameter types WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 76 Active Normal
Error CS0111 Type 'SplatRegistrations' already defines a member called 'RegisterLazySingleton' with the same parameter types WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 85 Active Normal
Error CS0111 Type 'SplatRegistrations' already defines a member called 'RegisterConstant' with the same parameter types WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 92 Active Normal
Error CS0111 Type 'SplatRegistrations' already defines a member called 'RegisterConstant' with the same parameter types WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 98 Active Normal
Error CS0111 Type 'SplatRegistrations' already defines a member called 'SetupIOC' with the same parameter types WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 103 Active Normal
Error CS0111 Type 'SplatRegistrations' already defines a member called 'SetupIOC' with the same parameter types WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 112 Active Normal
Error CS0111 Type 'SplatRegistrations' already defines a member called 'SetupIOCInternal' with the same parameter types WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 118 Active Normal
Error CS0756 A partial method may not have multiple defining declarations WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 118 Active Normal
Error CS0579 Duplicate 'System.AttributeUsage' attribute WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 124 Active Normal
Error CS0101 The namespace 'Splat' already contains a definition for 'DependencyInjectionPropertyAttribute' WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 125 Active Normal
Error CS0111 Type 'DependencyInjectionPropertyAttribute' already defines a member called 'DependencyInjectionPropertyAttribute' with the same parameter types WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 130 Active Normal
Error CS0579 Duplicate 'System.AttributeUsage' attribute WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 138 Active Normal
Error CS0101 The namespace 'Splat' already contains a definition for 'DependencyInjectionConstructorAttribute' WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 139 Active Normal
Error CS0111 Type 'DependencyInjectionConstructorAttribute' already defines a member called 'DependencyInjectionConstructorAttribute' with the same parameter types WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.g.cs 144 Active Normal
Error CS0757 A partial method may not have multiple implementing declarations WpfApp1 C:\Source\repos\WpfApp1\WpfApp1\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.Generator\Splat.DI.Reg.g.cs 7 Active Normal
The curious thing is this used to work just fine on version 1.1.33
but now, when I revert, it still doesn't work. I've rolled back to an older commit on my main project that DID work, but it still won't build. This leads me to believe it's something with NET6. I'm on SDK 6.0.400
. I would have likely been on 6.0.300
when I last had a successful build, but I have not yet tested that theory.
Steps To Reproduce
Failing Example Project:
WpfApp1.zip
Provide the steps to reproduce the behavior:
<ItemGroup>
<PackageReference Include="ReactiveUI" Version="18.3.1" />
<PackageReference Include="ReactiveUI.Events.WPF" Version="15.1.1" />
<PackageReference Include="ReactiveUI.Fody" Version="18.3.1" />
<PackageReference Include="ReactiveUI.WPF" Version="18.3.1" />
<PackageReference Include="Splat.DependencyInjection.SourceGenerator" Version="1.1.69">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
Expected behavior
It should build.
Screenshots
Environment
Additional context
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.