Applying multiple OpenRasta OperationInterceptors
OpenRasta has the concept on an OperationInterceptor, a special class that runs before and after your handler method is called. It’s useful when you have a cross-cutting action that should apply across all your resources. A couple of typical examples are:
- Validation, when you need to validate the incoming resource object on a
PutorPost - Authorization, when you need to check that the authenticate user has rights to perform this action on the resource.
So you create a subtype of OperationInterceptor, and you hook it up in your IConfigurationSource like this:
ResourceSpace.Uses
.CustomDependency<IOperationInterceptor, MyOperationInterceptor>
(DependencyLifetime.Transient);
Unfortunately, OpenRasta only supports one OperationInterceptor. That makes the OpenRasta API much simpler: if OpenRasta supported multiple interceptors, it would need a mechanism for ordering each of the OperationInterceptor instances that you pass it.
Thanksfully it’s easy to get around because we can use create a CompositeOperationInterceptor that does our ordering for us. It’s dead simple and you’ll find the code below. This’ll work if you’ve got a ValidationInterceptor and an AuthorizationInterceptor (you’ll need to write these yourself, of course).
public class CompositeOperationInterceptor : OperationInterceptor
{
readonly IEnumerable<OperationInterceptor> _interceptors;
public CompositeOperationInterceptor(IDependencyResolver resolver)
{
_interceptors = new List<OperationInterceptor> {
resolver.Resolve(typeof(ValidationInterceptor))
as OperationInterceptor,
resolver.Resolve(typeof(AuthorizationInterceptor))
as OperationInterceptor
};
}
public override bool BeforeExecute(IOperation operation)
{
return _interceptors.All(i => i.BeforeExecute(operation));
}
public override bool AfterExecute(IOperation operation,
IEnumerable<OutputMember> outputMembers)
{
return _interceptors.All(i => i.AfterExecute(operation, outputMembers));
}
}
And in your IConfigurationSource you need to hook up your interceptors:
ResourceSpace.Uses.
CustomDependency<ValidationInterceptor, ValidationInterceptor>
(DependencyLifetime.Transient);
ResourceSpace.Uses.
CustomDependency<AuthorizationInterceptor, AuthorizationInterceptor>
(DependencyLifetime.Transient);
ResourceSpace.Uses
.CustomDependency<IOperationInterceptor, CompositeOperationInterceptor>
(DependencyLifetime.Transient);
Easy
Just on a final note: the exclusion of this feature in OpenRasta is a great example of API design. It keeps the design simple, minimises the API surface area and yet doesn’t limit functionality. Nice.
2 Responses to Applying multiple OpenRasta OperationInterceptors
Leave a Reply Cancel reply
Tweets
- Confession: This past year, I forgot that I'm a geek. I love technology. I am obsessed with software. And now it's time to be myself again!
- @Jermolene I can just imagine you outside with your laptop in a cardboard box. That is so completely you. Awesome.
- What's the best device for writing code outdoors in the sunshine? It's a beautiful day outside and I'm stuck indoors:(
- RT @randompunter: 10 Check Amazon.co.uk for cheap touchpad. 20 GOTO 10
- @IJohnson_TNF typical example of a beautiful Ruby blog - http://t.co/6lHzwX2 show me a .NET blog like that!
- @IJohnson_TNF I think the ruby people are just more shiny
- Blogs about Ruby are always so much prettier than blogs about .NET.
- @Oura_In_Flames It's not all that amazing, and very short!
- @Oura_In_Flames you played the Sonic Generations demo yet? ;)
- @Oura_In_Flames cheers for the birthday wishes, I've been too busy at work to even think about it...
- Reading Programming in Scala, and loving it!
- Learning Scala this weekend... Quite exciting!
- @serialseb okay, on Thursday ;)
- @Oura_In_Flames lol thanks
- After two weeks of dev, finally deployed my OpenRasta app on IIS. Feeling so pleased with myself!
- Arrived at work for 8am. First time that's happened in years!
- Last one in the office - again. #deadlines
- Finishing the evening with a book in bed!
- @Oura_In_Flames Then I must have done something terribly bad as I just got drenched
- Dear Mother Nature, please check your wall calendar is on the correct page. It's June, not January. Yes, June. #wet





Actually no, we support as many operation interceptors as you want. Have you tried registering multiple interceptors in your container?
Thanks Seb, I’ll give it a go and see what happens and I’ll update the post with my findings.