English 中文(简体)
如何使用Autofac2.4.5处理循环引用?
原标题:How to handle circular references with Autofac 2.4.5?

关于循环引用表示使用:

cb.Register<DependsByProp>().OnActivated(ActivatedHandler.InjectUnsetProperties);

但是看起来<strong>ActivatedHandler</strong>在2.4.5中已经不存在了。在源代码中挖掘,我找到了该类的实现,因此我将方法实现放在了OnActivated中。不幸的是,这仍然不起作用。

我在这里整理了一个最小的repo,看起来像Wiki页面上的内容。

class M
{
    public VM VM { get; set; }

    public M()
    {
    }
}

class VM
{
    public VM(M m)
    {
    }
}

[Fact]
void CanResolveCircular()
{
    ContainerBuilder builder = new ContainerBuilder();

    builder.RegisterType<VM>();
    builder.RegisterType<M>().OnActivated(e => e.Context.InjectUnsetProperties(e.Instance));

    using (var container = builder.Build())
    {
        var m = container.Resolve<M>();
        Assert.NotNull(m);
    }
}

在尝试解析时,此代码仍然引发堆栈溢出异常。我错过了什么?让Autofac处理循环依赖关系的正确方法是什么?

最佳回答

循环依赖项状态:

Note, it doesn t make sense to set up this scenario if both classes are registered with Factory scope.

您的MVM注册都是InstancePerDependency(以前称为FactoryScope),因此此语句适用于您的场景。因此,您将陷入创建M和VM实例的无休止循环中。

如果您希望注入的属性VM采用您解析的M同一实例,则应将M生存期更改为InstancePerDependency以外的其他值(例如SingleInstance)。如下所示:

builder.RegisterType<M>().PropertiesAutowired(true).SingleInstance();

注意:我还使用了最近的PropertiesAutowired(true)扩展方法。您可以使用它来代替您的repo中的OnActivated代码

如果您不希望每个应用程序只有一个M实例,您可以设置LifetimeScope并使用InstancePerLifetimeScope

问题回答

对于任何感兴趣的人来说,这里有一种新的方法:

class DependsByProp1
{
    public DependsByProp2 Dependency { get; set; }
}

class DependsByProp2
{
    public DependsByProp1 Dependency { get; set; }
}

// ...

var cb = new ContainerBuilder();
cb.RegisterType<DependsByProp1>()
      .InstancePerLifetimeScope()
      .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);
cb.RegisterType<DependsByProp2>()
      .InstancePerLifetimeScope()
      .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);

属性/属性依赖项





相关问题
How to Inject Open Connections with an IoC

First, my scenario. I have a service, BillingService, has a constructor like this: BillingService(IInstallmentService, IBillingRepository) The InstallmentService has a constructor that looks like ...

Using the Ninject kernel as a Unit of Work object factory

So I m starting to use Ninject for dependency injection and I m wondering what people think of using a kernel as an object factory for Unit of Work type objects like Linq2Sql Datacontexts. I would ...

array dependency injection in spring?

is there a way to use dependency injection to inject all available implementations of a specific interface in spring? This is kind of the same thing as asked here for .NET. Though my aim is to use @...

Unity constructors

I have setup unity in my project and it is working for objects that don t have constructor injection implemented on them. The issue is now I do have an object which requires a custom object as a ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

ASP.NET MVP Injecting Service Dependency

I have an ASP.NET page that implements my view and creates the presenter in the page constuctor. Phil Haack s post providing was used as the starting point, and I ll just the examples from the post ...

Authorization and Windsor

I m trying to implement my custom authorize attribute like: public class MyCustomAuth : AuthorizeAttribute { private readonly IUserService _userService; public MyCustomAuth(IUserService ...