2

How can I make unity to emit decorator which implements all interfaces of decorated dependency?

Unity allows to create build aware dependencies. This is done by implementing IBuildAware interface and using BuilderAwareExtension which casts to IBuildAware in its internals.

The problem is that when using interception decorator does not implement any interface besides the registered one.

public interface IFoo
{
    void Bar();
}

public class Foo : IFoo, IDisposable
{
    public void Bar()
    {
        throw new Exception();
    }

    public void Dispose()
    {
    }
}


container.RegisterType<IFoo, Foo>(
    new InterceptionBehavior<PolicyInjectionBehavior>(),
    new Interceptor<InterfaceInterceptor>());

As the result Unity calls neither Dispose nor OnBuiltUp/OnTearingDown methods.

Update:
This interceptor also does not mirror public properties/methods with applied to them attributes. This can break custom extensions which use reflection.
TransparentProxyInterceptor does not have these issues, but it considerably affects performance.

Update 2: Related question about TransparenProxyInterceptor.

How can I make unity to emit decorator which implements all interfaces and completely mirrors public members of decorated dependency?

Community
  • 1
  • 1
Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137

0 Answers0