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

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

问题描述

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

  someType.Output + = someOtherType.Input; 

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

 < object id = SomeType type = Whatever.SomeType,Whatever /> 
< object id = SomeOtherType type = Whatever.SomeOtherType,无论如何>
< listener event = Output method = Input>
< ref object = SomeType />
< / listener>
< / object>

Autofac能够做到吗?如何做到?

解决方案

我假设您的对象在一起没有直接依赖关系,例如:

 公共类SomeType 
{
public event EventHandler Input;

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

公共类SomeOtherType
{
public void输出(对象源,EventArgs处理程序)
{
Console.WriteLine( Handled);
}
}

您可以使用激活或绑定委托: / p>

已激活:

  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();

代理版本,将注册替换为:

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

请注意,进行这种类型的绑定有时会有些危险(当您创建一个



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

>

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


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;

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>

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");
    }
}

You can either use Activated or bind a delegate:

Activated:

        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.

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

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天全站免登陆