Changes for page Overview-Key Value Coding
Last modified by Pascal Robert on 2007/09/03 13:25
From version 3.1
edited by Ray Kiddy
on 2007/07/12 17:35
on 2007/07/12 17:35
Change comment:
There is no comment for this version
To version 6.1
edited by Pascal Robert
on 2007/09/03 13:25
on 2007/09/03 13:25
Change comment:
There is no comment for this version
Summary
-
Page properties (4 modified, 0 added, 0 removed)
-
Attachments (0 modified, 1 added, 0 removed)
-
Objects (0 modified, 1 added, 0 removed)
Details
- Page properties
-
- Title
-
... ... @@ -1,1 +1,1 @@ 1 - Programming__WebObjects-Overview-Key Value Coding1 +Overview-Key Value Coding - Parent
-
... ... @@ -1,0 +1,1 @@ 1 +Development Architecture - Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. kiddyr1 +XWiki.probert - Content
-
... ... @@ -1,27 +1,27 @@ 1 -Key-value coding is a way to separate interfaces and a way to flexibly access data and methods in object instances. The concept has a long history. It appeared in Eiffel, and other places as well. If you familiar with [[OGNL>>http://en.wikipedia.org/wiki/OGNL]] then you will know it from there also.1 +Key-value coding is a way to separate interfaces and a way to flexibly access data and methods in object instances. The concept has a long history. It appeared in [[Eiffel>>url:http://en.wikipedia.org/wiki/Eiffel_(programming_language)||shape="rect"]], and other places as well. If you are familiar with [[OGNL>>url:http://en.wikipedia.org/wiki/OGNL||shape="rect"]] then you will know it from there. We'll call key-value coding KVC in the context of this page to abbreviate. KVC also exists in the Cocoa infrastructure shipped with MacOS-X, having evolved from OPENSTEP and the development tools created for it. In this page we will focus on KVC in the context of WebObjects and java. 2 2 3 3 = Conceptual Aspects = 4 4 5 -Consider the following model segment ;5 +Consider the following model segment: 6 6 7 -[[image:ModelForKVCArticle.png]] 7 +[[image:attach:ModelForKVCArticle.png]] 8 8 9 -This model shows a number of entities and the lines between the entities show relationships. 9 +This model shows a number of entities and the lines between the entities show relationships. For example, the Organisation entity has a to-one relationship with a Worker called "workerAccount" and it also has a "to-many" relationship to "OrgRegion" called "orgRegions". 10 10 11 -The term "instance" here is referring to a specific organisation. 11 +The term "instance" here is referring to a specific organisation. For example, "The Corn Flour Company" and "Aqua Breweries" might be instances of the Organisation entity. 12 12 13 13 == Value For Key == 14 14 15 -An instance of an Organisation can supply values for the keys "address", "countryIso", "email" etc... and they will return a value for the key. 15 +An instance of an Organisation can supply values for the keys "address", "countryIso", "email" etc... and they will return a value for the key. It is essentially acting like an NSDictionary or a Map object. This is how data is extracted from an object. 16 16 17 17 == Take Value For Key == 18 18 19 -This is where an instance of an entity is asked to "take up" some value for a key. 19 +This is where an instance of an entity is asked to "take up" some value for a key. For example, an instance of an organisation might be told to take the value "foo@foo.co.nz" for the key "email". This is how data is loaded into an object using KVC. 20 20 21 21 == Value For Key Path == 22 22 23 -Using a dotted path, it is possible to traverse relationships to access data in other entities. 23 +Using a dotted path, it is possible to traverse relationships to access data in other entities. For example, if you have an instance of "OrgExpertise", it is possible to get the value for the key path "organisation.workerAccount.name". This is how you can extract data from the interconnected network of object instances. 24 24 25 25 == Take Value For Key Path == 26 26 27 -Using a dotted path, it is possible to get an object in the interconnected network of object instances to "take up" some piece of data. 27 +Using a dotted path, it is possible to get an object in the interconnected network of object instances to "take up" some piece of data. For example, if you have an instance of "OrgExpertise" then it is possible to get the object to "take up" a value of "Joe Smith" for the key path "organisation.workerAccount.name".
- ModelForKVCArticle.png
-
- Author
-
... ... @@ -1,0 +1,1 @@ 1 +XWiki.XWikiGuest - Size
-
... ... @@ -1,0 +1,1 @@ 1 +41.7 KB - Content
- XWiki.XWikiComments[0]
-
- Author
-
... ... @@ -1,0 +1,1 @@ 1 +XWiki.gavineadie - Comment
-
... ... @@ -1,0 +1,27 @@ 1 +This is some text I wrote a little quite ago to explain KVC to some colleagues. It introduces KVC in a non database context, which I think is a bit easier to get a grip on, but it doesn't fit the style of this page, hence added as a comment: 2 + 3 +---- 4 + 5 +One design pattern of WebObjects that is important to have a decent understanding of (and it's an understanding that gets deeper and deeper with experience) is "key-value coding" (KVC). It is a simple capability but lies at the heart of the WebObjects user interface code and at the heart of Enterprise Objects. 6 + 7 +It's the seeming magic that allows values to be pulled out of classes by only knowing their name (or key) – the code that tries six ways to get a value. When a class inherits this behavior it becomes a bit like a dictionary – you can ask a class for a VALUE when all you know is the KEY. 8 + 9 +Many classes in the WebObjects frameworks inherit this capability, with the result that you have classes that can exchange data at run time, though they had no knowledge of each other at compile time. Very powerful. 10 + 11 +You've seen this in a WOComponent: at run time as WebObjects is building the HTML to send back to the browser it keeps bumping into these <WEBOBJECT NAME=xxx> tags. As each tag is encountered the bindings in the definition file: 12 + 13 +{{noformat}} 14 + 15 + xxx: WOString { 16 + value = countryOfBirth; 17 + } 18 + 19 +{{/noformat}} 20 + 21 +are used to expand the HTML. When the value of the WOString is being determined the component is interrogated using KVC for value of the key "countryOfBirth". 22 + 23 +In the Enterprise Object (EO) side of things where databases are managed, KVC is also used extensively. In simple terms EO's job is to transform a database row into a class - the columns of the row are accessed via KVC – that is, you tell the class to get the VALUE of the 24 + KEY which is the column name. So with the class Employee, for example, you get/set entries in the class (and ultimately, on a commit, the database) with keys like "firstName", "Salary", "Manager". 25 + 26 +But this gets really spooky with EO! In the HTML expansion, we have to supply a variable or write a getter method to satisfy the KVC hunt. In EO, you don't! Any class which represents rows in a database, inherits the KVC behavior, but for these classes (based on EOGenericRecord), in 27 + addition to the six getter methods, it can also return values directly from database rows already cached in memory. - Date
-
... ... @@ -1,0 +1,1 @@ 1 +2007-08-13 19:24:51.0