Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Support Func<> resolution #166

wants to merge 3 commits into from

Conversation

pakrym
Copy link
Owner

@pakrym pakrym commented Dec 30, 2023

No description provided.

@pakrym pakrym changed the title Support Func<> resoultion Support Func<> resolution Dec 30, 2023
@MitchRazga
Copy link

@pakrym Is this complete but just not published?

Played around with this branch earlier and Func<> resolution seemed to work as expected. Tests were passing too.

@MitchRazga
Copy link

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants