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 Auto Inject #7

Open
vipwan opened this issue Mar 26, 2024 · 1 comment
Open

Support Auto Inject #7

vipwan opened this issue Mar 26, 2024 · 1 comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@vipwan
Copy link
Owner

vipwan commented Mar 26, 2024

提供自动注入的支持 .NET7+

    public interface ITestService
    {
        string Say(string message);
    }

    public interface ITest2Service
    {
        string Say2(string message);
    }

    [AutoInject]
    [AutoInject<TestService>]
    [AutoInject<ITestService>(ServiceLifetime.Singleton)]
    [AutoInject<ITest2Service>(ServiceLifetime.Scoped)]
    public class TestService : ITestService, ITest2Service
    {
        public string Say(string message)
        {
            return $"hello {message}";
        }

        public string Say2(string message)
        {
            return message;
        }
    }
vipwan added a commit that referenced this issue Mar 26, 2024
vipwan added a commit that referenced this issue Mar 26, 2024
vipwan added a commit that referenced this issue Mar 27, 2024
vipwan added a commit that referenced this issue Jun 19, 2024
@vipwan
Copy link
Owner Author

vipwan commented Jun 19, 2024

提供 NET8 keyed Service的支持

//NET8.0+ 支持keyed
[AutoInjectKeyed<ITest2Service>("test2", ServiceLifetime.Transient)]
[AutoInjectKeyed<ITest2Service>(nameof(TestService2))]
public class TestService2 : ITest2Service
{
    public string Say2(string message)
    {
        return message;
    }
}

将会创建类似的代码:

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddAutoInject(this Microsoft.Extensions.DependencyInjection.IServiceCollection services)
{
    services.AddKeyedTransient<Biwen.AutoClassGen.TestConsole.Services.ITest2Service, Biwen.AutoClassGen.TestConsole.Services.TestService2>("test2");
    services.AddKeyedScoped<Biwen.AutoClassGen.TestConsole.Services.ITest2Service, Biwen.AutoClassGen.TestConsole.Services.TestService2>("TestService2");
    return services;
}

@vipwan vipwan added documentation Improvements or additions to documentation enhancement New feature or request labels Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant