Autofac如何在运行时注册和解析通用接口?

我在运行时解析通用接口实现时遇到问题。我正在使用一个事件总线,它将根据事件的类型解析事件处理程序。当我尝试在没有通用实现的情况下解决事件处理程序时,一切都会按预期进行。我想实现一个通用接口,这样我就可以有一个处理特定类型事件的基类。


在创建通用实现之前,我遇到过以下情况:


public interface IEvent

    {

        Guid EntityId { get; set; }

    }


public interface IEventHandler<TEvent> where TEvent : IEvent

    {

        Task Handle(TEvent @event);

    }


public class EventBus

    {

        private readonly IComponentContext _context;


        public EventBus(IComponentContext context)

        {

            _context = context;

        }


        public async Task HandleEvent<TEvent>(TEvent @event) where TEvent : IEvent

        {

            var handler = _context.Resolve<IEventHandler<TEvent>>();

            await handler.Handle(@event);

        }

    }

我按如下方式注册事件处理程序:


builder.RegisterAssemblyTypes(ThisAssembly).AsClosedTypesOf(typeof(IEventHandler<>));


实施示例:


public class FooEventHandler :

        IEventHandler<FooArchivedEvent>,

        IEventHandler<FooRestoredEvent>,

        IEventHandler<FooSomethingElseHappenedEvent>

    {

        private readonly IRepository<Foo> _repository;


        public FooEventHandler(IRepository<Foo> repository)

        {

            _repository = repository;

        }


        public async Task Handle(FooArchivedEvent @event)

        {

            var Foo = await _repository.Get(@event.EntityId);

            Foo.Archive();

        }


        public async Task Handle(FooRestoredEvent @event)

        {

            var Foo = await _repository.Get(@event.EntityId);

            Foo.Restore();

        }


        public async Task Handle(FooSomethingElseHappenedEvent @event)

        {

            // do something else with Foo

        }

    }


阿波罗的战车
浏览 69回答 1
1回答

宝慕林4294392

我最终编写了一个方法来检查已注册处理程序的类型的每个接口:private IEventHandler<TEvent> GetHandler<TEvent>(Type type = null) where TEvent : IEvent&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object handler;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type = type ?? typeof(TEvent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (_container.TryResolve(typeof(IEventHandler<>).MakeGenericType(type), out handler))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (IEventHandler<TEvent>)handler;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var t in type.GetInterfaces())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var h = GetHandler<TEvent>(t);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (h != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return h;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP