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

AsSelfWithInterfaces = AsImplementedInterfaces+AsSelf right? #202

Open
gitlsl opened this issue Apr 23, 2023 · 1 comment
Open

AsSelfWithInterfaces = AsImplementedInterfaces+AsSelf right? #202

gitlsl opened this issue Apr 23, 2023 · 1 comment

Comments

@gitlsl
Copy link

gitlsl commented Apr 23, 2023

.AddClasses(classes => classes.AssignableTo<ISingletonService>().WithoutAttribute<NotInjectAttribute>())
        .AsImplementedInterfaces(x => x != typeof(ISingletonService))
        .AsSelf()
        .WithSingletonLifetime()    // A

.AddClasses(classes => classes.AssignableTo<ISingletonService>().WithoutAttribute<NotInjectAttribute>())
        .AsSelfWithInterfaces()
        .WithSingletonLifetime() //B

public class TestService2 : ISingletonService
{
    public TestService2()
    {
        Console.WriteLine("ctor2 in TestService ");
    }
}

            Console.WriteLine(provider.GetService<ISingletonService>().GetType().FullName);
            Console.WriteLine(provider.GetService<TestService2>().GetType().FullName);

beacause AsSelfWithInterfaces can not filter interface, so I seprate AsImplementedInterfaces+AsSelf
but code will print 2 times "ctor2 in TestService " in scense A , print 1 time in scense B

@khellang
Copy link
Owner

Hello @gitlsl! 👋🏻

Yeah, AsSelfWithInterfaces is kinda like AsImplementedInterfaces + AsSelf, but there's a small difference, which is why you're seeing two outputs in the first scenario and only one in the second scenario. When using AsSelfWithInterfaces, it will register as itself once and base all the interface registrations on the self-registration. That means that if you're using a singleton lifetime, you will only have a true singleton when using AsSelfWithInterfaces, while AsImplementedInterfaces will give you a singleton per interface.

I've added an overload with a predicate in #209 (see 1a99cfb)

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

No branches or pull requests

2 participants