Intercepting method calls with an IInterceptorSelector

95 views
Skip to first unread message

gusgorman

unread,
Aug 8, 2018, 9:54:47 AM8/8/18
to Castle Project Users
Hello,

I want to mark methods for interception using an attribute.

The attribute will have a few properties which will be then used to configure the interceptor (by setting the corresponding properties on it).

The code in SelectInterceptors would check if the method has the attribute, if so get it, and then configure the appropriate interceptor and return it.

If an interceptor is added to the container and marked as transient, will a new instance be created and passed to SelectInterceptors every time a method is called?

I want to make sure this approach is safe in a multi user web environment where the services themselves (ie the candidates for interception) are stored in the container as singletons.

Or is there a better approach?

many thanks,
Jordan.

Krzysztof Koźmic

unread,
Aug 8, 2018, 11:16:53 AM8/8/18
to castle-pro...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-u...@googlegroups.com.
To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at https://groups.google.com/group/castle-project-users.
For more options, visit https://groups.google.com/d/optout.
--

sent from my phone
Krzysztof

Jordan

unread,
Aug 8, 2018, 11:30:12 AM8/8/18
to Castle Project Users
On Wednesday, August 8, 2018 at 4:16:53 PM UTC+1, Krzysztof Koźmic wrote:

Thanks for the reply! However, after one quick read I don't understand!

By "Yes. there is" do you mean yes there is a better way to do this? I'm a bit confused because your example doesn't configure the interceptor for each call based on the properties of an attribute (or based on anything else).

Really really sorry if I've missed something obvious.

Jordan


 

Krzysztof Koźmic

unread,
Aug 8, 2018, 2:42:07 PM8/8/18
to castle-pro...@googlegroups.com
Hi,

No need to apologise. Having re-read your question I think I misunderstood what you wanted to do. Can you elaborate on how you want to configure the interceptors with attributes? You mention a container, does that mean your question is in the context of Windsor as opposed to using just plain DynamicProxy?

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-u...@googlegroups.com.
To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at https://groups.google.com/group/castle-project-users.
For more options, visit https://groups.google.com/d/optout.

Jordan

unread,
Aug 9, 2018, 4:11:06 AM8/9/18
to Castle Project Users
Hello,

Yes this is the context of Windsor as opposed to using just plain DynamicProxy. I am working on a MVC Web app with NHibernate 5.1.3.

In the absence of an up to date NHibernate Facility I have created a very minimal OpenSessionInView implementation and a Transaction attribute/interceptor.

I want the Transaction attribute to have a couple of properties, eg System.Data.IsolationLevel and maybe a ReadOnly boolean which will be set on the interceptor so it knows how to configure the Transaction.

many thanks,
Jordan




On Wednesday, August 8, 2018 at 7:42:07 PM UTC+1, Krzysztof Koźmic wrote:
Hi,

No need to apologise. Having re-read your question I think I misunderstood what you wanted to do. Can you elaborate on how you want to configure the interceptors with attributes? You mention a container, does that mean your question is in the context of Windsor as opposed to using just plain DynamicProxy?

On Wed, 8 Aug 2018 at 17:30 'Jordan' via Castle Project Users <castle-pro...@googlegroups.com> wrote:
On Wednesday, August 8, 2018 at 4:16:53 PM UTC+1, Krzysztof Koźmic wrote:

Thanks for the reply! However, after one quick read I don't understand!

By "Yes. there is" do you mean yes there is a better way to do this? I'm a bit confused because your example doesn't configure the interceptor for each call based on the properties of an attribute (or based on anything else).

Really really sorry if I've missed something obvious.

Jordan


 

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-users+unsub...@googlegroups.com.

To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at https://groups.google.com/group/castle-project-users.
For more options, visit https://groups.google.com/d/optout.

Krzysztof Koźmic

unread,
Aug 9, 2018, 7:23:13 AM8/9/18
to castle-pro...@googlegroups.com
Ah I see. So it's more about configuring the interceptors, rather than selecting which methods to intercept and which interceptors to use for the job?

Sounds like you might want to have your interceptors implement IOnBehalfAware https://github.com/castleproject/Windsor/blob/master/docs/interceptors.md#ionbehalfaware-interface to let you inspect the component model implementation for your attributes there, cache the data and then apply appropriately during interception.

On Thu, 9 Aug 2018 at 10:11 'Jordan' via Castle Project Users <castle-pro...@googlegroups.com> wrote:
Hello,

Yes this is the context of Windsor as opposed to using just plain DynamicProxy. I am working on a MVC Web app with NHibernate 5.1.3.

In the absence of an up to date NHibernate Facility I have created a very minimal OpenSessionInView implementation and a Transaction attribute/interceptor.

I want the Transaction attribute to have a couple of properties, eg System.Data.IsolationLevel and maybe a ReadOnly boolean which will be set on the interceptor so it knows how to configure the Transaction.

many thanks,
Jordan




On Wednesday, August 8, 2018 at 7:42:07 PM UTC+1, Krzysztof Koźmic wrote:
Hi,

No need to apologise. Having re-read your question I think I misunderstood what you wanted to do. Can you elaborate on how you want to configure the interceptors with attributes? You mention a container, does that mean your question is in the context of Windsor as opposed to using just plain DynamicProxy?

On Wed, 8 Aug 2018 at 17:30 'Jordan' via Castle Project Users <castle-pro...@googlegroups.com> wrote:
On Wednesday, August 8, 2018 at 4:16:53 PM UTC+1, Krzysztof Koźmic wrote:

Thanks for the reply! However, after one quick read I don't understand!

By "Yes. there is" do you mean yes there is a better way to do this? I'm a bit confused because your example doesn't configure the interceptor for each call based on the properties of an attribute (or based on anything else).

Really really sorry if I've missed something obvious.

Jordan


 

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-u...@googlegroups.com.
To post to this group, send email to castle-pro...@googlegroups.com.
--

sent from my phone
Krzysztof

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-u...@googlegroups.com.

To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at https://groups.google.com/group/castle-project-users.
For more options, visit https://groups.google.com/d/optout.

Krzysztof Koźmic

unread,
Aug 9, 2018, 7:26:06 AM8/9/18
to castle-pro...@googlegroups.com
Separately from that, if that's about ASP.NET MVC controllers, wouldn't IActionFilter make more sense than using DynamicProxy?

Jordan

unread,
Aug 9, 2018, 8:17:29 AM8/9/18
to Castle Project Users
Great! Thanks.

So given that each transaction on a single service may be configured differently, does the ComponentModel passed in tell me the method that is being intercepted?

And If an interceptor is added to the container and marked as transient, will a new instance be created for each method on a component that is being intercepted?

thanks,
Jordan.



So in the case of does target.Configuration.Attributes


On Thursday, August 9, 2018 at 12:23:13 PM UTC+1, Krzysztof Koźmic wrote:
Ah I see. So it's more about configuring the interceptors, rather than selecting which methods to intercept and which interceptors to use for the job?

Sounds like you might want to have your interceptors implement IOnBehalfAware https://github.com/castleproject/Windsor/blob/master/docs/interceptors.md#ionbehalfaware-interface to let you inspect the component model implementation for your attributes there, cache the data and then apply appropriately during interception.

On Thu, 9 Aug 2018 at 10:11 'Jordan' via Castle Project Users <castle-pro...@googlegroups.com> wrote:
Hello,

Yes this is the context of Windsor as opposed to using just plain DynamicProxy. I am working on a MVC Web app with NHibernate 5.1.3.

In the absence of an up to date NHibernate Facility I have created a very minimal OpenSessionInView implementation and a Transaction attribute/interceptor.

I want the Transaction attribute to have a couple of properties, eg System.Data.IsolationLevel and maybe a ReadOnly boolean which will be set on the interceptor so it knows how to configure the Transaction.

many thanks,
Jordan




On Wednesday, August 8, 2018 at 7:42:07 PM UTC+1, Krzysztof Koźmic wrote:
Hi,

No need to apologise. Having re-read your question I think I misunderstood what you wanted to do. Can you elaborate on how you want to configure the interceptors with attributes? You mention a container, does that mean your question is in the context of Windsor as opposed to using just plain DynamicProxy?

On Wed, 8 Aug 2018 at 17:30 'Jordan' via Castle Project Users <castle-pro...@googlegroups.com> wrote:
On Wednesday, August 8, 2018 at 4:16:53 PM UTC+1, Krzysztof Koźmic wrote:

Thanks for the reply! However, after one quick read I don't understand!

By "Yes. there is" do you mean yes there is a better way to do this? I'm a bit confused because your example doesn't configure the interceptor for each call based on the properties of an attribute (or based on anything else).

Really really sorry if I've missed something obvious.

Jordan


 

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-users+unsub...@googlegroups.com.
To post to this group, send email to castle-pro...@googlegroups.com.
--

sent from my phone
Krzysztof

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-users+unsub...@googlegroups.com.

To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at https://groups.google.com/group/castle-project-users.
For more options, visit https://groups.google.com/d/optout.

Jordan

unread,
Aug 9, 2018, 8:18:18 AM8/9/18
to Castle Project Users
I am running my transactions at the application service level, not at the controller level.


On Thursday, August 9, 2018 at 12:26:06 PM UTC+1, Krzysztof Koźmic wrote:
Separately from that, if that's about ASP.NET MVC controllers, wouldn't IActionFilter make more sense than using DynamicProxy?

On Thu, 9 Aug 2018 at 13:22 Krzysztof Koźmic <krzyszto...@gmail.com> wrote:
Ah I see. So it's more about configuring the interceptors, rather than selecting which methods to intercept and which interceptors to use for the job?

Sounds like you might want to have your interceptors implement IOnBehalfAware https://github.com/castleproject/Windsor/blob/master/docs/interceptors.md#ionbehalfaware-interface to let you inspect the component model implementation for your attributes there, cache the data and then apply appropriately during interception.

On Thu, 9 Aug 2018 at 10:11 'Jordan' via Castle Project Users <castle-pro...@googlegroups.com> wrote:
Hello,

Yes this is the context of Windsor as opposed to using just plain DynamicProxy. I am working on a MVC Web app with NHibernate 5.1.3.

In the absence of an up to date NHibernate Facility I have created a very minimal OpenSessionInView implementation and a Transaction attribute/interceptor.

I want the Transaction attribute to have a couple of properties, eg System.Data.IsolationLevel and maybe a ReadOnly boolean which will be set on the interceptor so it knows how to configure the Transaction.

many thanks,
Jordan




On Wednesday, August 8, 2018 at 7:42:07 PM UTC+1, Krzysztof Koźmic wrote:
Hi,

No need to apologise. Having re-read your question I think I misunderstood what you wanted to do. Can you elaborate on how you want to configure the interceptors with attributes? You mention a container, does that mean your question is in the context of Windsor as opposed to using just plain DynamicProxy?

On Wed, 8 Aug 2018 at 17:30 'Jordan' via Castle Project Users <castle-pro...@googlegroups.com> wrote:
On Wednesday, August 8, 2018 at 4:16:53 PM UTC+1, Krzysztof Koźmic wrote:

Thanks for the reply! However, after one quick read I don't understand!

By "Yes. there is" do you mean yes there is a better way to do this? I'm a bit confused because your example doesn't configure the interceptor for each call based on the properties of an attribute (or based on anything else).

Really really sorry if I've missed something obvious.

Jordan


 

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-users+unsub...@googlegroups.com.
To post to this group, send email to castle-pro...@googlegroups.com.
--

sent from my phone
Krzysztof

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-users+unsub...@googlegroups.com.

To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at https://groups.google.com/group/castle-project-users.
For more options, visit https://groups.google.com/d/optout.
--

sent from my phone
Krzysztof

Krzysztof Koźmic

unread,
Aug 9, 2018, 9:03:58 AM8/9/18
to castle-pro...@googlegroups.com
The ComponentModel will give your interceptor the information about the component so you can pre-cache whatever information you need to extract from the attributes (componentModel.Implementation will give you the type).

Then in the Intercept you can quickly look that data up. It's a performance optimisation. IIRC you'll get an interceptor instance per component instance, not per method, so one interceptor will apply to all methods on an instance (unless you exclude it with IInterceptorSelector).

Hope that makes sense

To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-u...@googlegroups.com.
To post to this group, send email to castle-pro...@googlegroups.com.
--

sent from my phone
Krzysztof

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-u...@googlegroups.com.

To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at https://groups.google.com/group/castle-project-users.
For more options, visit https://groups.google.com/d/optout.
--

sent from my phone
Krzysztof

--

sent from my phone
Krzysztof

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-u...@googlegroups.com.

To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at https://groups.google.com/group/castle-project-users.
For more options, visit https://groups.google.com/d/optout.

Jordan

unread,
Aug 9, 2018, 10:01:26 AM8/9/18
to Castle Project Users
Brilliant thanks! It has all fallen into place now.

I can cache the attributes for every method from the ComponentModel in a dictionary using the MethodInfo as the key, and then retrieve them from the cache using invocation.Method when the interceptor is called.

I am of course hoping that MethodInfo behaves nicely as a key in a dictionary. Not tested it yet :)

Jordan

unread,
Aug 9, 2018, 10:36:09 AM8/9/18
to Castle Project Users
ps - MethodInfo doesn't behave nicely as the key, but MethodInfo.Name does

Krzysztof Koźmic

unread,
Aug 9, 2018, 10:40:02 AM8/9/18
to castle-pro...@googlegroups.com
It's been years but IIRC MetadataToken was a good key (name will work only if you don't overload your methods).

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-u...@googlegroups.com.
To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at https://groups.google.com/group/castle-project-users.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages