Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
This video introduces the service-DAO design pattern in which we code data access objects (DAOs) to interact directly with our database (via Hibernate), and services to facilitate communication between clients (controllers in our case) and our underlying data.
During the last video,
we looked at a bunch of principles,
0:00
including the separation of concerns,
single responsibility, and loose coupling.
0:03
All of that can seem really
abstract without an example.
0:08
Let's now take a peek at what it means for
us in Giflib.
0:11
[SOUND] In Giflib, we'll split our
application into three layers,
0:15
the web layer, data access layer,
and service layer.
0:19
[SOUND] The web layer will consist of the
controllers we've already started as well
0:24
as anything relating specifically
to serving web content.
0:28
The data access layer we'll name DAO,
which stands for Data Access Object.
0:32
Here we'll write one DAO class for
each entity class.
0:38
And its sole purpose will be to
interact directly with Hibernate.
0:42
This will be the place where we
code all of our CRUD operations.
0:46
Finally, we'll have a service layer.
0:50
[SOUND] This layer will receive requests
from a service client, which in our case
0:53
is a controller, and it'll make any
calls to the DAO that are necessary.
0:57
This layer could also
perform any computations or
1:02
processing that are outside
the realm of our model objects.
1:05
Our layers will be structured as
packages and we'll keep relationships
1:09
between the packages tightly
controlled and loosely coupled.
1:12
What this means is that any time there's
an interaction between the layers,
1:16
that interaction will take place
via interfaces, not classes.
1:20
As we get the structure in place,
you may see a redundancy in code.
1:26
We'll create an interface and
1:30
a class implementing that interface for
each entity in our application.
1:31
Know that this apparent repetition
is in our best interest as we code
1:36
an application that is more maintainable,
extensible, reusable, and testable.
1:40
You need to sign up for Treehouse in order to download course files.
Sign up