1

I have a REST application where 'actions' can be executed to perform anything in the system. For example: user.save, user.get, etc...

I also have pre and post execution 'handlers'. For example a post execution handler is used to add tags to the users; it's an AddOn module that handles the tags of the users (and other entities). I also have another module (called Rules) that can, for example, validate the user information and do something based on that (eg, send an email).

The problem I'm facing now is that in general the execution order of those modules is not important, but sometimes it is. Take the following cases:

Example 1:

  1. Action: Save user
  2. Handler: Tag the user as "VIP"
  3. Handler: Send an email if user is tagged as "VIP"

=> The email gets send.

Example 2:

  1. Action: Save user
  2. Handler: Send an email if user is tagged as "VIP"
  3. Handler: Tag the user as "VIP"

=> The email won't be send.

All the operations (action and handlers) are executed inside a single transaction.

What would be a good solution to make sure the "Send email" handler is executed after the "Tag user" one?

Some ideas: - Have post-action handlers and post-(post-action) handlers. But how many post-post-post would I end up adding? - Add a weight or priority index to each handler. But when you end up having several handlers, the priorities tend to be confusing as each module doesn't know about the other modules and handlers.

What would you recommend? Thanks.

In case anyone finds it important: it's C# 4.5, ASP.NET, IIS.

  • Can you clarify, is each 'action' a new call from the client to the server (a new HTTP request?). If so then you shouldn't try and make them execute in a particular order. If the order is important make them one HTTP request. – Cormac Mulhall Mar 5 '15 at 10:33
  • Yes. Each action is an HTTP request, but I'm not trying to order the actions, I'm trying to order the "handlers". You can think of a handler as an IHttpModule and an action as an IHttpHandler, but they all running inside the same transaction. – Diego Jancic Mar 5 '15 at 13:50
  • So I'm not familiar with C# frameworks, but how I would do it in Python is using a publish subscribe system, so that each handler fires a notification that it is finished and every subscribed handler would then be called. So each handler does not have to be aware of what handlers are waiting for them to finish. – Cormac Mulhall Mar 5 '15 at 14:16
1

There are obviously dependencies between the handlers.

One possible way to do this is to assign priorities to the modules - good thing is that it is easy to implement, but (as Frank Hileman noted) this solution is not scalable. Essentially you hide the dependencies between modules from the system and solve these dependencies yourself by manually ordering the modules - you need to keep these dependencies in your head or in some non-formal way (comments, documentation). This is fine as long as there isn't a lot of modules and dependencies.

More scalable solution would be to express the dependencies direclty in the code and then have some engine which resolves them automatically and orders the execution of the handlers. For example each handler can have two properties:

  • prerequisites (e.g. TAGS) - what is necessary to be done for this handler to be executed (tag operations must be completed)
  • operatesOn (again TAGS) - what is this handler doing (modifies tags)

Rule is that handler can be executed only after all of its prerequisites have been already finished. "Finished" means that there's no other handler with given prerequisites in its "operatesOn" field which hasn't been already executed.

Example, you have two handlers:

Handler tagging the user as VIP:

  • prerequisites: none
  • operatesOn: TAGS

Handler sending the email:

  • prerequisites: TAGS
  • operatesOn: USER_EMAIL

First you execute handlers with no dependencies (VIP tagging one). Then you try to find handlers for which all dependencies have been completed. You look at handler sending the email, it has dependency TAGS. You check that all handlers which operate on TAGS have been finished so you can execute handler sending the email.

This will effectively create a tree of handlers which expresses the dependencies. As with any trees, you must be careful to not introduce circular dependencies - one handler depends on the result of different handler which again depends on the result of the first handler.

Of course this has quite an upfront cost of designing such system, so it's more useful for complex (and generalized) systems with a lot of handlers and dependencies.

1

I would consolidate the actions and handlers into higher level abstractions that operate using a fixed order of operations. The priority-based solution will not scale.

0

The general approach for ensuring a list of handlers getting executed in the right order is to not make every handler as being ready to execute, but to only make the first such handler as being ready, and pass on the remaining list of handlers to the first handler, so that the first handler can execute the next one on the list as the first one finishes. Each handler will then pop one off and execute the next, until the list becomes empty.
(This is not specific to ASP.NET; it is just a general approach in software.)

Another possibility is that you will need to have someone review your source code and implementation, to see whether you are using IHttpModule and IHttpHandler in the correct (intended) way. If your code did not achieve the desired effect because you are not using them according to the framework design, it will be futile to ask such questions on Programmers.
(This should not be done on Programmers.SE nor CodeReview.SE; you should seek someone from your own software department.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.