Comments (8)
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.
That's definitely a more compelling design. Some way of naming factories and using those specifically for resolution.
from stronginject.
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.
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 T
s 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.
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.
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.
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.
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)
- Optionally generated service location API HOT 2
- Inject Func<Owned<T>> parameters HOT 1
- [Feature/Idea] Support for primary constructor generator HOT 2
- Innermost hasAwaitStarted catch block is a no-op and could be omitted HOT 3
- Update xamarin sample to maui
- 'registerAs' parameter for FactoryAttribute HOT 9
- IOwned<out T> HOT 4
- Convenience methods for creating Owned<T> instances HOT 3
- Permitting null instead of empty delegate for Owned<T> and AsyncOwned<T> HOT 2
- Add diagnostic if any registered method return types are by ref.
- Circular dependency check is too restrictive for delegates HOT 23
- SI1103 warning location forces suppression to be all or nothing HOT 6
- Separate Microsoft DI from Asp.Net Core HOT 2
- Convert to IIncrementalGenerator HOT 18
- Retrieving design-time info about registrations HOT 5
- StrongInject Source Generator API HOT 1
- Confusing use of the term 'delegate parameters' in the readme HOT 2
- Support decorator factories that aren't gratuitously generic HOT 4
- Misleading info in README HOT 2
- Generic interface registration HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stronginject.