8

我需要在静态类中获取或访问我的 IoC 容器。这是我的(简化的)场景:

我在 Startup 类中注册了 ASP .net Web Api 的依赖项(但我也为 MVC 或 WCF 执行此操作。我有一个 DependecyResolver 项目,但为简单起见,请考虑以下代码)

// Web Api project - Startup.cs
public void Configuration(IAppBuilder app)
{
    HttpConfiguration config = new HttpConfiguration();

    var builder = new ContainerBuilder();

    // ... Omited for clarity
    builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
        .AsClosedTypesOf(typeof(IHandle<>))
        .AsImplementedInterfaces();

    // ...
    IContainer container = builder.Build();
    config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
    // ...
}

然后,在一个单独的类库中,我有我的静态类(为清楚起见再次简化):

public static class DomainEvents
{
    private static IContainer Container { get; set; }

    static DomainEvents()
    {
        //Container = I need get my Autofac container here
    }

    public static void Register<T>(Action<T> callback) where T : IDomainEvent { /* ... */ }

    public static void ClearCallbacks() { /* ... */  }

    public static void Raise<T>(T args) where T : IDomainEvent
    {
        foreach (var handler in Container.Resolve<IEnumerable<IHandle<T>>>())
        {
            handler.Handle(args);
        }
        // ...
    }
}

知道我怎么能得到这个吗?

4

2 回答 2

9

我需要在静态类中获取或访问我的 IoC 容器。知道我怎么能得到这个吗?

是的,你没有!严重地。静态DomainEvents类的模式源于 Udi Dahan,但 Udi 也承认这是一个糟糕的设计。需要自己依赖的静态类使用起来非常痛苦。它们使系统难以测试和维护。

相反,创建一个IDomainEvents抽象并将该抽象的实现注入到需要发布事件的类中。这完全解决了你的问题。

DomainEvents您可以按如下方式定义您的类:

public interface IDomainEvents
{
    void Raise<T>(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>(T args) where T : IDomainEvent {
        var handlers = this.context.Resolve<IEnumerable<IHandle<T>>>();
        foreach (var handler in handlers) {
            handler.Handle(args);
        }
    }
}

您可以按如下方式注册此类:

IContainer container = null;

var builder = new ContainerBuilder();

builder.RegisterType<AutofacDomainEvents>().As<IDomainEvent>()
    .InstancePerLifetimeScope();

// Other registrations here

container = builder.Build();
于 2015-11-27T10:57:39.110 回答
7

您可以在DomainEvents类中创建一个静态方法来注入容器,如下所示:

public static class DomainEvents
{
    public static void SetContainer(IContainer container)
    {
        Container = container;
    }

    ....
}

然后从您的 ASP.NET 应用程序中调用此方法来注入容器:

DomainEvents.SetContainer(container);

请注意,我正在直接回答您的问题。但是,以下是我看到的一些问题:

  • 当类需要依赖项时,不应使用静态类。在这种情况下,重构以使用非静态类并使用构造函数注入在类中注入您需要的依赖项。
  • 在Composition Root之外使用容器称为 Service Location 并且被认为是一种反模式
  • 类库不应该使用容器,甚至不应该有组合根。引用我引用的 Composition Root 文章:

只有应用程序应该有组合根。库和框架不应该。

于 2015-11-27T10:55:09.743 回答