Giter Club home page Giter Club logo

Comments (8)

YairHalberstadt avatar YairHalberstadt commented on May 28, 2024 1

That's an interesting idea - I will definitely have to think about it!

One issue is that SourceGenerators can't call into other SourceGenerators, so this will have to be a core part of StrongInject, rather than an Asp.Net specific plugin. I'm not sure how comfortable I am with "polluting" core StrongInject with Asp.Net specific logic.

I think there's definitely room to explore using the full power of SourceGenerators to improve the Asp.Net integration, but I want to consider it a bit more to make sure I get it right. Maybe it might make for a 2.0 release?

from stronginject.

YairHalberstadt avatar YairHalberstadt commented on May 28, 2024 1

That's definitely a more compelling design. Some way of naming factories and using those specifically for resolution.

from stronginject.

YairHalberstadt avatar YairHalberstadt commented on May 28, 2024 1

On issue is typeof(ILogger<>) can't actually be used in attributes. If dotnet/roslyn#26337 ever lands, we should be able to do something here.

from stronginject.

slang25 avatar slang25 commented on May 28, 2024 1

Yeah, that works and is really clean!
I think in this example the ILogger<T> would just be ILogger<>, I've started a trend of copy-pasting redundant Ts apparently 😄.

I think on the generic attribute language feature thing, I'm hoping that lands in C# 10, then you can add versions of Register and family that take type args in stead of the good old Type parameters as is done today (of course keeping them for compat and choice), it will make everything look neater. Thankfully it shouldn't require any forward thinking from an API design perspective, as the type argument would distinguish them (if that makes sense), so we don't need to think about overload ambiguity etc...

from stronginject.

YairHalberstadt avatar YairHalberstadt commented on May 28, 2024 1

I've now implemented this and merged this in. You should be able to try it out in the latest preview nuget package. In a few days I'll hopefully cut a release.

from stronginject.

slang25 avatar slang25 commented on May 28, 2024

Thanks for the quick response, yeah those are fair concerns.

To be a bit clearer, I'm not looking to erase the service provider code, but eliminate the factory methods, so you can declare some of the deps at the top with normal registered types. I see the problem being you might have 10-20 dependencies still coupled to ServiceCollection after initial adoption.

So something like this would also keep me happy:

[Register(typeof(WeatherForecastController), Scope.InstancePerResolution)]
[Register(typeof(WeatherForecastProvider), Scope.InstancePerDependency, typeof(IWeatherForecastProvider))]
[Register(typeof(WeatherSummarizer), Scope.SingleInstance, typeof(IWeatherSummarizer))]
[Register(typeof(UsersController), Scope.InstancePerResolution)]
[Register(typeof(DatabaseUsersCache), Scope.SingleInstance, typeof(IUsersCache))]
[Register(typeof(MockDatabase), Scope.SingleInstance, typeof(IDatabase))]
[RegisterDecorator(typeof(DatabaseDecorator), typeof(IDatabase))]
[RegisterWithNamedFactory(typeof(ILogger<>), "serviceprovider")]
public partial class Container : IContainer<WeatherForecastController>, IContainer<UsersController>
{
    private readonly IServiceProvider _serviceProvider;

    public Container(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    [Factory(Name = "serviceprovider")] private T GetService<T>() => _serviceProvider.GetRequiredService<T>();
}

Again, the names and types aren't important, just trying to convey the concept.

from stronginject.

dadhi avatar dadhi commented on May 28, 2024

On issue is typeof(ILogger<>) can't actually be used in attributes

You may consider to use ILogger<object> for this case - just like saying that the logger is for any object.

from stronginject.

YairHalberstadt avatar YairHalberstadt commented on May 28, 2024

One issue is typeof(ILogger<>) can't actually be used in attributes. If dotnet/roslyn#26337 ever lands, we should be able to do something here.

Turns out you can. I was getting confused with that you cant do this:

public class C<T> {
    [MyAttribute(typeof(T))]
    class D {}
}

So I think I decent design here is as follows:

Any generic method can be marked with any number of [FactoryOf(Type type, Scope scope = Scope.InstancePerResolution)] attributes instead of the [Factory] attribute.

If so it acts exactly like any other generic Factory, except it's only used to resolve types which are in FactoryOf. If the type is open generic, then we can resolve any type which is created by parameterizing the types in FactoryOf.

So your example would be:

[Register(typeof(WeatherForecastController), Scope.InstancePerResolution)]
[Register(typeof(WeatherForecastProvider), Scope.InstancePerDependency, typeof(IWeatherForecastProvider))]
[Register(typeof(WeatherSummarizer), Scope.SingleInstance, typeof(IWeatherSummarizer))]
[Register(typeof(UsersController), Scope.InstancePerResolution)]
[Register(typeof(DatabaseUsersCache), Scope.SingleInstance, typeof(IUsersCache))]
[Register(typeof(MockDatabase), Scope.SingleInstance, typeof(IDatabase))]
[RegisterDecorator(typeof(DatabaseDecorator), typeof(IDatabase))]
public partial class Container : IContainer<WeatherForecastController>, IContainer<UsersController>
{
    private readonly IServiceProvider _serviceProvider;

    public Container(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    [FactoryOf(typeof(ILogger<T>)] private T GetService<T>() => _serviceProvider.GetRequiredService<T>();
}

What do you think?

from stronginject.

Related Issues (20)

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.