Can any of existing IoC containers create the lazy proxy classes dynamically?

前端 未结 2 823
轮回少年
轮回少年 2021-01-06 14:29

I study different DI patterns. And now I interested in the lazy life-time implementation. For example, I want to write a proxy class that hides the factory behind a service\

相关标签:
2条回答
  • 2021-01-06 14:49

    Here is how it can be done in Castle:

    http://litemedia.info/lazy-loading-property-with-castle-dynamicproxy2

    0 讨论(0)
  • 2021-01-06 15:04

    Unity does this out of the box. You declare a dependency of type Func<IService> in your constructor (like you did with ServiceFactoryProxy) and tell Unity how it should resolve IService. The container will generate the delegate for you.


    Update

    That proxy looks like an encapsulation of Lazy<T>.

    If that is your intention you might want to have a look at this extension for Unity.

    As far as I know Unity 3.0 will have support for .NET4 so I expect something similar to turn up as part of Unity with the next release.


    Update2

    It took me a while but I finished the lazy proxy feature. It generates IL code that looks like this

    public class MyContract_LazyInstantiationProxy : IMyContract
    {
      private readonly Lazy<IMyContract> instance;
      public MyContract_LazyInstantiationProxy(Func<IMyContract> factory)
      {
        Guard.AssertNotNull(factory, "factory");
        this.instance = new Lazy<IMyContract>(factory);
      }
      public IMyContract Instance
      {
        get { return this.instance.Value; }
      }
      public string Foo(Bar bar)
      {
        return this.Instance.Foo(bar);
      }
    }
    

    So you get the performance of hand-coded lazy proxies but they will be generated dynamically for you.

    Have a look at the TecX project on codeplex. The lazy proxy generation can be found in the source code of TecX.Unity.Proxies. A sample on how to use it is in the according test project TecX.Unity.Proxies.Test.

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