如何使用 Autofac 使用方法连接事件? [英] How to wire events with methods using Autofac?

查看:18
本文介绍了如何使用 Autofac 使用方法连接事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过接口/类(通过构造函数和属性注入)将事件连接到 Autofac 的方法而不是整个对象.我想在函数级别而不是类型级别进行绑定.我希望以编程方式完成以下工作(在 C# 中):

Is it possible to wire events to methods with Autofac instead of whole object via interfaces/classes (through constructor and property injection). I want to bind at function level instead of type level. Programmatically I expect the following job to be done (in C#):

someType.Output += someOtherType.Input;

例如 Spring.net 确实支持以下构造来实现:

For example Spring.net does support the following construct to achieve that:

<object id="SomeType" type="Whatever.SomeType, Whatever" />
<object id="SomeOtherType" type="Whatever.SomeOtherType, Whatever">
  <listener event="Output" method="Input">
    <ref object="SomeType" />
  </listener>
</object>

Autofac 是否能够做到这一点,以及如何做到这一点?是否可以将 xml 配置用于此类任务?

Is Autofac able to do that and how? Is it possible to use xml config for such a task?

推荐答案

我假设你的对象没有直接的依赖关系,比如:

I assume that you objects have no direct dependency together, like :

    public class SomeType
{
    public event EventHandler Input;

    public void Raise()
    {
        if (Input != null)
        {
            Input(this, new EventArgs());
        }
    }
}

public class SomeOtherType
{      
    public void Output(object source, EventArgs handler)
    {
        Console.WriteLine("Handled");
    }
}

您可以使用 Activated 或绑定委托:

You can either use Activated or bind a delegate:

已激活:

        ContainerBuilder cb = new ContainerBuilder();

        cb.RegisterType<SomeOtherType>();
        cb.RegisterType<SomeType>()
            .OnActivated(act => 
            { 
                var other = act.Context.Resolve<SomeOtherType>(); 
                act.Instance.Input += other.Output; 
            });
        var container = cb.Build();

        var obj2 = container.Resolve<SomeType>();
        obj2.Raise();

委托版本,将注册替换为:

Delegate version, replace registration by:

        cb.Register(ctx =>
        {
            var other = ctx.Resolve<SomeOtherType>();
            var obj = new SomeType();
            obj.Input += other.Output;
            return obj;
        }).As<SomeType>();

附带说明一下,进行这种类型的绑定有时会有点危险(因为您创建了事件依赖项)并造成内存泄漏.

As a side note, doing this type of binding can sometimes be a bit dangerous (as you create an event dependency) and create memory leak.

创建一个小类来附加两个元素并实现 IDisposable 以在不再需要时取消注册事件可能是一个明智的选择.

Creating a small class that attach both elements and implement IDisposable to unregister event when not needed anymore could be a sensible option.

我认为不可能通过 xml 配置连接事件,对于这种类型的绑定,我更喜欢代码提供的编译时安全性,但也许您有 xml 的用例.

I don't think it is possible to wire events via xml configuration, and for this type of binding I would largely prefer the compile time safety offered by code, but maybe you have a use case for xml.

这篇关于如何使用 Autofac 使用方法连接事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆