1

I will try to be as concise as possible. I'm using Flex/Hibernate technologies for my app. I also use Cairngorm micro-architecture for Flex. Because i'm beginner, i have probably misunderstand something about Caringorm's ModelLocator purpose. I have following problem...

Suppose that we have next data model:

USER  ---------------->  TOPIC  ------------->  COMMENT     
      1              M          1           M 

User can start many topics, topics can have many comments etc. It is pretty simple model, just for example. In hibernate, i use EAGER fetching strategy for unidirectional USER->TOPIC and TOPIC->COMMENT relations(here is no question about best practices etc, this is just example of problem).

My ModelLocator looks like this:

...
public class ModelLocator  ....
{
    //private instance, private constructor, getInstance() etc...
    ...

    //app state
    public var users:ArrayCollection;
    public var selectedUser:UserVO;
    public var selectedTopic:TopicVO;
}

Because i use eager fetching, i can 'walk' through all object graph on my Flex client without hitting the database. This is ok as long as i don't need to insert, update, or delete some of the domain instances. But when that comes, problems with synchronization arise.

For example, if i want to show details about some user from some UserListView, when user(actor) select that user in list, i will take selected index in UserList, get element from users ArrayCollection in ModelLocator at selected index and show details about selected user.

When i want to insert new User, ok, I will save that user in database and in IResponder result method i will add that user in ModelLocator.users ArrayCollection.

But, when i want to add new topic for some user, if i still want to use convenience of EAGER fetching, i need to reload user list again... And to add topic to selected user... And if user is in some other location(indirectly), i need to insert topic there also. Update is even worst. In that case i need to write even some logic...

My question: is this good way of using ModelLocator in Cairngorm? It seems to me that, because of mentioned, EAGER fetching is somehow pointless. In case of using EAGER fetching, synchronization on Flex client can become big problem. Should I always hit database in order to manipulate with my domain model?

EDIT: It seems that i didn't make myself clear enough. Excuse me for that.

Ok, i use Spring in technology stack also and DTO(DVO) pattern with flex/spring (de)serializer, but i just wanted to stay out of that because i'm trying to point out how do you stay synchronized with database state in your flex app. I don't even mention multi-user scenario and poling/pushing topic which is, maybe, my solution because i use standard request-response mechanism. I didn't provide some concrete code, because this seems conceptual problem for me, and i use standard Cairngorm terms in order to explain pseudo-names which i use for class names, var names etc.

I'll try to 'simplify' again: you have flex client for administration of above mentioned domain(CRUD for each of domain classes), you have ListOfUsersView(shows list of users with basic infos about them), UserDetailsView(shows user details and list of user topics with delete option for each of topic), InsertNewUserTopicView(form to insert new topic) etc.

Each of view which displays some infos is synchronized with ModelLocator state variables, for example:

ListOfUsersView ------binded to------> users:ArrayCollection in ModelLocator
UserDetailsView ------binded to------> selectedUser:UserVO in ModelLocator 
etc.

View state transition look like this:

ListOfUsersView----detailsClick---->UserDetailsView---insertTopic--->InsertTopicView

So when i click on "Details" button in ListOfUsersView, in my logic, i get index of selected row in ListOfUsers, after that i take UserVO object from users:ArrayCollection in ModelLocator at mentioned index, after that i set that UserVO object as selectedUser:UserVO in ModelLocator and after that i change view state to UserDetailsView(it shows user details and selectedUser.topics) which is synchronized with selectedUser:UserVO in ModelLocator.

Now, i click "Insert new topic" button on UserDetailsView which results in InsertTopicView form. I enter some data, click "Save topic"(after successful save, UserDetailsView is shown again) and problem arise.

Because of my EAGER-ly fetched objects, i didn't hit the database in mentioned transitions and because of that there are two places for which i need to be concerned when insert new topic for selected user: one is instance of selectedUser object in users:ArrayCollection (because my logic select users from that collection and shows them in UserDetailsView), and second is selectedUser:UserVO(in order to sync UserDetailsView which comes after successfull save operation).

So, again my question arises... Should i hit database in every transition, should i reload users:ArrayCollection and selectedUser:UserVO after save in order to synchronize database state with flex client, should i take saved topic and on client side, without hitting the database, programmatically pass all places which i need to update or...?

It seems to me that EAGER-ly fetched object with their associations is not good idea. Am i wrong?

Or, to 'simplify' :) again, what should you do in the mentioned scenario? So, you need to handle click on "Save topic" button, and now what...?

Again, i really try to explain this as plastic as possible because i'm confused with this. So, please forgive me for my long post.

slomir
  • 407
  • 2
  • 6
  • 16

3 Answers3

4

From my point of view the point isn't in fetching mode itself but in client/server interaction. From my previous experience with it I've finally found some disadvantages of using pure domain objects (especially with eager fetching) for client/server interaction:

  • You have to pass all the child collections maybe without necessity to use them on a client side. In your case it is very likely you'll display topics and comments not for all users you get from server. The most like situation you need to display user list then display topics for one of the selected users and then comments for one of the selected topics. But in current implementation you receive all the topics and comments even if they are not needed to display. It is very possible you'll receive all your DB in a single query.
  • Another problem is it can be very insecure to get all the user data (or some other data) with all fields (emails, addresses, passwords, credit card numbers etc).

I think there can be other reasons not to use pure domain objects especially with eager fetching.

I suggest you to introduce some Mapper (or Assembler) layer to convert your domain objects to Data Transfer Objects aka DTO. So every query to your service layer will receive data from your DAO or Active Record and then convert it to corresponding DTO using corresponding Mapper. So you can get user list without private data and query some additional user details with a separate query.

On a client side you can use these DTOs directly or convert them into client domain objects. You can do it in your Cairngorm responders.

This way you can avoid a lot of your client side problems which you described.

For a Mapper layer you can use Dozer library or create your own lightweight mappers.

Hope this helps!

EDIT What about your details I'd prefer to get user list with necessary displayable fields like first name and last name (to display in list). Say a list of SimpleUserRepresentationDTO.

Then if user requests user details for editing you request UserDetailsDTO for that user and fill tour selectedUser fields in model with it. The same is for topics.

The only problem is displaying list of users after user details editing. You can:

  • Request the whole list again. The advantage is you can display changes performed by other users. But if the list is too long it can be very ineffective to query all the users each time even if they are SimpleUserRepresentationDTO with minimal data.
  • When you get success from server on user details saving you can find corresponding user in model's user list and replace changed details there.
Constantiner
  • 14,231
  • 4
  • 27
  • 34
  • @Constantiner - Thanks for the answer. I edited my question. As i mention there, i use DTO(or DVO) pattern and i can choose which data i want to map from my server side domain objects to client side objects. If i understand well, you suggest me to simplify my DTOs and hit database for every peace of info i need (for example, if i need user topics, i will query database for them and so on)? – slomir May 27 '11 at 07:01
  • Right. Just create simple and applicable DTOs. – Constantiner May 27 '11 at 07:05
  • @slomir I've added some details to my answer. – Constantiner May 27 '11 at 07:56
  • @Constantiner - Thank you very much (ili Hvala ;). I got your point. I will create better granularity in DTOs and apply your approach. – slomir May 27 '11 at 08:26
  • @Constantiner - Please sorry for interrupt, i have just one question. First time, in your answer, you suggested me some assembler layer, most precisely Dozer. It is great lib and now i got his purpose. Now, on server side i have my service layer which returns domain objects. I don't want to change that because maybe, in future, my app will be exposed to classical Spring MVC etc. Is it reasonable to create some adapter layer to adapt my current service interface to Flex client calls? In adapter, i will call existent service layer, assemble my DTOs as i wish and return them to flex. – slomir May 27 '11 at 13:25
  • Yes^ I had exactly the same architecture as you suggested and it worked fine. – Constantiner May 27 '11 at 13:28
0

Tell you the truth, there's no good way of using Cairngorm. It's a crap framework.

I'm not too sure exactly what you mean by eager fetching (or what exactly is your problem), but whatever it is, it's still a request/response kind of deal and this shouldn't be a problem per say unless you're not doing something right; in which case I can't see your code.

As for frameworks, I recommend you look at RobotLegs or Parsley.

J_A_X
  • 12,857
  • 1
  • 25
  • 31
  • Thanks for the answer. By EAGER i mean that i get complete(or partial) object graph when i load some instances(it depends of hibernate association mapping and configuration). In my example, i practically get whole database because when i get all users, each of them will fetch their topics, each of topics their comments etc. To simplify, my problem is how to synchronize that application state(object graph) with database state after i insert, update, delete domain objects in database? Should I hit database and reload all again or...? – slomir May 26 '11 at 18:56
  • Wait, is your client talking directly to the DB? Unless this is an Air project, that's a very big no-no (and only if it's a local db). There should be some kind of middle layer between the two (java, php, something). Either way, there's 2 way to get your data, by polling or pushing. Polling means the client requests the new data every x amount of time, while pushing is a constant connection to the server where the server sends information about what's changed to the client automatically, to which the client performs an action on said data. – J_A_X May 26 '11 at 19:29
  • No. I use Spring as middle layer also. But, you mentioned 2 ways to get data from server: polling or pushing(i think that you describe long polling). I didn't consider that because of network traffic stuffs(in case of polling or long polling). Do you use (long)polling for your client/server sync? How much time to set for polling? If it is to long then there is a question how to synchronize your state right after you update database state, on the other hand if it is to short, network traffic problems arise. I'm open for that solutions, if you can give me some advice [Also, see my edit] – slomir May 27 '11 at 06:00
  • Pushing doesn't exactly mean long polling. If you're using LCDS, it's using the RTMP protocol which is real time pushing of information. However, long polling has worked well for me in the past and it's very little work on the front it, it's more on the back end. I'm sure you can find documentation online about the polling time. – J_A_X May 27 '11 at 13:22
0

Look at the "dpHibernate" project. It implements "lazy loading" on the Flex client.

an0nym0usc0ward
  • 1,207
  • 8
  • 8