WWDC2004 Session 609
Transcript
Kind: captions
Language: en
good afternoon before I start I
recognize a few faces in the audience
can I see how many people have already
done any web objects development before
would you forgive me for asking the
question why are you here
this is an introductory session on
learning to develop using web objects so
if you've already done a few weeks worth
of oil widgets development this is
probably not the session for you if
anybody wants to take the opportunity to
find something more advanced to go to
please do so now okay
so what will we cover this is going to
be for those of you who've been on any
web watches programming classes about
the equivalent of the first weeks worth
of what objects programming class in
about 55 minutes I wonder though if I
might just ask if the clock has started
yet just so I can count down so it's got
to be necessarily a very rapid
run-through we're not going to be able
to go into any great deal of depth on
the subject we're going to cover a
little bit about what web objets is how
you use it some of the basic design
principles that you use with web objects
as a technology some of the main
features and some of the benefits that
you get from using web objects in
particular as we go through again to
highlight some of the more useful
aspects of the technology in some
respects web objects is already doing
and has been doing for some time what a
number of other technologies are aiming
to do in separating the business logic
from the presentation layer what I want
to do is illustrate some of that with
demonstrations as we go through as I go
through this presentation are a few
things about making of you the sorts of
things the sorts of technologies that
web objects developers need to know
about in order to be successful the
technology are first of all Java
it's possible to learn Java as you're
developing and gaining experience with
web objects who here just sort of
interest then has not done any Java
development before a few of you if you
have a look on Apple's website you'll
find a document Java for web objects
developers many people have found that
that's a very useful document for
learning just enough Java to figure out
how to use web objects it's actually
quite a good introductory document just
for learning Java itself it's also
useful to know something about databases
so why our database is useful how do you
interact with them and so on and of
course because there's a web-based
development environment is useful to
know about something about HTML as well
as important in it as as any of these
other technologies though to be
successful with web objects you need an
ability to think in terms of objects so
this is a theme that I'm going to repeat
again on a couple of occasions
throughout for those of you who've come
from a procedural background for those
of you who are used to scripting and so
on this may mark a fairly major paradigm
shift for you a different way of
thinking you'll find quite foreign so
the common theme throughout is objects
for the web what does a web objects
provide for you where objects aims to
give you a high degree of abstraction so
we've got object representations of a
number of the different state management
entities and so on that you'll use
during typical web development we
therefore get all the benefits that you
traditionally associated with object
oriented design so reuse simplicity of
concept and so on I want to emphasize
that last point simplicity of concept
one of the I'd like to suggest myths
that's commonly associated with web
objects is that it has a steep learning
curve q Justice Steve Heymann therefore
pointing out that the benefit of a steep
learning curve is it means that you
learn an awful lot in a short space of
time what I would also like to suggest
though is that many people find that web
objects has a steep learning curve
because they do not bring to the table
the knowledge that I listed earlier is a
set of prerequisites people who
typically have difficulties with web
objects are people who don't know Java
don't know a thing about databases and
don't know anything about HTML if you
bring a knowledge of all those
technologies to the table and think in
terms of objects you'll have a much
easier time so bear that in mind as you
approach the technology another thing to
bear in mind is that having done a lot
of training courses using this
technology in their past I've often
noticed that people have a tendency to
make things difficult for themselves if
at any stage you find yourself
struggling with a technology if you
can't figure out how to get things
connected if you can't figure out how to
do something stop for a moment pause sit
back think back to this session so take
a moment just have a look around the
room and familiarize yourself with this
environment think back to here and think
back to my voice on stage saying it's
probably easier than you're making it
you're thinking that it's more
complicated than it really is often to
do something complicated in webobjects
takes a couple of lines of code the
difficulty of course is figuring out
what those couple of lines are so let's
have a quick recap then on what web
algae's is all about and in particular
for those of you who might have been to
one of the previous sessions or read a
bit about the technology and so on what
is web objects from a developer's
perspective so as you sit here what will
what use will you make you for the
technology from a perspective that we're
looking at it from today we're looking
at web objects as a web development
environment it's a set of tools and a
set of four libraries for doing web
based development as you're probably
aware we're babji's is actually useful
for creating other sorts of application
as well so web services for example you
can use it to create desktop based
applications and so on but we're
concentrating on its initial strengths
here of building interactive dynamic
websites the main strengths and the main
reason that many people are interested
in the technology is because it's
particularly good at talking to
databases so if you need to create a
dynamic website website that talks to an
Oracle database aside a
they state abase or whatever webobjects
gives you an excellent set of
technologies for doing that it's also
specially scalable so if you bear in
mind it for example the iTunes music
store uses web objects as a back-end
technology that gives you an idea of how
robust how scalable and so on this
technology is it's been around for a
long time so it's heritage goes back a
decade or so in terms of weather widgets
it's in many respects its can be
considered as being middleware if we
look to this report card when its main
goals is to play well with others so it
doesn't matter what web server you use
it doesn't matter what database you use
doesn't matter what HTML you want to
produce and so on where Bob Jets fits in
with the way that you want to work one
of the advantages of this one particular
benefits of this and the way the
technology itself is structured is that
it allows you to concentrate on that
part of the application that you're most
interested in as a developer you're
probably most interested in the jar
where the application logic it allows
your database in device admins to
concentrate on the database side of
things and your graphic designers to
concentrate on the HTML again from the
developers perspective what tools do you
use on a daily basis the main one that
you're going to use it certainly on Mac
OS 10 so there are other options for
somebody's on other platforms on Mac OS
10 you'll use Xcode so the traditional
Mac OS 10 development environment as you
see who's not has anybody not used Xcode
before okay so a couple of you but for
the rest of you familiar territory where
objects builder provides you with a
graphical HTML editing tool you can use
others but web objects builder we'll see
in a moment obviously has particular
strengths for dealing with some of the
dynamic elements of web objects users yo
modeler finally is our entity relational
mapping tool so it's going to be the key
to figuring out how stuff in the
database gets into our application on
screen some of the other things that
we're Bob J's gives us as a as an entire
package it gives us a collection of
frameworks for providing object
abstraction so most of the entities or
things that we want to deal with as web
developers in particular for
representing dynamic web elements that's
individual strings hyperlinks webpages
and so on and for doing state management
one of the frameworks that I'll just
mention in passing here is the
foundation framework who's done any
cocoa development or heard of the
foundation framework in cocoa for those
of you again small number the there's a
fairly strong correspondence between the
foundation framework in cocoa and the
foundation framework in web objects it
provides you with a set of utility
classes for representing number numbers
dates dictionaries arrays and so on
in addition there are a couple of other
frameworks the web objects framework
itself provides the object of structions
for the dynamic elements and then
there's a framework called the
enterprise or a couple of frameworks
that are bundled together and something
called the enterprise object framework
which actually interacts with a database
for you
so that's web objects in a nutshell from
the developer's perspective in the
remaining sections what I want to look
at first of all some of the underlying
design patterns that are going to be
useful to you as web objects developers
so some of the main ideas that underlie
the technology then have a look in
greater detail at some of the frameworks
that you'll be using so what's in the
fan what's in the web objects framework
what's in the enterprise objects
framework and see then particularly
illustrated using demonstrations some
other tasks your face on a day-to-day
basis so turning first of all to the
concepts to reiterate a point that I
made of several occasions up until now
then objects objects objects think in
terms of objects many other tools that
you may have used in the past will
require you to think in procedural terms
requires to do a lot of data munging or
whatever to figure out what's going on
in your application web objects is as
the name implies object oriented so we
have object abstractions for webpage and
things on a web page that you could send
messages to where you have object
abstractions for records in the database
so when you retrieve information the
database you get objects representing
the records even the HTTP requests and
response are abstracted out into objects
so for those of you who may in some
environments be used to doing your own
parsing or messing around with HTTP
requests and
response don't have to do that we'll see
an illustration of that and very early
on and then of course for state
management so why are we interest in
this state management again thinking
about web objects as a traditional web
development environment one of the
things that many people want to get out
of a web development or a web
environment is some sort of state
management if you're putting up a
website particularly commercial websites
these days you want to have an idea of a
user so that a user has a shopping cart
that they can put stuff into and later
give you a credit card number that you
can charge that you can earn some money
and it's possibly not part of the
traditional web development environment
there you go so web objects provides you
with state management abstractions for
individual sessions for an individual
user and then for an application and
then at the the level of an individual
page the design patterns the main ones
that you need to bear in mind first of
all model-view-controller has anybody
who's heard of model-view-controller
before okay this is going to be
something if you go to many of the other
sessions particularly the cocoa sessions
for those of you who are interested
you're going to get drummed into during
the remainder of this week the idea
fundamentally behind model of your
controller is the separation of the
factoring out of function as a name
implies you have different sorts of
objects in your applications model
objects which typically correspond to
records from the database view objects
which in this which in our case are
going to represent webpages or dynamic
elements within a webpage and then a
controller object which mediates the
between the two the idea is that you
should not pollute your model objects in
particular with any knowledge of the
user interface this makes them
particularly reusable between
applications similar story then with the
view objects if a view object is not
particularly bothered about what sort of
data is presenting their reusable the
controller is the glue code which sits
between the two and typically that's
unique to each individual application
another important design pattern key
value coding probably again familiar to
some of you from the cocoa environment
the idea behind key value coding
is that it gives you a means of
accessing and objects properties using
the name of a property as a string so if
a person has a first name property
rather than using an accessive method or
accessing an instance variable of that
object directly you can use a message
such as value for key and then passing a
parameter first name as a string the
main advantage of this is that this
gives you very good flexibility at
runtime you can determine at runtime
what property of an object is that you
want to access this is what gives web
objects an awful lot of its flexibility
another point to bear in mind here is
that in addition to asking for just a
single property you can follow what are
known as key paths so if a person has a
relationship to their manager and the
manager then is another person who has a
first name you can ask a person object
for their managers first name by using
the key path manager dot first-name so
key value coding gives you means of
traversing relationships the final
design pattern that I want to draw your
attention to here is one that's perhaps
a little bit unexpected target action
again anybody from the cocoa development
environment or from any other
development environment who's heard of
target action before okay a very simple
idea basically that when and certainly
in a desktop application if you click a
button a message is sent to whatever
object you've connected it to what we'll
see I think straight up very very early
on is it with web objects you get a
design a development environment it's
very much like in many respects a
desktop application development
environment when you click on a button
or a hyperlink you can cause an action
method to message to be sent to an
object
to return to frameworks for those who
haven't come across the jargon word
framework before you can think of a
framework as being pretty much like a
class library collection of objects the
main differences are a a framework may
contain other resources so it may
contain HTML that may contain
documentation it may contain yo model
files we'll talk about those later in
addition one of the main things to that
that may affect you as developers here
is that programming using frameworks is
philosophically rather different to some
of the programming that you may have
been used to if you use the procedural
programming or scripting whatever
typically you're used to being in charge
at the top of your program you'll deal
with getting information from the HTTP
requests you'll do something with it at
the end omit HTML perhaps or that might
be the sort of development that you're
used to in a framework based environment
you're typically sitting back as a
passive receiver of messages so and
other things I've noticed in training is
this can be quite discomforting for some
people people don't or developers don't
feel that they're in control of their
application if you find yourself in that
situation again
sit back don't worry relax the reality
is you're not in control but that's a
good thing what again I hope we'll see
very swiftly is that letting web objects
take care of the heavy lifting frees you
from all of that responsibility and
allows you to concentrate on the more
interesting things which would you
rather deal with HTTP requests and
responses or the actual application
logic
I suggest that the dealing with the
application logic is going to certainly
be a more productive way of using your
time so turning first of all to the web
objects framework and we're just gonna
discuss it very little bit before going
into a couple of demos remember that web
objects the web budgets framework is the
framework and that contains the object
abstractions for the dynamic elements
you'll have on a page a page itself and
provide
the state management classes the base
method the route that I'm sorry that the
base class for webpages and indeed for
parts of webpages is woe component so as
you create web pages or new web pages
you're going to be creating new
instances or new classes subclassed from
wou component so let's illustrate that
with the first demo so quick quick
reminder here I'm not expecting here for
everybody to go away being an expert web
objects developer what I want you to get
out of this is a flip flip feeling for
what web objects development and is
about what some of the typical processes
are and so on so that subsequently if
you want to take a look at the look at
the product and start using it you can
feel comfortable having seen somebody
else do this and seeing that it's not
particularly difficult so in project
builder I'm going to start off by
creating a new web object application
that I'm going to inspiringly name hello
whoa world and I'm not going to type it
correctly and because this is a demo
let's just ignore all these parts for
the moment and go wind look at the
default configuration so just out of the
box project Xcode gives us a project
that contains a lot of stuff let's just
start off by building and running that
to see what happens
well it goes away builds runs and for
your convenience it starts up and
displays the webpage at the moment
there's nothing in it
that's probably unsurprising we haven't
done anything what do we have to do to
actually configure this and get
something to display
the page that we're seeing first of all
is the main page so out of the box the
project template provides us with a main
component which comprises a number of
different files including this main dot
woh thing which is the template for the
first page if I open that in web objects
builder straightaway I hope you can see
that where budgets builder is a
graphical HTML editing tool into which I
can type and if I build and run the
application again since that's the first
page that's displayed in the fullness of
time I get the text but I wanted that's
not particularly exciting I said that
web obvious was it was a dynamic
development environment so it's not
particularly dynamic content let's
suppose we want to at least display some
semi dynamic content I can replace whoa
world with a whoa string a whoa string
is a dynamic element at runtime that
string is going to be substituted by
whatever value we associated with that
string let's say oops that I want to add
to my page a variable called user name
to represent the user's name let's see
what happens when I click Add here if I
go to my Java code we'll see that I've
got a class called main which is the
object that underlies this page when
it's generated that currently just has a
constructor method when I say add once
add some access of methods as well
you'll see I get an instance variable
added and suitable access and methods in
web objects builder I also get user name
added here
as a variable from which I can drag this
is now set the value for this string to
be whatever username happens to be at
runtime in my constructor let's say then
I set that to be something not
particularly dynamic oops and build and
run the application
I get some sort of dynamic content I'm
going to pass on for the moment figuring
out how we can actually get the user's
name directly is going to come in the
next session so let's for now see how
can we get something a little bit more
dynamic than that let's suppose we want
to display the current time again we're
going to use a wou string and add
another key for current time in this
case though this is one thing that just
lot pause and think about for a moment
in this case I'm only going to define
and access a method this is an if add a
read-only value I don't need to have an
instance variable for this I just need a
means of generating this value so I'm
going to have a get method current time
which is going to return an NS timestamp
and I can connect that up and without
going into too much detail here
associate a date format with that so
that specifies how this how the time is
going to be represented on the page in
my current time method then rather than
returning an instance variable I'm going
to return a new NS timestamp so when a
new NS timestamp is created with no
additional constructor arguments it'll
be set to the current date and time so
if I build that
we actually do have some dynamic content
I hope you'll agree that that was fairly
straightforward in particular a couple
of things to notice there we're sitting
back being the passive recipient of
messages so this illustrates the
framework aspect we're actually doing
anything very active in particular has
anybody spotted where the HTTP requests
an HTTP response come into this we
haven't seen any hot hide or hair of
them we're not bothered we can get to
them if we want to but typically we
don't have to I can still make this a
little bit more dynamic without going
into too much more detail at this stage
let's suppose that rather than just
displaying hello on the first page I
want to display a hyperlink the
hyperlink is going to take us to a new
page hyperlink you might guess straight
off is represented by an instance or a
dynamic element woah hyperlink if I
select in this toolbar I surround the
text that I've selected with a hyperlink
by clicking on the hyper hyperlink
button a hyperlink is associated with an
action so to my web page I'm going to
add an action this time rather than a
key the action method is going to be
called go to welcome and it's going to
take us to the Welcome page now this is
important because I'm getting a step
ahead of myself here deliberately I want
to watch what happens when I add this
here go - welcome gets added to the list
of variables because web objects builder
doesn't know any better at this stage
I'm gonna specify that the action oops
sorry
the action associated with the hyperlink
is go to welcome and save that and now
I'm going to add
the Welcome component
to my project
on this page I do want user name and it
means of setting it because then to say
welcome user name if you watched or if
you're watching very carefully when I
saved that go to welcome in Maine
suddenly appeared bahut below a new line
that's appeared web objects builder
differentiates between variables in a
page and action methods action methods
appear below the line web objects is all
that well we'll just builders figured
out that welcome is actually a web
objects component so a method that
returns a web objects component page and
takes no methods is counted as an action
method notice in the action method that
was added to the Java file I get a
helpful hint here initialize your
component if there's an initialization
that I want to do I can do that after I
created the instance of the page I'm
about to go to so I can simply pass a
variable to the next page
thank you
so again we get our sort of dynamic
content click me and I get taken to the
next page with some new information on
it so that was a very quick run-through
let's have a quick recap of what it was
that we saw first of all actually let's
just illustrate one other pink point
here I sort of mentioned that a
component comprises a number of
different files there are three files
that were primarily interested in in a
web objects page first of all the HTML
that defines the template for the page
the Java is the under the Java file is
the underlying business logic for that
page and then there's a wad file the wad
file is what defines the mapping between
what's in the dynamic elements and
what's in the code the main component is
created by web objects automatically
points to make about the HTML file
firstly remember that web objects tries
to play well with others just because
you're using web objects does not mean
that you can't use other web-based
technologies as well so if you want to
use CSS if you want to use JavaScript or
whatever you can use those technologies
and web widgets based application the
only thing that's different about a web
widgets page is it includes a web object
a set of web budgets tags to represent
the dynamic elements the dynamic
elements that we've seen so far
whoa string and whoa hyperlink later on
we'll see were women he's got a
collection of attributes each of which
you can set graphically using web
objects builder the Java file then is
the business logic you can put whatever
business whatever the instance variables
you want into the page you can put
whatever methods you want them to the
page and so on
the only difference about a sum of the
page some similar methods is some
methods may be action methods action
methods have no arguments and return a
subclass of an instance of a subclass of
whoa component these are what you use
for navigation the wad file then is a
just simple text file and that binds the
two together that specifies what
attribute of the Java page corals is
is used for what attribute such as the
value for example of a string to go into
some new areas then let's have a look at
state management typically when you deal
with a web-based application you're
interested in managing users Co for an
individual when a person comes to your
website you want to keep track of them
so you can find out what products are
interested in buying and so on sometimes
you need to maintain application wide
state so where objects provides you with
abstractions of both of these as you
might expect from the names here a
session represents the current user
which you can access from a page using a
static method session which simple
returns of the current session whatever
it happened whoever it happens to be
says note so you don't have to do any
work in figuring out who the current
user is similar sort of thing with the
application object if you wan to add
States to the application Clark or
object you can access the application
object from any page using the
application static method of the
application class if you're dealing with
dynamic content you typically need some
means of retrieving information from the
user so as you might expect from a
web-based application we have
abstractions for a form on a web page
and text fields and so on it'll be able
to retrieve information from that from
the user we also have a submit button so
wow so mitt button that you can use for
it to allow the user to submit their
information and again let's straighten
that in a demonstration so going back to
our previous application rather than
just click me I want to add a form
where I asked the user to add their name
or tell us what their name is so I've
added a whoa form to the page at is a
text field and I'll have a submit button
the submit button may as well go to go
to welcome this time however I'm not
going to collect that information on the
current page username is probably
something I want to collect a delivery
application sorry I pick my level of a
session so to my session class I'm going
to add a key user name that's a string
and bind the value of the text field to
the sessions user name now as I have
that I can rather happily remove code
I don't now need to pass the user name
from the first page the second moreover
I don't now get into the user name from
the second page that I'm going to I'm
going to get it directly from the
session so
again build and run
pretty straightforward I know that was
quick you're probably I suspect that
half of you go what did he just do there
so reminder I've got to keep reminding
myself this as well I'm not training you
I'm giving you a feel for what the
development environment is like I hope
that either today or tomorrow you'll get
a collection of all of these examples on
the downloadable disk image so if you
haven't been following just now you'll
have the examples to have a look out
later plus as a wealth of documentation
already available online so where do we
go from here if we have a look at a
little bit more about some of the other
dynamic elements that we can use there's
a whole range of different dynamic
elements that web objects provides you
with so in addition just two strings
hyperlinks and so on in particular a
repetition to allow you to iterate over
the contents of an array conditional to
allow you to specify whether or not
barring a dependence on what the current
circumstances are a particular part of a
web page gets displayed we have support
for checkboxes radio buttons and so on
and in particular then we have support
for partial components so the main page
that we've been created creating so far
or the Welcome page could actually be
sub parts of a page that we can reuse in
different applications or in different
apps or on different pages one of the
more exciting ones in some respects is
the woe component content which I add
you to have a look at let's some stage
which can represent the template for a
page so we actually have done dynamic
templates for our pages as well the only
one I want to have a look at just for
now is the whoa repetition to illustrate
how that can be used to iterate over the
contents of an array in again another
demo this time starting
with an existing project so in this
project I've already defined a session
class that creates for me an array of
person objects in my first page I want
to display their list of all of the
people in that array and I'll do that by
adding a whoa repetition which will
iterate over all of the people in that
array to be able to iterate over those I
need a variable which will be set to
each of those in turn and that's going
to be a variable of type person as you
don't need access and methods so for
each person in turn I want to display
their full name your build and run that
application then
for some reason there's a permissions
problem actually no which I had checked
beforehand of it there you go
tips
let's try that again
so
I get a list of all of the people in the
array that's not particularly exciting
by itself
how about allowing the user to select
one of those I can surround the person's
name or each person's name with a
hyperlink to just remind myself select a
person so the action method that will be
invoked when the user clicks on the
hyperlink is select person what that
will do is go to the next page is going
to be selection and pass current person
as a selected person the selection page
you can probably imagine is going to
display the name of the selected person
so I'm passing information from one page
to the next
so dealing with iterations or
repetitions within web objects turns out
to be very simple if you think in terms
of objects final thing just to add here
to add a little bit of substance to it
let's add the ability to display a
graphic so another access a method
that's a read-only value that simply
returns the person's name with the
suffix jpg which I can then bind to the
file name of a wou image in the
resources for my project I've got a
series of images
so when I select an individual I can see
their picture and so on so I hope you're
getting the feeling from this already
that it's a fairly easy development
environment in particular very
interactive so there's not a big there's
not a big turnaround time you can very
quickly see the results of what it is
that you're doing the final thing that I
want to come on to is what many people
regard as the crown jewels of this
technology the database access layer
database access as I mention is mediated
through the enterprise objects framework
AOF as perhaps sometimes confusingly its
referred to is the collection of objects
that mediate between what's in the
database and the objects in your
application so loosely speaking you can
say that a record or a row in your
database is going to correspond to an
object in your application in your
database you have tables and either
records or rows depending on the
terminology in your application you have
classes and instances of classes VOF
is what map's the two things together
typically you're dealing at a high level
of abstraction one thing I want to
re-emphasize here and something that
applies throughout web objects is that
even though you're using typically a
high level of that FF abstraction you
are not precluded from using lower level
details should you wish to do so so if I
unwrap the double negative there even
though you're using web objects you can
still use raw sequel if you want you can
still use JDBC you can still get at the
header the HTTP header information and
so on
if you want to if you feel that you need
to the other vital thing about AOF is
that it provides a great degree of
management for you I said loosely
speaking an object in your application
corresponds to a record in the database
or vice versa and because if I simply
say that a record in the database
corresponds to an object in your
application there's a temptation to
think from that that your entire
database is going to end up in memory
that is not the case one of the things
that the AF is particularly good at that
those of you who anybody go to the core
data presentation just now when andreas
spoke just now about faulting the same
technology applies to EOF you only
retrieve into memory using EOF those
records that you actually need so if
you're collecting all of the purse or a
selection of persons from a person table
in your database
you only get enterprise objects created
for those persons that you retrieve if a
person has a relationship to a
department until such time as you ask a
person for the department to which they
belong the department record is not
fetch from the database so AOF puts
bounds for you on the object graph that
you create in particular we'll see
illustrated in a couple of slides time
eyo F also manages relationships between
objects for you how many people do
database access who uses sequel here ok
so you're probably familiar with the
concepts of primary key foreign key and
so on we'll see that the way that
relationships that were represented in
objects is very different to the way
they represented in a database and EF
needs to do some extra work for you how
does it do its work well again for those
of you who are at Andreas's session
it relies ultimately on a model
of the key parts of developing your
application is it's at the outset you
should create an ER model that defines
the mapping between what's in your
application what's in the database you
can also create custom classes to
represent your data objects you don't
have to if you don't want to EF provides
for you a neo generic record class which
is able to represent any sort of entity
typically however in order to add value
you'll do some customization it also
then provides you with a whole
infrastructure for actually accessing
the database for making changes to
objects and then saving those changes
back to the database should you wish
ultimately as I mentioned though
everything is basically founded upon the
EO model the ER model you edit
graphically using the ER model a tool
which I'm not going to demonstrate now
right so the picture should show you
that basically is an entity relationship
modeling tool with it you can specify
class what class in your application is
going to be used to represent what table
in your database and what columns in a
given table are going to be used as
instance variables in a given class
moreover you specify what the primary
key is or the primary keys are for any
given entity and what their what
variables are actually going to
represent in a class so if you've got a
collection so so for example you don't
have to use all of the columns from a
given from a given table if you don't
want to in your application your classes
are that your database rows or records
are represented by instances of
enterprise objects so we have yo
jennette record and I mention on an
earlier slide our custom object yo
generic record is rather like a date
addiction rates capable of representing
any sort of entity
as a historical footnote it happens to
inherit from yo custom object in in your
applications you'll typically just
subclass
yo generic record should you wish to do
so in a subclass you can add whatever
custom logic whatever behavior you want
again for those of you who are at
Andreas this session this corresponds to
an s managed object in core data some of
the behavior that you might want to add
may be very straightforward if we have a
person class it may be that you simply
want to have a consistent means of
representing a person's full name so a
bit of custom logic would simply be to
implement a full name method which
returns a catenation of first name and
last name if you want to edit enterprise
objects thinking back to what I said
about weblog just as a technology as a
whole
you're just been dealing with objects
what do you want what do you do when you
want to edit at any other object in Java
simply send a message if you remember
also what I said about key value coding
you can edit a given Enterprise object
by simply sending using key value coding
methods such as take value for key so
you pass in as arguments to take value
for key method the new value and the
name of the attribute that you want to
change as a string or if you've
implemented your own custom subclasses
of energy our egg record which have
custom access and methods defined you
simply call other relevant access and
Method an important point here is that a
similar sort of thing works when you're
manipulating relationships as when
you're manipulating straightforward
attributes where objects even handles
many to many relationships for you for
that so for the DBAs amongst you who had
to deal with join tables up until now
you do not have to deal with join tables
using the AF it's all handled for you
for you prove
Yuda specify the model correctly so if
you're using if you have a relationship
say between a movie entity and a studio
entity and you want to set the movie so
it's the studio to which a movie belongs
if you had a custom subclass of a
generic record you might simply send a
set studio message to give an instance
of a movie class of a movie or the movie
class there's a little bit extra work
that you might have to do if you have
too many relationships for too many
relationship to set the movies for a
given studio you might have a custom
methods add to and then name of
relationship and remove from named
relationship one of the issues here
though relates back to the issue that I
mentioned about the way that
relationships are represented in object
versus the way that they're represented
in your database if you remember in a
database a relationship is modeled by
having a foreign key in a table in the
object world things are a little bit
different with objects we have
references whether it be directly to an
instance of the destination of the
relationship or a reference to an array
that contains a collection of instances
of the destination for a relationship
therefore if we want to assign in this
example an employee to a different
department our employee Jackson happens
currently to belong to the events
department if we simply tell Jackson
that you're now in sales if you think
about this in terms of the real world if
all that we do is tell Jackson his
current department doesn't know that
he's been reassigned
moreover the new department is going to
doesn't know that he's being reassigned
we have to explicitly mattel each of the
two departments about the move it's a
similar thing in objects on the database
all that we will do is change Jackson
that their foreign key in a table in
objects we have to be more explicit and
actually send messages to the other two
departments if I go back one of things
that I skipped over at the end of this
slide was it is actually a rather long
method name and object to both sides of
the relationship with key which it first
is rather intimidating when you start
using well objects you'll learn to like
it because what it does is it manages
all of this stuff for you so for those
of you who are DBAs or have dealt with
databases in the past this is a fairly
high degree of abstraction you don't
have to worry about primary keys you
don't have to worry about foreign keys
you don't have to worry about
maintenance of relationship integrity of
relationships EF manages all of that for
you a lot of that's managed for you by
the editing context the editing context
is can be thought of basically as a sort
of scratch pad it's the object that
mediates between you and the underlying
connection to the database so it's the
object that you turn to when you want to
fetch stuff from the database and safe
and safe stuff back to the database
it also observes all of the objects
you've retrieved from the database looks
for changes to them and then in
particular allows you to undo those
changes should you wish you can get hold
of it then add in contacts right each
session as you might expect so every
users wants to have their own collection
of products that they're buying in their
own shopping cart and so on each session
comes along with their own its own
default editing context that you
typically turn to when you want to do
something like fetch information from
the database
so to fetch information from the
database you have to specify what it is
that you're after you specify it using
an object again known as a fetch
specification sending a message
objects with specification to the
editing context returns to you an array
of enterprise objects that match the
filter that you set for that specific
specification so let's suppose we have
our database full of movie information
we can ask an editing context to ask the
database through the intervening objects
in ef4 for example all of the movies
whose title big contains the word Nemo
we might also add that we want to have
the objects returned sorted by title so
we simply send a message objects the
Fest pacification past the parameter
this fetch specification that we
specified to the editing context it
returns then a collection of objects the
fetch specification itself contains in
for all the information that a of'
requires to determine what's going to be
got from database minimally we must
specify the name of the entity so what
table is in effect that we're interested
in we might also want to narrow our
search so maybe we would say as in this
case we're only interested in movies
whose title contains the word Nemo if we
don't specify a qualifier we'll get back
everything from that table we might also
specify an array of sort ordering so we
may won't want to get the movies back
order first of all by title then by
revenue then by studio or whatever so as
an example a bit of code this bit of
code is a little bit dense so apologies
for that but this bit of code basically
does all of that for us so first of all
get hold of an editing context in the
first line create a new qualifier create
a new set of sort or rings it and it
contains a single sort ordering here
and then create our new face
specification object and send that to
our editing context so didn't give you
very long to have a look at that but
I've got a prepackaged demo that does
that
you'll notice that I've already gotten
the Oh model in my application so
I've already set up a repetition it's
going to iterate over a collection of
movies one at a time and display the
name of the studio in this case that I'm
interested in for no particular reason
if I have a look at the code the code is
basically the same as in essence the
code that I just displayed except in
this case because the database if I
happen to be using doesn't contain Nemo
anywhere and we're going to be looking
for studios named or movies from studios
named MGM there's a subtle point here
which I'll come back to in a moment and
I'm going to get hold of the editing
context create a qualifier create a set
of sort orderings f8 specification and
then use that fetch specification this
again
I'm sorry this is rather tiresome
there's gonna be more kind thank you
that's more interesting still
now I'm going to guess that this has
something to do with some of the another
setup that was done after I set my stuff
up
what's happening is that for some reason
I can't actually connect to the database
and I suspect that it has something to
do with the IP setup and it was done
afterwards
such is life and I don't think that
there's anything that I can quickly do
to rectify that which is shall we say
unfortunate and I'm guessing from the
lack of responses there that that's the
answer so I hope you will forgive me
that's going to be even less impressive
a demo than we might have expected if
you could imagine that rather than a
collection of error messages this
webpage displayed a list of all of the
movies that we'd fetched I hope you
might agree that had it worked this is
actually not very many lines of code to
go to a database fetch some information
and collect a series of records back
from the database the subtle point that
I wanted to make here that I said I was
just glossing over a little bit was
notice that in our qualifier which
specified that we were interested in the
studio name for the movies so rather
than just searching by title which is
searching within the single table that
we were interested in
we're actually traversing a relationship
here so we're traversing the
relationship from a movie to the studio
to which that movie belongs and looking
to see what what the name is of that
studio so very easily which reversing
relationships in this query as well not
particularly difficult so for those of
you who have used other environments
particularly anybody here used EJB
anybody care to do a coat light lines of
code comparison between what we should
have achieved here had the database
connection been up and what you would
had to do an EJB fewer lines of code
here at least I'm going if honestly
there's somebody nodding in the front
row so with that I said it was going to
be a very very quick run-through
the goal here was just to give you some
sort of a taste of what it's like to do
web objects development and as you can
see there's occasional frustrations with
it
but I hope that you might also see that
it compared with some environments that
you're used to it's actually quite fluid
very responsive and in particular
there's not a lot of not many lines of
code to actually achieve quite an
interesting result the examples that
I've been through I will get up on the
connector a whole calm sight as quickly
as possible after this session it may be
tomorrow evening that it goes up in the
interim though there's plenty of
documentation available online so we're
all jizz is very thoroughly documented
basically the main place that I would
suggest that you start with is the aptly
named getting started page