Giter Site home page Giter Site logo

duskembayev / enhanced.dependencyinjection Goto Github PK

View Code? Open in Web Editor NEW
10.0 3.0 0.0 216 KB

Enhanced.DependencyInjection is a NuGet package for simplified dependency registration in .NET Core DI container. It uses C# attributes and a source generator for explicit registration, making the process more understandable and faster.

License: MIT License

C# 100.00%
dependency-injection dotnet ioc-container modules source-generators

enhanced.dependencyinjection's People

Contributors

dependabot[bot] avatar duskembayev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

enhanced.dependencyinjection's Issues

Impossible to use dependency injection attributes for nested types

If I try to use Enhanced.DependencyInjection like here:

public interface IValidator<T> { }

public static class MyAwesomeAction
{
    public record Request;

    [Scoped<IValidator<Request>>]
    public class RequestValidator : IValidator<Request> { }
}

I get the Error ENHDI01 : Unable to resolve type 'ProblemDemonstration.RequestValidator'

But there is no problem to register this type via IServiceCollection

Code-time type validation for ContainerEntry attribute

It would be very good to verify types in ContainerEntry attribute. Currently, a mistake can be seen only in runtime when ASP.NET resolves dependency with a wrong interface.

For example:

[ContainerEntry(ServiceLifetime.Scoped, typeof(IOtherService))]
public class MyService { }

public interface IOtherService { }

If you try to resolve IOtherService dependency, you will get the exception:

System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: AspNetWebApp.IOtherService Lifetime: Scoped ImplementationType: AspNetWebApp.MyService': Implementation type 'AspNetWebApp.MyService' can't be converted to service type 'AspNetWebApp.IOtherService')
 ---> System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: AspNetWebApp.IOtherService Lifetime: Scoped ImplementationType: AspNetWebApp.MyService': Implementation type 'AspNetWebApp.MyService' can't be converted to service type 'AspNetWebApp.IOtherService'
 ---> System.ArgumentException: Implementation type 'AspNetWebApp.MyService' can't be converted to service type 'AspNetWebApp.IOtherService'
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite..ctor(ResultCache cache, Type serviceType, ConstructorInfo constructorInfo, ServiceCallSite[] parameterCallSites)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(ServiceDescriptor serviceDescriptor, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ValidateService(ServiceDescriptor descriptor)
   --- End of inner exception stack trace ---
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ValidateService(ServiceDescriptor descriptor)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
   --- End of inner exception stack trace ---
   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
   at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build()
   at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()
   at Program.<Main>$(String[] args) in \AspNetPlaygroung\AspNetWebApp\Program.cs:line 13

Feature request. Attribute for registering hosted services.

I think the possibility of registering hosted service via a special attribute will really helpful feature.

[HostedService]
public class MyAwesomeHostedService : IHostedService
{
    public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;
    public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}

[HostedService]
public class MyAwesomeBackgroundService : BackgroundService
{
    protected override Task ExecuteAsync(CancellationToken stoppingToken) => Task.CompletedTask;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.