[Feature] CommunityToolkit.Extensions.DependencyInjection package #463
Replies: 9 comments 4 replies
-
What if we need to supply our own values? |
Beta Was this translation helpful? Give feedback.
-
Then if you need inject these services inside ViewModels will be the same classic way (App.services.GetRequiredService<>()) OR it will be automatically using some "Attributes" like this: [SomeAttribute]
public partial class ViewModel
{
[CustomAttribute]
private ISomeService1;
[CustomAttribute]
private ISomeService2;
} |
Beta Was this translation helpful? Give feedback.
-
Wouldn't this make more sense in the MVVM toolkit instead of the windows toolkit, so it can be used not only with WinUI? |
Beta Was this translation helpful? Give feedback.
-
I would be very interested in this experiment. Sergio and the team have an excellent track record for delivering high-quality and performant source generators. However, I am curious to know how this would distinguish itself or offer any advantages compared to other popular solutions, such as Jab and StrongInject |
Beta Was this translation helpful? Give feedback.
-
This experiment is now available in Labs, here. |
Beta Was this translation helpful? Give feedback.
-
@Sergio0694 I installed version 0.1.230830 and it seems that the generator would create code that passes in the Note that this bug doesn't reproduce if the Minimal sample to trigger this bugusing CommunityToolkit.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
namespace TKDIBugRepo;
static partial class Program
{
static void Main()
{
}
[Singleton(typeof(Foo))]
[Singleton(typeof(Test))]
static partial void ConfigureServices(IServiceCollection collection);
}
public class Foo
{ }
public class Test
{
private readonly Foo _foo;
public Test(Foo foo)
=> _foo = foo;
} Generated code// <auto-generated/>
#pragma warning disable
#nullable enable
namespace TKDIBugRepo
{
/// <inheritdoc/>
partial class Program
{
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Extensions.DependencyInjection.SourceGenerators.ServiceProviderGenerator", "1.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static partial void ConfigureServices(global::Microsoft.Extensions.DependencyInjection.IServiceCollection collection)
{
global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton(collection, typeof(global::TKDIBugRepo.Foo), new global::TKDIBugRepo.Foo());
global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton(collection, typeof(global::TKDIBugRepo.Test), static services => new global::TKDIBugRepo.Test(
global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService<global::TKDIBugRepo.Foo>(collection)));
}
}
} Suggestion for fixReplace |
Beta Was this translation helpful? Give feedback.
-
Hey @Sergio0694, the new package is not in the official feed yet, right? Any ETA for when (or if) this will happen? |
Beta Was this translation helpful? Give feedback.
-
Requesting support for registering more than one implementation of an interface, something like public class Something(IEnumerable<IService> service) { }
[SingletonGroup(typeof(Impl1), typeof(IService))]
[SingletonGroup(typeof(Impl2), typeof(IService))]
[Singleton(typeof(Something))]
internal static partial void ConfigureServices(IServiceCollection services); |
Beta Was this translation helpful? Give feedback.
-
Requesting support for Keyed Services: public class SomethingA([FromKeyedServices("key1")]IService service) { }
public class SomethingB(IEnumerable<IService> service) { }
[Singleton("key1", typeof(Impl1), typeof(IService))]
[Singleton("key2", typeof(Impl2), typeof(IService))]
[Singleton(typeof(SomethingA))]
[Singleton(typeof(SomethingB))]
internal static partial void ConfigureServices(IServiceCollection services); Asking for the services as enumerable without a key should use |
Beta Was this translation helpful? Give feedback.
-
We built a source generator (with analyzers) to help with dependency injection registration in the Microsoft Store, and we would like to open source this work into the Windows Community Toolkit Labs for Windows. This small library has the following advantages:
This is how you can use the library:
This will generate an implementation of that
ConfigureServices
method with registration calls for all declared services, and fully pre-generated reflection-free stubs to instantiate every service as well. That is, this will work just fine with AOT/trimming as well.API overview
The following attributes are included:
We could of course also add
[Scoped]
if necessary.Beta Was this translation helpful? Give feedback.
All reactions