-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Func<> resolution #166
base: main
Are you sure you want to change the base?
Conversation
@pakrym Is this complete but just not published? Played around with this branch earlier and |
Did run into one issue when multiple constructors use implicit funcs it will always choose the first one defined even if the service is not registered/optional. eg. using Jab;
using System.Diagnostics;
internal class Program
{
private static void Main(string[] args)
{
using Services services = new();
IProvider<bool> provider = services.GetService<IProvider<bool>>();
}
}
[ServiceProvider]
[Singleton(typeof(IProvider<>), typeof(Provider<>))]
//[Singleton(typeof(IService1<>), typeof(Service1<>))]
[Singleton(typeof(IService2<>), typeof(Service2<>))]
internal sealed partial class Services;
public interface IService1<T>;
public class Service1<T> : IService1<T>;
public interface IService2<T>;
public class Service2<T> : IService2<T>;
public interface IProvider<T>;
public class Provider<T> : IProvider<T>
{
//public Provider(IService1<T> factory) => Debugger.Break();
public Provider(Func<IService1<T>> factory) => Debugger.Break(); // Always hit even when not the best match
//public Provider(Func<IService1<T>>? factory = null) => Debugger.Break(); // Same as above. Always hit.
//public Provider(IService2<T> factory) => Debugger.Break();
public Provider(Func<IService2<T>> factory) => Debugger.Break(); // Never hit unless moved above other constructors
} |
@MitchRazga this should work reasonably enough but I didn't have time for thorough testing. I wonder is the constructor selection issue you see if related to funcs or a general bug. |
@eXpl0it3r / @MitchRazga / @sensslen any interest in push this over the finish line? :) |
Other than the multiple constructor issue mentioned above, it seemed pretty good as is. I tried step-through debugging the source generator but still didn't have enough understanding to be able to fix it. |
Looks like in jab/src/Jab/ServiceProviderBuilder.cs Lines 801 to 807 in 587703b
jab/src/Jab/ServiceProviderBuilder.cs Lines 830 to 831 in 587703b
Might need to something here to first extract the service type from the if (genericType != null &&
SymbolEqualityComparer.Default.Equals(genericType.ConstructedFrom, _knownTypes.FuncType) &&
genericType.TypeArguments[0] is INamedTypeSymbol funcServiceType)
{
serviceType = funcServiceType;
} Did a quick test offline and worked as expected. Tests were still passing too. |
No description provided.