Ninject. Strange intercept to inner set-properties

前端 未结 1 1420
借酒劲吻你
借酒劲吻你 2021-01-29 01:41

Domain object:

TargetObject.cs

    public class TargetObject
    {
        public virtual ChildTargetObject ChildTargetObject
        {
         


        
相关标签:
1条回答
  • 2021-01-29 02:17

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

    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.

    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.

    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/

    Alternatively you could also decorate your methods using Fody MethodDecorator

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