Intercept creation of instances in Ninject

▼魔方 西西 提交于 2019-12-11 10:16:20

问题


I am looking to intercept the creation of instances that implement a certain interface, or have a certain attribute. I am able to do something similiar with the interception extension, but that only seems to do method and property interception.

Here is how I can intercept method and property calls, but it doesn't intercept the constructor call:

_kernel.Bind<IInterceptor>().To<LogInterceptor>().InSingletonScope();
_kernel.Intercept(x =>
{
    if (x.Plan.Type.GetInterface(typeof(ITriggerLoggingInterception).FullName) != null)
    {
        return true;
    }

    return false;
}).With<LogInterceptor>();

回答1:


As you found out for yourself, what comes closest to doing something on instanciation for every binding - without requiring the binding to be altered - is an IActivationStrategy.

for example (example taken from here:

public class StartableStrategy : ActivationStrategy
{
  public override void Activate(IContext context, InstanceReference reference)
  {
    reference.IfInstanceIs<IStartable>(x => x.Start());
  }

  public override void Deactivate(IContext context, InstanceReference reference)
  {
    reference.IfInstanceIs<IStartable>(x => x.Stop());
  }
}

which is added to the ninject kernel the following way:

kernel.Components.Add<IActivationStrategy, StartableActivationStrategy>();

Alternative - binding syntax sugar

let me go into more detail about the OnActivation() extension i mentioned in the comments:

    public static IBindingOnSyntax<T> RegisterEvents<T>(this IBindingOnSyntax<T> binding)
    {
        // todo check whether <T> implements the IHandle<> interface, if not throw exception
        return binding
            .OnActivation((ctx, instance) => ctx.Kernel.Get<EventAggregator>().Subscribe(instance));
    }

this you would employ manually to the binding:

kernel.Bind<FooViewModel>().ToSelf()
      .RegisterEvents()
      .InSingletonScope();

(the InSingletonScope() isn't needed - it's just to show that you can use the other binding extensions/features as before).

Now i think you want to employ this rather "by convention". If you create your bindings by convention (ninject.extensions.conventions), you can use an IBindingGenerator to create the binding accordingly (with or without the call to RegisterEvents). If not, it get's trickier. I'd say you'd have to extend ninject's pipeline.



来源:https://stackoverflow.com/questions/27949636/intercept-creation-of-instances-in-ninject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!