r/dotnet • u/Inevitable-Draft-971 • 5d ago
Promotion Injectify v1.0 attribute-based service registration for Microsoft.Extensions.DependencyInjection
Hi r/dotnet,
I've been working on a small .NET library called Injectify. It's now at version 1.0.5 on NuGet, and this is the first time I'm sharing it here.
The idea came from wanting to reduce the repetitive dependency registration code that often accumulates in Program.cs (AddScoped, AddSingleton, AddTransient, etc.).
With Injectify, you can declare service lifetimes directly on classes using attributes like [Scoped], [Singleton], and [Transient], then register everything from an assembly with a single call.
It also includes DrawAggregator, which generates Mermaid dependency graphs from annotated classes, making it easier to visualize constructor dependencies.
The library is lightweight and has no external dependencies.
GitHub: https://github.com/Roma1011/Injectify
NuGet: https://www.nuget.org/packages/Injectify.Auto
I'd love to hear your feedback. Would you consider using attribute-based DI registration in your projects, or do you prefer keeping registrations explicit in Program.cs?
Thanks!
113
u/daltorak 5d ago edited 5d ago
With all due love and affection, and with respect for the time you've put in...... man, this isn't the way.
Jeremy Miller invented this concept in Structuremap like 20 years ago.... and I was one of the first people to use it, so I saw all this unfold in real time. He eventually realized that there is a major flaw with attribute-based registration: It forces your domain to have a dependency on your DI library, which it absolutely should not have.... DI containers are an infrastructure concern, not an application concern. Also, application may want different instantiation behaviours under different conditions, and these become really difficult to express as attributes when simple code is easier to write and comprehend.
That's actually why Microsoft chose to not implement attributes when they introduced their first-party DI container. It's not an oversight that was waiting for someone to come along and fix, it is extremely intentional so that developers wouldn't footgun themselves.
It's the same story with the old DataAnnotations class and related attributes [Range], [Required] and all that. Microsoft introduced that model way back in the day, then realized after a few years that it created more problems than it solved, so their recommendations moved away from annotating properties with validations, and towards external validator functions. Again, its separation of concerns: A plain C# class at the core of your domain model shouldn't need to know how to generate localized error text -- which is an application-level concern, not domain model level.
In more recent times, another practical reason has emerged for why assembly scanning isn't the way: It doesn't work well with Native AOT, because types that aren't referenced anywhere (including attributes) are stripped from the final output. If someone is stringing together their classes using interfaces, and there's no line of code somewhere that connects it to a concrete class, then that concrete classes end up having no references, and poof!
30
u/philip_laureano 5d ago
As someone that was there since .NET 1.0 and when IOC containers were in their infancy: those who do not study history are doomed to repeat it.
24
u/Impressive-Desk2576 5d ago
Oh boy, not another one. You do not understand the purpose of DI.
8
u/Kanegou 5d ago
No. I would not use it. It's not even about preference. Attribute based registration simply doesn't not work if you need more advanced registrations like custom factory methods. Besides, having a dependency on anything di related inside your interfaces and implementations is a code smell in my opinion.
2
u/Discipline_Cautious1 5d ago
Why do you need it on both interface and implementation?
Is it Keyed dependency injection?
11
u/anina_jessika 5d ago
i'd keep registrations explicit in program.cs. attributes on the classes hide where the wiring actually happens
1
u/Responsible-Cold-627 5d ago
Does it use runtime reflection, or source generators? I would check myself, but github got taken over by unicorns again.
1
u/harrison_314 5d ago
As many others have mentioned, using attributes is not the right approach. I would also recommend this page, which explains the design decisions behind Simple Injector - in my view, it is the best DI container from an architectural standpoint. I used it back in the full .NET Framework days.
The article explains why Simple Injector avoids certain practices, saving you from having to explore dead ends yourself.
https://docs.simpleinjector.org/en/latest/decisions.html
2
u/Liberal_Mormon 5d ago
I will mention, I use a library for service registration via attributes... But it uses those attributes for code generation, and then you use the generated code to register your services in a single method per assembly. This gets around a lot of the pitfalls of pure-attribute registration
If you are curious, it is baked into FastEndpoints.
2
u/hevilhuy 5d ago edited 5d ago
There is an exact same library, but not with reflection. Roar.DependecyInjection is a source-generstor with attrbites like ScopedService, TransientService, SingletonService. It's also support gRPC. NuGet: https://www.nuget.org/packages/Roar.DependencyInjection GitHub: https://github.com/Blazor-School/roar-dependency-injection
2
u/One_Web_7940 5d ago
But why ?
1
u/AutoModerator 5d ago
Thanks for your post Inevitable-Draft-971. Please note that we don't allow spam, and we ask that you follow the rules available in the sidebar. We have a lot of commonly asked questions so if this post gets removed, please do a search and see if it's already been asked.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/vORP 5d ago
The Angular side of me says, "Oh, cool!"
The .NET side of me, however, thinks this is a bad idea - there are a lot of good reasons already shared on this thread, but aside from all of those... attributes would be a pain to manage in a large app at scale like this, and I would not want to go hunting for poorly decided scope lifetimes if I needed to make changes