Welcome to WebmasterWorld Guest from 3.80.6.254

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Role of a Router vs Role of a Dispatcher

     
11:54 am on May 21, 2015 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 20, 2004
posts: 615
votes: 0


Hello All -

I've been around the web a few times on this question but the router vs dispatcher role is still not completely clear to me.

The reason I'm asking is that I'd like to build a router and a dispatcher class just for practice but, obviously, I need to know what the job of each is.

So, from what I understand, a...

Router take a uri or post request, breaks the request apart and then returns the result as an array, while a...

Dispatcher takes the router array and grabs appropriate parts of a database table based upon the various array indexes.

While the above seems like the logical role for each object, this description also seems overly simplistic so I must be missing something (or a lot of things... aren't I?).

Can anyone please fill in the blanks for me about the full role a router and dispatcher plays in an application framework?

Thanks to all in advance!
1:30 pm on June 15, 2015 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


Hey neophyte,

In our world technical jargon abounds. Analysts and programmers love to lay claim to being the first to birth the child and then offer it a name. If they feel it is somewhat different than something very close to identical, it gets a different name. So when setting out to learn we often find ourselves wrapped up in the jargon becoming more confused than when we started on the venture. (sigh)

Descriptions are supposed to be overly simplistic so that the common person might understand the definition. Drifting from this is a tragedy ;-)

In my humble opinion you do already understand the full role. Routing is exactly what it means -- figuring out which direction to take. It doesn't always have to be an array though. And dispatching is responding to the requested route. In it's simplest definition, that's all there is to it.

Different libraries/frameworks may use different terminology and definitions and even different programmatic solutions, but the premise is the same -- we need to respond (dispatch) to a request (router).

I find things often make most sense after I've absorbed as much background education as I can and then apply it. How did your practice turn out?
1:41 am on June 17, 2015 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 20, 2004
posts: 615
votes: 0


Hi Coopster –

Thanks very much for breathing some life into my little post.

Yes, I agree, it would appear to be pretty straight-forward regarding what a router and dispatcher does (as I initially outlined and you have confirmed) and I now I realize that my question should have been more specific.

Aside from (what I would consider to be) the obvious:
-A Router object would take a URI, pick it apart and (in my case) validate the parts against application requirements. If all URI segments pass validation, the result is then passed to a …
-Dispatcher object which uses the router’s result to retrieve appropriate database data fields. This data set would then (in my case) be passed to one or more controller methods which would prep the data set for display.

While “I think” that the above would be the essential roles for each object, I was really after (but didn’t specify) in my initial post is – aside from the above – are there any:
-Best practice issues I should implement in each object?
-Are there any specific security concerns I should trap for (particularly in the Router object)?
-Should a dispatcher (from a best practices standpoint) simply draw data from DB tables and then send that data off to another object for display, or is it better to have the dispatcher display the data as well?

I don't mind at all keeping things as simple as possible... I just want to make sure I'm not missing any "best practices" issues when coding these objects.
1:47 pm on June 19, 2015 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


Best practices? You are going to get answers all over the board on this one because of differing developer personalities, style and beliefs in best coding practices. I prefer simplicity without sacrificing security and performance. And how you have designed and structured your application must be part of the equation, of course.

A URI is user-supplied input so my router checks for exactly what I expect and from what I read in your post it sounds like you are doing the same. Depending on your application design you need to take security points into consideration for that request. For example, if your router is validating against database values then you better be sure you are preparing the user-supplied information properly before processing a query. If it is assembling a file system path and/or file name you best be sure it is within your document root and/or the expected path without unexpected characters -- ../../../, for example.

Your dispatcher could certainly do both, that is up to you. Think about the future though ... will there ever be a time where you may need to display the data differently than you do today and if you build your dispatcher without this flexibility in design, is it going to serve you well or will you have to redesign your application? What if you need to use load balancers and multiple database servers? What if you need to dispatch your request to a different server to retrieve information?

I don't mean to answer your question with more questions. I just want you to think through your situation and needs as you develop. And the best part about it is that your are a developer so you have the ability to change things if and when you need to do so.

And just because a blog post (or a forum moderator) says you can and/or should do it one way does not mean they are correct. They may be correct for the application they are coding, but perhaps not for you.