views:

49

answers:

3

I am working on web project. We are using flex as UI layer. My question is often we are writing core service layer separately from web/UI layer so we can reuse same services for different UI layer/technology. So practically is it possible to reuse same core layer services without any changes/addition in API with different kind of UI technologies/layers. For e.g. same core service layer with UI technology which supports synchronized request response (e.g. jsp etc.) and non synchronize or event driven UI technology (e.g Ajax, Flex, GWT etc.) or with multiple devices like (computers, mobiles, pdas etc.). Personally I feel its very tough to write core service layer without any knowledge of UI. Looking for thoughts from other people.

+1  A: 

It shouldn't be difficult to write core services without knowledge of the UI at all.

The core services just need to know what data is needed to perform their tasks (where it comes from doesn't matter).

Once you have the core services designed, you can then build several different UIs on top of them that collect the necessary data and pass it to the services...which would then perform their specific duties.

Justin Niessner
Suppose I am writing core services for flex(or any UI technology which works on asynchronous communication) then it expects original VO back with response due to asynchronous nature, but if I am writing core services for jsp(or any UI technology which works on synchronous communication) then original VO is not required in response due to synchronous communication. This is just thought. I am facing difficulties in same. So may be you can correct me if I am wrong.
Silent Warrior
I've written services that I've used in jsp's, flex apps, and sharepoint for that matter. If you're designing your basic services around your UI or even UI technologies, you aren't doing it right. ;)And even though I've written a couple of huge Flex projects, I've never noticed that it required the same data structure passed back that is passed in, in fact I'm sure it isn't.
mezmo
I'd like to expand a little on what I said previously. I have, in the interests of speeding things up, at times added methods to my service layer that would aggregate service calls in certain cases. In my case it was a flex module that had to fill 25 combo boxes, BlazedDS will collect a certain number of calls and issue them together on the back end, for my combos it was taking about 15 seconds. I added one method to my service layer that returned an array with all 25 combo results in it, which took 3 seconds or so. So the core services didn't really change, but its now much faster.
mezmo
+2  A: 

It is possible of course. Services layer is most often stateless, so it just offers methods to be called with parameters that come from the UI. You can imagine it as an API that you call from your UI layers. It is important not to pass anything UI-related as a parameter. For example, don't pass HttpServletRequest or HttpSession as parameters - obtain the values needed and pass them.

Bozho
+1  A: 

My question is often we are writing core service layer separately from web/UI layer so we can reuse same services for different UI layer/technology

The service layer should be agnostic of the UI, so that's actually very good.

For e.g. same core service layer with UI technology which supports synchronized request response (e.g. jsp etc.) and non synchronize or event driven UI technology (e.g Ajax, Flex, GWT etc.) or with multiple devices like (computers, mobiles, pdas etc.).

A business operation exposed in a service layer should theoretically not depend on who calls it and which technology. But you seem to be facing to common symptoms:

  1. different UI do actually require slightly different business operations (e.g. the amount of data returned may differ for mobile and non-mobile caller)
  2. the business operations is coupled with the remoting technology (e.g. synchronous vs. asynchronous)

For 1. the question you must ask yourself is whether you can factor these slightly different operations elegantly, or the opposite, how to expose variations of the same operations elegantly. In both cases, it should be possible to design a service API that is consistent and fit the need of various clients.

For 2. the question you must ask yourself is how to decouple the business operations from the remoting technologies. A bridge or adapter or an extra mediation layer could be introduced, but that should not lead to over-engineering.

I know it's sometimes hard to decouple the business operation completely from its actual usage. Let's consider pagination: if the business operation is "I want data for query X", the concrete operation exposed does also embed some knowledge of the UI, namely the paging.

I can only provide a general advice: fight for a clear separation between UI and business when it comes to data, formatting, etc. and when you can't, try to figure out the most generic API that makes sense for all clients.

Personally I feel its very tough to write core service layer without any knowledge of UI.

I agree it's hard sometimes, but you seem to be doing it right -- so continue this way :)

ewernli