jamesmh / coravel

Near-zero config .NET library that makes advanced application features like Task Scheduling, Caching, Queuing, Event Broadcasting, and more a breeze!
https://docs.coravel.net/Installation/
MIT License
3.8k stars 248 forks source link

Feat: Auto-Register and Auto-Subscribe All Event Listeners in Assembly #148

Open ryanelian opened 4 years ago

ryanelian commented 4 years ago

Describe the solution you'd like

Continuing discussion from https://github.com/jamesmh/coravel/issues/142

That being said, there are ways to auto register all event listener classes which could make things easier (but that would be a new feature request 😉)

Inspired by your insight, I decided to propose another API for auto-configuring event listeners:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddEventsFromAssembly<Program>();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
             app.UseCoravelEvents();
        }

When AddEventsFromAssembly is invoked, the method will scan the Assembly of that class for all IEvent and all IListener<> classes. Then those classes will be auto-registered into the Dependency Injection.

In addition, Coravel should automatically perform Register and Subscribe for all corresponding events and listeners based on the IListener<> generic type parameter.

Describe alternatives you've considered

Feature not yet available in Coravel.

Blinke commented 4 years ago

I should have a PR ready for this soon. The functionality is done but there's some code cleanup and test writing left to do.