You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by MG...@escholar.com on 2010/03/30 22:51:13 UTC
Cayenne pattern quesiton
When using Cayenne are DAO's still the pattern used for findAll,
findByProperty, and findById query methods or is there some better way to
do this in Cayenne? For example in the pet store project I see DAO's
being implemented, but I know that example was taken from other projects
that use DAO's to begin with so I'm not sure if it was just retrofitted to
use DAO's with Cayenne. I tried to look at some other examples from the
website, but most of those are either not complex enough to warrant that
kind of structure or the links are dead. I hope this is clear. :) Also
if DAO's are the recommended pattern to use is there a tool (i.e. eclipse
plugin) that will generate them?
Thanks.
-Mike
Re: Cayenne pattern quesiton
Posted by Mike Kienenberger <mk...@gmail.com>.
I used DAOs in my last set of Cayenne projects, and other than dealing
with injecting the DAO into the methods needing them, it worked really
well. I went with a central ServiceLocator pattern to a DAOFactory to
deal with that situation as we had no IoC providers.
I used templates to generate 90% of the DOA methods, and used a
generation gap pattern to add in custom find methods when needed.
As for the usefulness, a DAO as an interface and injected resource
makes it incredibly simple to write unit tests.
I'm now working on a JPA project where we are using the static methods
on entity classes, and it's pretty much impossible to write easy unit
tests. Instead I have to try to mock up pieces of the JPA layer
instead, which is orders of magnitude more difficult. It didn't help
that we dropped entities-as-interfaces at the same time :)
On Tue, Mar 30, 2010 at 5:32 PM, Robert Zeigler
<ro...@roxanemy.com> wrote:
> Daos can be useful in certain contexts. In particular, if you have some
> means of automagically generating the dao. :)
> For instance, on a Tapestry5 + hibernate-based project I'm working on right
> now, there's a DaoSource service.
> What's more, is there's an @InjectDao annotation that plays nicely with T5's
> pages and components.
> So you can do something like:
>
> @InjectDao(User.class)
> Dao<User> userDao;
>
> public List<User> getAllUsers() {
> return userDao.listAll();//or something like this.
> }
>
> The key piece being the fact that Dao<User> wasn't code you have to write
> (or even generate).
>
> It can make things convenient, but, like Michael and John, I tend away from
> Dao's.
>
> Robert
>
> On Mar 30, 2010, at 3/304:26 PM , John Armstrong wrote:
>
>> My latest project has a lot of DAO going on and while it made sense (for
>> some reason, habit?) at first I regret going that way and will, over time,
>> be migrating into the same pattern that Michael highlights. Its really
>> just
>> a big useless layer for all the reasons he highlights, at least in my
>> context.
>>
>> John-
>>
>> On Tue, Mar 30, 2010 at 2:20 PM, Michael Gentry
>> <mg...@masslight.net>wrote:
>>
>>> Hi Mike,
>>>
>>> I personally tend to not create DAOs for Cayenne. If I need a findBy*
>>> type method, I just add it to the Cayenne-generated class as a static
>>> method. (For example, I have a User.withUsernameAndPassword() static
>>> method in my User class.) For findById, you can use DataObjectUtils
>>> directly if you like:
>>>
>>> http://cayenne.apache.org/doc/api/org/apache/cayenne/DataObjectUtils.html
>>> (objectForPK)
>>>
>>> Of course, this is just what I tend to do. Your mileage and that of
>>> others will likely vary. To me, part of the concept of the DAO is
>>> that you can transparently and magically change the DAO code to use a
>>> different ORM and if everything else is using the DAOs, it'll all just
>>> work. Of course, the assumes that every single ORM works exactly the
>>> same way (they don't) and it also means you are choosing your ORM for
>>> failure (because you expect the need to swap them out). Cayenne works
>>> differently than Hibernate which works differently than other ORMs. I
>>> choose an ORM for the features it gives me. To me it doesn't make as
>>> much sense to code with the DAOs (especially when they limit the use
>>> of your ORM), but I'm sure others will disagree with me. :-)
>>>
>>> mrg
>>>
>>>
>>> On Tue, Mar 30, 2010 at 4:51 PM, <MG...@escholar.com> wrote:
>>>>
>>>> When using Cayenne are DAO's still the pattern used for findAll,
>>>> findByProperty, and findById query methods or is there some better way
>>>> to
>>>> do this in Cayenne? For example in the pet store project I see DAO's
>>>> being implemented, but I know that example was taken from other projects
>>>> that use DAO's to begin with so I'm not sure if it was just retrofitted
>>>
>>> to
>>>>
>>>> use DAO's with Cayenne. I tried to look at some other examples from the
>>>> website, but most of those are either not complex enough to warrant that
>>>> kind of structure or the links are dead. I hope this is clear. :)
>>>> Also
>>>> if DAO's are the recommended pattern to use is there a tool (i.e.
>>>> eclipse
>>>> plugin) that will generate them?
>>>>
>>>> Thanks.
>>>> -Mike
>>>>
>>>
>
>
Re: Cayenne pattern quesiton
Posted by Robert Zeigler <ro...@roxanemy.com>.
Daos can be useful in certain contexts. In particular, if you have
some means of automagically generating the dao. :)
For instance, on a Tapestry5 + hibernate-based project I'm working on
right now, there's a DaoSource service.
What's more, is there's an @InjectDao annotation that plays nicely
with T5's pages and components.
So you can do something like:
@InjectDao(User.class)
Dao<User> userDao;
public List<User> getAllUsers() {
return userDao.listAll();//or something like this.
}
The key piece being the fact that Dao<User> wasn't code you have to
write (or even generate).
It can make things convenient, but, like Michael and John, I tend away
from Dao's.
Robert
On Mar 30, 2010, at 3/304:26 PM , John Armstrong wrote:
> My latest project has a lot of DAO going on and while it made sense
> (for
> some reason, habit?) at first I regret going that way and will, over
> time,
> be migrating into the same pattern that Michael highlights. Its
> really just
> a big useless layer for all the reasons he highlights, at least in my
> context.
>
> John-
>
> On Tue, Mar 30, 2010 at 2:20 PM, Michael Gentry
> <mg...@masslight.net>wrote:
>
>> Hi Mike,
>>
>> I personally tend to not create DAOs for Cayenne. If I need a
>> findBy*
>> type method, I just add it to the Cayenne-generated class as a static
>> method. (For example, I have a User.withUsernameAndPassword() static
>> method in my User class.) For findById, you can use DataObjectUtils
>> directly if you like:
>>
>> http://cayenne.apache.org/doc/api/org/apache/cayenne/DataObjectUtils.html
>> (objectForPK)
>>
>> Of course, this is just what I tend to do. Your mileage and that of
>> others will likely vary. To me, part of the concept of the DAO is
>> that you can transparently and magically change the DAO code to use a
>> different ORM and if everything else is using the DAOs, it'll all
>> just
>> work. Of course, the assumes that every single ORM works exactly the
>> same way (they don't) and it also means you are choosing your ORM for
>> failure (because you expect the need to swap them out). Cayenne
>> works
>> differently than Hibernate which works differently than other
>> ORMs. I
>> choose an ORM for the features it gives me. To me it doesn't make as
>> much sense to code with the DAOs (especially when they limit the use
>> of your ORM), but I'm sure others will disagree with me. :-)
>>
>> mrg
>>
>>
>> On Tue, Mar 30, 2010 at 4:51 PM, <MG...@escholar.com> wrote:
>>> When using Cayenne are DAO's still the pattern used for findAll,
>>> findByProperty, and findById query methods or is there some better
>>> way to
>>> do this in Cayenne? For example in the pet store project I see
>>> DAO's
>>> being implemented, but I know that example was taken from other
>>> projects
>>> that use DAO's to begin with so I'm not sure if it was just
>>> retrofitted
>> to
>>> use DAO's with Cayenne. I tried to look at some other examples
>>> from the
>>> website, but most of those are either not complex enough to
>>> warrant that
>>> kind of structure or the links are dead. I hope this is
>>> clear. :) Also
>>> if DAO's are the recommended pattern to use is there a tool (i.e.
>>> eclipse
>>> plugin) that will generate them?
>>>
>>> Thanks.
>>> -Mike
>>>
>>
Re: Cayenne pattern quesiton
Posted by John Armstrong <si...@gmail.com>.
My latest project has a lot of DAO going on and while it made sense (for
some reason, habit?) at first I regret going that way and will, over time,
be migrating into the same pattern that Michael highlights. Its really just
a big useless layer for all the reasons he highlights, at least in my
context.
John-
On Tue, Mar 30, 2010 at 2:20 PM, Michael Gentry <mg...@masslight.net>wrote:
> Hi Mike,
>
> I personally tend to not create DAOs for Cayenne. If I need a findBy*
> type method, I just add it to the Cayenne-generated class as a static
> method. (For example, I have a User.withUsernameAndPassword() static
> method in my User class.) For findById, you can use DataObjectUtils
> directly if you like:
>
> http://cayenne.apache.org/doc/api/org/apache/cayenne/DataObjectUtils.html
> (objectForPK)
>
> Of course, this is just what I tend to do. Your mileage and that of
> others will likely vary. To me, part of the concept of the DAO is
> that you can transparently and magically change the DAO code to use a
> different ORM and if everything else is using the DAOs, it'll all just
> work. Of course, the assumes that every single ORM works exactly the
> same way (they don't) and it also means you are choosing your ORM for
> failure (because you expect the need to swap them out). Cayenne works
> differently than Hibernate which works differently than other ORMs. I
> choose an ORM for the features it gives me. To me it doesn't make as
> much sense to code with the DAOs (especially when they limit the use
> of your ORM), but I'm sure others will disagree with me. :-)
>
> mrg
>
>
> On Tue, Mar 30, 2010 at 4:51 PM, <MG...@escholar.com> wrote:
> > When using Cayenne are DAO's still the pattern used for findAll,
> > findByProperty, and findById query methods or is there some better way to
> > do this in Cayenne? For example in the pet store project I see DAO's
> > being implemented, but I know that example was taken from other projects
> > that use DAO's to begin with so I'm not sure if it was just retrofitted
> to
> > use DAO's with Cayenne. I tried to look at some other examples from the
> > website, but most of those are either not complex enough to warrant that
> > kind of structure or the links are dead. I hope this is clear. :) Also
> > if DAO's are the recommended pattern to use is there a tool (i.e. eclipse
> > plugin) that will generate them?
> >
> > Thanks.
> > -Mike
> >
>
Re: Cayenne pattern quesiton
Posted by Michael Gentry <mg...@masslight.net>.
Hi Mike,
I personally tend to not create DAOs for Cayenne. If I need a findBy*
type method, I just add it to the Cayenne-generated class as a static
method. (For example, I have a User.withUsernameAndPassword() static
method in my User class.) For findById, you can use DataObjectUtils
directly if you like:
http://cayenne.apache.org/doc/api/org/apache/cayenne/DataObjectUtils.html
(objectForPK)
Of course, this is just what I tend to do. Your mileage and that of
others will likely vary. To me, part of the concept of the DAO is
that you can transparently and magically change the DAO code to use a
different ORM and if everything else is using the DAOs, it'll all just
work. Of course, the assumes that every single ORM works exactly the
same way (they don't) and it also means you are choosing your ORM for
failure (because you expect the need to swap them out). Cayenne works
differently than Hibernate which works differently than other ORMs. I
choose an ORM for the features it gives me. To me it doesn't make as
much sense to code with the DAOs (especially when they limit the use
of your ORM), but I'm sure others will disagree with me. :-)
mrg
On Tue, Mar 30, 2010 at 4:51 PM, <MG...@escholar.com> wrote:
> When using Cayenne are DAO's still the pattern used for findAll,
> findByProperty, and findById query methods or is there some better way to
> do this in Cayenne? For example in the pet store project I see DAO's
> being implemented, but I know that example was taken from other projects
> that use DAO's to begin with so I'm not sure if it was just retrofitted to
> use DAO's with Cayenne. I tried to look at some other examples from the
> website, but most of those are either not complex enough to warrant that
> kind of structure or the links are dead. I hope this is clear. :) Also
> if DAO's are the recommended pattern to use is there a tool (i.e. eclipse
> plugin) that will generate them?
>
> Thanks.
> -Mike
>
Re: Cayenne pattern quesiton
Posted by Erdinc <ko...@yahoo.com>.
I agree. My favorite method is to use Domain Driven Design techniques.
________________________________
From: Malcolm Edgar <ma...@gmail.com>
To: user <us...@cayenne.apache.org>
Sent: Thu, April 1, 2010 6:32:05 AM
Subject: Re: Cayenne pattern quesiton
I would argue against putting finder methods in the generated
entities, as I think it is a separate concern and you can easily end
up with tightly coupled code which is difficult to refactor.
The "Rich Domain" model can end up becoming the "Blob" anti-pattern.
I don't think stateful entities objects should be be mixed up with
static finder methods.
regards Malcolm Edgar
On Wed, Mar 31, 2010 at 6:10 PM, Andrus Adamchik <an...@objectstyle.org> wrote:
> Also I suspect I am not using a classic version of DAO. My DAO's are morphed
> with the "business layer" and can have interactions with other subsystems,
> not just Cayenne (e.g. caching, data access via web services, etc.). Also
> they are not auto-generated, as any new methods are created when a need in
> them arises in the application. The idea is to keep it tight and avoid
> having any unused and unneeded methods.
>
> Andrus
>
> On Mar 31, 2010, at 10:02 AM, Andrus Adamchik wrote:
>
>> I am using a form of DAOs (with injection) with the main reason being that
>> I can provide alternative implementations in a context-sensitive manner
>> without changing the DAO interface (something that can't be done with the
>> static methods), which is very handy in numerous situations.
>>
>> Andrus
>>
>>
>> On Mar 30, 2010, at 11:51 PM, MGargano@escholar.com wrote:
>>>
>>> When using Cayenne are DAO's still the pattern used for findAll,
>>> findByProperty, and findById query methods or is there some better way to
>>> do this in Cayenne? For example in the pet store project I see DAO's
>>> being implemented, but I know that example was taken from other projects
>>> that use DAO's to begin with so I'm not sure if it was just retrofitted
>>> to
>>> use DAO's with Cayenne. I tried to look at some other examples from the
>>> website, but most of those are either not complex enough to warrant that
>>> kind of structure or the links are dead. I hope this is clear. :) Also
>>> if DAO's are the recommended pattern to use is there a tool (i.e. eclipse
>>> plugin) that will generate them?
>>>
>>> Thanks.
>>> -Mike
>>
>>
>
>
Re: Cayenne pattern quesiton
Posted by Malcolm Edgar <ma...@gmail.com>.
I would argue against putting finder methods in the generated
entities, as I think it is a separate concern and you can easily end
up with tightly coupled code which is difficult to refactor.
The "Rich Domain" model can end up becoming the "Blob" anti-pattern.
I don't think stateful entities objects should be be mixed up with
static finder methods.
regards Malcolm Edgar
On Wed, Mar 31, 2010 at 6:10 PM, Andrus Adamchik <an...@objectstyle.org> wrote:
> Also I suspect I am not using a classic version of DAO. My DAO's are morphed
> with the "business layer" and can have interactions with other subsystems,
> not just Cayenne (e.g. caching, data access via web services, etc.). Also
> they are not auto-generated, as any new methods are created when a need in
> them arises in the application. The idea is to keep it tight and avoid
> having any unused and unneeded methods.
>
> Andrus
>
> On Mar 31, 2010, at 10:02 AM, Andrus Adamchik wrote:
>
>> I am using a form of DAOs (with injection) with the main reason being that
>> I can provide alternative implementations in a context-sensitive manner
>> without changing the DAO interface (something that can't be done with the
>> static methods), which is very handy in numerous situations.
>>
>> Andrus
>>
>>
>> On Mar 30, 2010, at 11:51 PM, MGargano@escholar.com wrote:
>>>
>>> When using Cayenne are DAO's still the pattern used for findAll,
>>> findByProperty, and findById query methods or is there some better way to
>>> do this in Cayenne? For example in the pet store project I see DAO's
>>> being implemented, but I know that example was taken from other projects
>>> that use DAO's to begin with so I'm not sure if it was just retrofitted
>>> to
>>> use DAO's with Cayenne. I tried to look at some other examples from the
>>> website, but most of those are either not complex enough to warrant that
>>> kind of structure or the links are dead. I hope this is clear. :) Also
>>> if DAO's are the recommended pattern to use is there a tool (i.e. eclipse
>>> plugin) that will generate them?
>>>
>>> Thanks.
>>> -Mike
>>
>>
>
>
Re: Cayenne pattern quesiton
Posted by Andrus Adamchik <an...@objectstyle.org>.
Also I suspect I am not using a classic version of DAO. My DAO's are
morphed with the "business layer" and can have interactions with other
subsystems, not just Cayenne (e.g. caching, data access via web
services, etc.). Also they are not auto-generated, as any new methods
are created when a need in them arises in the application. The idea is
to keep it tight and avoid having any unused and unneeded methods.
Andrus
On Mar 31, 2010, at 10:02 AM, Andrus Adamchik wrote:
> I am using a form of DAOs (with injection) with the main reason
> being that I can provide alternative implementations in a context-
> sensitive manner without changing the DAO interface (something that
> can't be done with the static methods), which is very handy in
> numerous situations.
>
> Andrus
>
>
> On Mar 30, 2010, at 11:51 PM, MGargano@escholar.com wrote:
>> When using Cayenne are DAO's still the pattern used for findAll,
>> findByProperty, and findById query methods or is there some better
>> way to
>> do this in Cayenne? For example in the pet store project I see DAO's
>> being implemented, but I know that example was taken from other
>> projects
>> that use DAO's to begin with so I'm not sure if it was just
>> retrofitted to
>> use DAO's with Cayenne. I tried to look at some other examples
>> from the
>> website, but most of those are either not complex enough to warrant
>> that
>> kind of structure or the links are dead. I hope this is
>> clear. :) Also
>> if DAO's are the recommended pattern to use is there a tool (i.e.
>> eclipse
>> plugin) that will generate them?
>>
>> Thanks.
>> -Mike
>
>
Re: Cayenne pattern quesiton
Posted by Andrus Adamchik <an...@objectstyle.org>.
I am using a form of DAOs (with injection) with the main reason being
that I can provide alternative implementations in a context-sensitive
manner without changing the DAO interface (something that can't be
done with the static methods), which is very handy in numerous
situations.
Andrus
On Mar 30, 2010, at 11:51 PM, MGargano@escholar.com wrote:
> When using Cayenne are DAO's still the pattern used for findAll,
> findByProperty, and findById query methods or is there some better
> way to
> do this in Cayenne? For example in the pet store project I see DAO's
> being implemented, but I know that example was taken from other
> projects
> that use DAO's to begin with so I'm not sure if it was just
> retrofitted to
> use DAO's with Cayenne. I tried to look at some other examples from
> the
> website, but most of those are either not complex enough to warrant
> that
> kind of structure or the links are dead. I hope this is clear. :)
> Also
> if DAO's are the recommended pattern to use is there a tool (i.e.
> eclipse
> plugin) that will generate them?
>
> Thanks.
> -Mike