How to wire events with methods using Autofac?

前端 未结 1 1165
臣服心动
臣服心动 2020-12-18 02:09

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

相关标签:
1条回答
  • 2020-12-18 02:36

    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.

    0 讨论(0)
提交回复
热议问题