alsami / MediatR.Extensions.Autofac.DependencyInjection

Autofac plug-in for MediatR.
MIT License
55 stars 14 forks source link

Does it support PropertyInjection on handlers? #3

Closed jehof closed 5 years ago

jehof commented 5 years ago

I need to inject optional dependecies into my handlers (notification) through properties. Is this supported by your library

Autofac normally provides .PropertiesAutowired() for this case.

    public class Handler : INotificationHandler<Created>
    {
        private IRepository _repository;

        public Handler (IRepository fleets)
        {
            _fleets = fleets;
        }

        //optional
        public ILogger<Handler > Logger { get; set; }

        public async Task Handle(Created notification, CancellationToken cancellationToken)
        {
            // work
        }
   }
alsami commented 5 years ago

Hi Jens,

as far as I understand this documentation it won't work that way.

I am not calling .PropertiesAutowired() when registering the open-generics.

However you can simply inject ILogger<T> and assign it to your property. ILogger<T> must be registered anyway, otherwise Autofac won't be able to construct it.

If that doesn't suite your requirements, please feel free to open a pull-request.

jehof commented 5 years ago

This sounds right and may not be supported by Autofac. I have added an additional ctor to my class to inject the logger and all things are fine.