Access or get Autofac Container inside a static class

前端 未结 2 1376
北荒
北荒 2021-02-09 17:36

I need to get or access to my IoC container in a static class. This is my (simplified) scenario:

I register dependencies for ASP .net Web Api in a Startup class (but als

2条回答
  •  一个人的身影
    2021-02-09 18:08

    I need to get or access to my IoC container in a static class. Any idea how can I get this?

    Yes, you don't! Seriously. The pattern with the static DomainEvents class originates from Udi Dahan, but even Udi has admitted that this was a bad design. Static classes that require dependencies of their own are extremely painful to work with. They make the system hard to test and maintain.

    Instead, create a IDomainEvents abstraction and inject an implementation of that abstraction into classes that require publishing events. This completely solves the your problem.

    You can define your DomainEvents class as follows:

    public interface IDomainEvents
    {
        void Raise(T args) where T : IDomainEvent;
    }
    
    // NOTE: DomainEvents depends on Autofac and should therefore be placed INSIDE
    // your Composition Root.
    private class AutofacDomainEvents : IDomainEvents
    {
        private readonly IComponentContext context;
        public AutofacDomainEvents(IComponentContext context) {
            if (context == null) throw new ArgumentNullException("context");
            this.context = context;
        }
    
        public void Raise(T args) where T : IDomainEvent {
            var handlers = this.context.Resolve>>();
            foreach (var handler in handlers) {
                handler.Handle(args);
            }
        }
    }
    

    And you can register this class as follows:

    IContainer container = null;
    
    var builder = new ContainerBuilder();
    
    builder.RegisterType().As()
        .InstancePerLifetimeScope();
    
    // Other registrations here
    
    container = builder.Build();
    

提交回复
热议问题