Ninject.对内部set-properties的奇怪拦截 [英] Ninject. Strange intercept to inner set-properties

查看:86
本文介绍了Ninject.对内部set-properties的奇怪拦截的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

域对象:

TargetObject.cs

    public class TargetObject
    {
        public virtual ChildTargetObject ChildTargetObject
        {
            get { return ChildTargetObjectInner; }
            set { ChildTargetObjectInner = value; }
        }

        public virtual ChildTargetObject ChildTargetObjectInner { get; set; }
    }

配置和测试:

        var settings = new NinjectSettings
        {
            InjectNonPublic = true,
            AllowNullInjection = true
        };
        var kernel = new StandardKernel(settings);

        kernel.Bind<TargetObject>().ToSelf();

        kernel.InterceptReplaceSet<TargetObject>(t => t.ChildTargetObjectInner,
                (inv) =>
                {
                    inv.Proceed();  // <= we never step here. Why?
                }
            );


        var o = kernel.Get<TargetObject>();

        o.ChildTargetObject = new ChildTargetObject();

在最后一行中,我们更改了属性 ChildTargetObject ,并更改了内部属性 ChildTargetObjectInner .但是我们没有得到它的拦截.为什么?

In the last line we have change property ChildTargetObject and it change inner property ChildTargetObjectInner. But we didnt get interception of it. Why?

如果我删除 ChildTargetObject 附近的虚拟"对象,将可以正常工作(但是这种解决方法是不可能的,因为我使用的是NHiber).

If I remove the "virtual" near ChildTargetObject it will be work fine (but this workaround impossible because I use NHiber).

如果我直接更改 ChildTargetObjectInner (,例如o.ChildTargetObjectInner = new ChildTargetObject(); ),则会被拦截.

If I change ChildTargetObjectInner directly (ex, o.ChildTargetObjectInner = new ChildTargetObject();), I got intercept.

我如何截获任何更改(课堂上和课堂外)?谢谢你.

How can I intercept of any changes (in class and out of class)? Thank you.

推荐答案

这是代理框架的限制,也是ninject创建代理的方式.

This is a limitation of the proxying framework and the way ninject creates a proxy.

如果该方法上有virtual,则该方法将被代理/拦截.但是,当您删除virtual时,该方法将不再被代理/拦截.

If you've got a virtual on the method, the method will be proxied/intercepted. however, when you remove the virtual, the method will not be proxied/intercepted anymore.

现在,调用另一个(代理)方法的代理方法将不会调用该代理方法,而只会调用实现.所以你不能拦截这些.

Now apparently a proxied method making a call to another (proxied) method will not call the proxied method but rather the implementation. So you can't intercept these.

您可能正在使用城堡动态代理. Krzysztof撰写了非常好的教程,并且还介绍了虚拟方法和非虚拟方法

You are probably using castle dynamic proxy. Krzysztof has written a very good tutorial about it, and it also covers virtual and non-virtual methods.

还请注意,由于您正在使用NHibernate,因此NHibernate也将创建代理. 现在,当您创建一个新实体时,您可以通过ninject创建它,它将代理它并配置拦截. 但是,当您从数据库中检索一个持久化的实体时,它将由NHibernate创建.它还将代理它,并在其上放置其拦截器.但是它不知道ninject的代理,因此不会添加这些拦截器.关于这一点,您可能要研究

Also note, that since you are using NHibernate, NHibernate will also create proxy. Now when you create a new entity, you may create it through ninject, which will proxy it and configure the interception. However, when you retrieve a persisted entity from the database, it will be created by NHibernate. It will also proxy it and put its interceptors on it. But it doesn't know about ninject's proxies and thus will not add these interceptors. Regarding this, you may want to look into

  • http://nhibernate.info/blog/2008/12/12/entities-behavior-injection.html
  • http://blog.scooletz.com/2011/02/14/nhibernate-interceptor-magic-tricks-pt-3/

或者,您也可以使用 Fody MethodDecorator

这篇关于Ninject.对内部set-properties的奇怪拦截的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆