JonPSmith / NetCore.AutoRegisterDi

Extension method to find/register classes in an assembly into the Microsoft DI provider
https://www.thereformedprogrammer.net/asp-net-core-fast-and-automatic-dependency-injection-setup/
MIT License
238 stars 35 forks source link

Service registration #2

Open ZFi88 opened 4 years ago

ZFi88 commented 4 years ago

Hello! What do you think about using some custom attributes for marking classes, which needs to be registred in container, maybe [Service]? Also we can using attributes for marking classes life time scope([Transient], [Scoped], [Singleton])). By that change we can register dependencies in one call... I can create pull request for you.

JonPSmith commented 4 years ago

Hi @ZFi88,

There are some good ideas in there, and thanks for asking before you did a pull request. Many people just do a pull and I have to sort it out afterwards, which is a pain

I like what you propose, but suggestion the following changes:

  1. [Service] isn't a good idea as its a breaking change, i.e. I would have to add [Service] on all of my services! How about the opposite [DoNotAutoRegister]. I would find that useful.
  2. I like your idea of scope [Transient], [Scoped], [Singleton] attributes, but I worry about name clashes and intellisence. How about [RegisterAsTransient], [RegisterAsScoped], [RegisterAsSingleton]

Please add unit tests and comments on your attributes. I will check it out and release it and update the documentation. I am busy so there may be a delay.

ZFi88 commented 4 years ago

About your changes:

  1. Can we stay on current implementation and add additional method for scan services by attribute [Service]? So that will not breaking anything for current projects, and for new projects it will be very comfortably.
  2. Yes your variant is better.
JonPSmith commented 4 years ago

On item 1, my experience of using AutoRegisterDi is that you want it to register everything, but there may be one or two you don't want to register. Therefore a [DoNotAutoRegister] is much more useful than a [Service] attribute.

AutoRegisterDi is about simple registering with DI so the attributes should be for the unusual situations, not the normal situations.

ZFi88 commented 4 years ago

ok, i will try create merge request in the coming days

Alan-Burgess commented 2 months ago

I would also like to suggest the addition of automatic 'Keyed' registration and appropriate attributes.

JonPSmith commented 2 months ago

Hi @Alan-Burgess,

You might not know, but I have dementia - see this section, which means I rarely change any of my libraries that. Sorry to say no.

Alan-Burgess commented 2 months ago

Very sorry to hear that. 😞