Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intercepting methods called from within own class? #44

Open
grofit opened this issue Nov 1, 2017 · 2 comments
Open

Intercepting methods called from within own class? #44

grofit opened this issue Nov 1, 2017 · 2 comments

Comments

@grofit
Copy link

grofit commented Nov 1, 2017

I am not sure if this is a bug or not, but I have a class like so in a console app example:

public class SaySomethingAction : IAction
    {
        public virtual void Run()
        {
            Console.WriteLine("Type in what you want to say, then press enter");
            var message = Console.ReadLine();

            Say(message);
        }

        [DelayFor(2)]
        public virtual void Say(string message)
        {
            Console.WriteLine($"Saying {message}");
        }
    }

So my expectations is that when the Run method invokes Say it would trigger the DelayFor attribute (which is an InterceptAttribute), which would in turn call through to the relevant interceptor. However the interceptor associated with that attribute is not fired on Say, however if I were to put the [DelayFor(2)] onto the Run method, that WOULD correctly trigger and proxy the method.

So is this use case supported?

@grofit
Copy link
Author

grofit commented Nov 1, 2017

hmmm thinking about it Say is not part of the interface IAction only Run is... so maybe that is the problem, but that being said, is there a way to achieve this?

@spam1923
Copy link

Would you be able to Inject another custom class that contains your Say function into SaySomethingAction by puting a constructor that accepts that class? For Example:

public class SaySomethingAction : IAction
    {
        private readonly DelayedSayFunction _delayedSayFunction;
        public SaySomethingAction(DelayedSayFunction delayedSayFunction)
        {
            _delayedSayFunction = delayedSayFunction;
        }
        public virtual void Run()
        {
            Console.WriteLine("Type in what you want to say, then press enter");
            var message = Console.ReadLine();

            _delayedSayFunction.Say(message);
        }

        
    }

    public class DelayedSayFunction
    {
        [DelayFor(2)]
        public virtual void Say(string message)
        {
            Console.WriteLine($"Saying {message}");
        }
    }

Of course you would have to set up the Binding Bind().ToSelf();

Honestly never tried this on the IAction, but ninject seems to let you inject to any constructor that comes from the Kernel which the IActions should. Let me know if that ends up working I may have interest in this some day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants