【问题标题】:Instance per matching lifetime scope, with default?每个匹配生命周期范围的实例,默认?
【发布时间】:2013-02-03 12:38:05
【问题描述】:

我希望在 Autofac 中为每个匹配的生命周期范围注册一个实例,但有时需要从全局容器请求一个实例(其中没有匹配的生命周期范围)。在不存在匹配生命周期范围的情况下,我想给出一个顶级实例而不是抛出异常。

这可能吗?

【问题讨论】:

  • 为什么需要这个?您是否希望全局注册与范围注册不同?或者您是否希望消费者在一种情况下获取一个实例,而在其他情况下获取其他实例,而全局范围和标记范围的配置保持相同?
  • @PavelGatilov 后者。相同的配置,每个标记范围的不同实例和全局范围的不同实例。
  • 查看stackoverflow.com/a/55394197/545863 以获得巧妙的实现。

标签: c# dependency-injection autofac lifetime-scoping


【解决方案1】:

我认为您最好通过引入新的生命周期选项来扩展 Autofac。我拿了 Autofac 的源代码并做了一些修改:

public static class RegistrationBuilderExtensions
{
    public static IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> InstancePerMatchingOrRootLifetimeScope<TLimit, TActivatorData, TRegistrationStyle>(this IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> builder, params object[] lifetimeScopeTag)
    {
        if (lifetimeScopeTag == null) throw new ArgumentNullException("lifetimeScopeTag");
        builder.RegistrationData.Sharing = InstanceSharing.Shared;
        builder.RegistrationData.Lifetime = new MatchingScopeOrRootLifetime(lifetimeScopeTag);
        return builder;
    }
}

public class MatchingScopeOrRootLifetime: IComponentLifetime
{
    readonly object[] _tagsToMatch;

    public MatchingScopeOrRootLifetime(params object[] lifetimeScopeTagsToMatch)
    {
        if (lifetimeScopeTagsToMatch == null) throw new ArgumentNullException("lifetimeScopeTagsToMatch");

        _tagsToMatch = lifetimeScopeTagsToMatch;
    }

    public ISharingLifetimeScope FindScope(ISharingLifetimeScope mostNestedVisibleScope)
    {
        if (mostNestedVisibleScope == null) throw new ArgumentNullException("mostNestedVisibleScope");

        var next = mostNestedVisibleScope;
        while (next != null)
        {
            if (_tagsToMatch.Contains(next.Tag))
                return next;

            next = next.ParentLifetimeScope;
        }

        return mostNestedVisibleScope.RootLifetimeScope;
    }
}

只需将这些类添加到您的项目中并将您的组件注册为:

builder.RegisterType<A>.InstancePerMatchingOrRootLifetimeScope("TAG");

我自己没试过,但是应该可以的。

【讨论】:

    【解决方案2】:

    可能的解决方案是在子生命周期范围内覆盖注册。

    示例:

    public enum Scopes
    {
        TestScope
    }
    
    public class Test
    {
       public string Description { get; set; }
    }
    
    public class Tester
    {
        public void DoTest()
        {
            ContainerBuilder builder = new ContainerBuilder();
            builder.RegisterType<Test>()
                .OnActivating(args => args.Instance.Description = "FromRoot")
                .SingleInstance();
            var container = builder.Build();
    
            var scope = container.BeginLifetimeScope(Scopes.TestScope, b => b
                .RegisterType<Test>()
                .InstancePerMatchingLifetimeScope(Scopes.TestScope)
                .OnActivating(args => args.Instance.Description = "FromScope"));
    
            var test1 = container.Resolve<Test>();
            Console.WriteLine(test1.Description); //writes FromRoot
    
            var test2 = scope.Resolve<Test>();
            Console.WriteLine(test2.Description); //writes FromScope
    
            Console.ReadLine();
        }
    }
    

    【讨论】:

    • 虽然代码有点混乱,但@Memoizer 是对的——唯一的方法是覆盖子范围内的注册。不幸的是,问题是“我希望在一个地方有一个终身范围样式,在另一个地方有不同的样式”,这不是一个受支持的场景,所以它需要一些这样的自定义代码才能实现。
    【解决方案3】:

    根容器本身是一个生命周期,标签名称为:root,定义在LifetimeScope.RootTag

    所以你可以这样做:

    using Autofac.Core.Lifetime;
    
    builder.RegisterType<Service>().As<IService>()
        .InstancePerMatchingLifetimeScope(LifetimeScope.RootTag, "foobar");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多