容器在废弃时应如何表现?

发布于 2024-12-10 07:40:43 字数 1509 浏览 0 评论 0原文

它应该抛出异常、将解析传递给其父级还是其他什么?

我最喜欢的容器 Autofac 会引发异常 - 这是我唯一不喜欢它的地方。 我认为它应该将决议传递给其父级,这应该通过此代码解决我的问题


    class LazyClass
    {
        public void DoSomething() { }
    }

    class SomeClass
    {
        public event EventHandler WatchOut = (s, ea) => { };
        public void Start()
        {
            WatchOut(this, EventArgs.Empty);
        }
    }

    class LazyInterceptor
    {
        Lazy<LazyClass> lazy;
        public LazyInterceptor(Lazy<LazyClass> lazy)
        {
            this.lazy = lazy;
        }
        public void Register(SomeClass some)
        {
            some.WatchOut += (s, ea) => lazy.Value.DoSomething();
        }
    }

    [TestMethod]
    public void LazyAndEvents()
    {
        var builder = new ContainerBuilder();
        builder.RegisterType<LazyClass>().SingleInstance();
        var container = builder.Build();

        var someClass = new SomeClass();

        using (var inner = container.BeginLifetimeScope(cb => 
            cb.RegisterType().SingleInstance()))
        {
            var interceptor = inner.Resolve();
            interceptor.Register(someClass);
        }

        someClass.Start();
    }

我知道围绕此问题的三种解决方法,但它们似乎都是错误的

  • 不处置容器 - 这就是我目前所做的,但这只是为了让 Autofac 高兴
  • 显式注册惰性 - 似乎是错误的,我什至不知道
  • 将所有权进行处置会解决什么问题 - 它看起来并没有那么糟糕,但我不以这种方式使用对象,所以它再次看起来像是一种解决方法。另外,在 Autofac 中它引入了依赖性,这不太好

Should it throw an exception, pass resolution to its parent or something else?

My favorite container Autofac raises an exception - which is the only thing I don't like about it.
I think that it should pass resolution to its parent which should resolve my issue with this code


    class LazyClass
    {
        public void DoSomething() { }
    }

    class SomeClass
    {
        public event EventHandler WatchOut = (s, ea) => { };
        public void Start()
        {
            WatchOut(this, EventArgs.Empty);
        }
    }

    class LazyInterceptor
    {
        Lazy<LazyClass> lazy;
        public LazyInterceptor(Lazy<LazyClass> lazy)
        {
            this.lazy = lazy;
        }
        public void Register(SomeClass some)
        {
            some.WatchOut += (s, ea) => lazy.Value.DoSomething();
        }
    }

    [TestMethod]
    public void LazyAndEvents()
    {
        var builder = new ContainerBuilder();
        builder.RegisterType<LazyClass>().SingleInstance();
        var container = builder.Build();

        var someClass = new SomeClass();

        using (var inner = container.BeginLifetimeScope(cb => 
            cb.RegisterType().SingleInstance()))
        {
            var interceptor = inner.Resolve();
            interceptor.Register(someClass);
        }

        someClass.Start();
    }

I know of three workarounds around this, but they all seem just wrong

  • not disposing container - this is what I currently do, but that's just to make Autofac happy
  • explicitly registering lazy - seems just wrong and I don't even know what will be resolved
  • taking ownership for dispose - it doesn't seem as bad, but I don't use object in that way, so again it looks like a workaround. Also, in Autofac it introduces dependency which is not nice

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榕城若虚 2024-12-17 07:40:43

生命周期作用域的全部意义在于控制所包含实例的生命周期。在一次性模式的基础上,处置容器是释放该容器所管理的资源的一种干净且行之有效的方法。因此,当生命周期范围被释放时,您不应再依赖于从该生命周期范围解析的实例。

所以我想说你在这里使用 Autofac 的方式绝对是错误的。也许如果您解释了为什么要使用这样的范围,我们就可以找出正确的用法。

The whole point of lifetime scopes is that to control the lifetime of the contained instances. Building on the Disposable pattern, disposing the container is a clean and well-established way of releasing the resources governed by that container. Thus, when a lifetime scope is disposed you should no longer depend on instances resolved from that lifetime scope.

So I would say that you are definitively using Autofac the wrong way here. Perhaps if you explained why you want to use the scope like that, we could figure out the proper usage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文