1

Feature Request (if not available already?) selectedItemEquals property?

asked 2014-04-07 15:44:28 +0800

rickcr gravatar image rickcr
704 7

updated 2014-04-07 15:45:12 +0800

I asked this question here http://forum.zkoss.org/question/91512/your-business-domain-objects-in-zk-zk-have-an-equals-property/ but didn't get a response, maybe it was too lenghty a question:)

Bottom line question:

Is there not an attribute one could add to a listbox that provides a property on the objects backing the listbox in order to determine equality for determing "selectedItem" ? Right now, Zk seems to ONLY use the objects "equals" method but this has some serious drawbacks.

Let's say I want to set the selectedItem on a view model and the object I'm setting is fully hydrated. This object will likely have a different equals resolution than the objects I might use in my list backing my listbox.

For example if returning a list of employees to show in the UI I only need a query that returns the user's name and id. (I dont' need to hydrate, age, department, and all the other fields.) However when setting a selected item the employee I have in hand might be hydrated with many more fields, so that if my object's equals method resolves all fields the objects will never match.

Why not just provide an attribute "selectedItemEqualsOn" and you could provide a property that you compare equals on (typically it'll be an "id")

delete flag offensive retag edit

Comments

and why don't you implement the equals on id? you can set your db constraints of unique over multiple columns : http://www.w3schools.com/sql/sql_unique.asp

chillworld ( 2014-04-07 17:12:48 +0800 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2014-04-07 17:57:36 +0800

rickcr gravatar image rickcr
704 7

Since I don't have enough characters to type in the comments to chillworlds question I'll reply here. His comment:

"and why don't you implement the equals on id? you can set your db constraints of unique over multiple columns : http://www.w3schools.com/sql/sql_unique.asp"

That doesn't help chillworld. You can't assume database uniqueness applies to value object 'equality.' Often a domain object that you are using across projects will not even include something like "id" as part of its equals method. Often the point of implementing equals an object is to compare object equality on the properties of the object (not what makes it unique at the DB level.)

Let's say I manually create a Person object (doesn't have an id) and I want to see if it "equals" a different person object that "does" have an id. To to do this one would create the equals method comparing the objects fields NOT on the id.

And there lies the issue. I don't have the right as the UI developer to insist that the domain objects (often used across projects) have an equals method that matches what I want (based on an id.)

This leaves me two ugly solutions:

1) iterating over the list of domain objects before returning them to the front end and wrapping them inside of a wrapper object (adapter pattern) simply to override the equals method. or 2) Use a for loop in my view model and iterate over the list of items and compare equality to the id during each iteration until I find the correct selectedItem that matches on id.

All of this could be solved simply with another attribute on some of the tags that lets you choose a property you want to compare equality on. Going back over 10 years ago even basic Struts 1.0 had this option.

link publish delete flag offensive edit

Comments

I understand what you means, tomorrow I'll try something what I think is possible. (Thought I had it done once before but I'll check just to be sure.)

chillworld ( 2014-04-07 20:18:39 +0800 )edit
0

answered 2014-04-08 10:06:38 +0800

chillworld gravatar image chillworld flag of Belgium
5367 4 9
https://github.com/chillw...

updated 2014-04-08 10:08:07 +0800

Rickcr,

Is it possible for you to do class overriding?

Just tried in a project of me calling a webservice that returns a List of Objects that aren't in mine project. I created the class at the same FQN and added the @XmlRootElement to it(was original also in that class).

Overrided the toString to just give me "Chillworld object". The program reacted normally, and I get a nice "Chillworld object" as toString. All the other data where present.

So basicly you could then override the equals to what you wanted. If you want it further, you can implement a static enum to "switch" your equals method. (or work with static booleans to add properties if the boolean s true but I prefer the enum).

Greetz chill.

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2014-04-07 15:44:28 +0800

Seen: 16 times

Last updated: Apr 08 '14

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More