You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by John Huss <jo...@gmail.com> on 2011/09/21 04:37:24 UTC

Use same entity classes for server and ROP client

I'm try to find out if I can make a single app that can work either with a
local DB or using ROP with a remote DB.

For the ObjectContext it looks like as long as the code uses the
ObjectContext interface that should allow to code to run against either and
there doesn't seem to be much loss of functionality from just using the
interface.

But for the entities it looks like the client should use PersistentObject
subclasses and the server should use CayenneDataObject subclasses, and
PersistentObject is missing some useful methods.  So I'm wondering what in
CayenneDataObject prevents it from being used on the client, or what in
PersistentObject prevents it from being a good fit on the server.  What is
the best way to end up with a single set of classes that supports both?

Thanks,
John

Re: Use same entity classes for server and ROP client

Posted by John Huss <jo...@gmail.com>.
I got this to work today after some experimentation.  My app is a client app
that normally connects to a remote server, but can also run offline by
connecting to a local DB.  So the client will have the model / map files
present locally to enable the offline mode.  Since the model is available,
CayenneDataObject can be used as the common base class (with a little
work*).  CayenneDataObject is not that different from PersistentObject (it
is a subclass of it after all).  The primary difference is the presence of
the validate methods, and those require access to the DbEntity info
contained in the model.  I also was able to get a version working based on
PersistentObject for both the client and server, but I chose to stick with
CayenneDataObject since it is more standard and more full-featured.  So I
think I'm good to go now.

* The little work is replacing the entity resolver on the client with an
entity resolver obtained from a ServerRuntime (for the local offline db),
and making sure the server doesn't use the client entity resolver when
sending data to the client.  If anyone is interested I can share some more
 specific code.

John

On Wed, Sep 21, 2011 at 7:50 PM, Aristedes Maniatis <ar...@maniatis.org>wrote:

> On 21/09/11 12:37 PM, John Huss wrote:
>
>> I'm try to find out if I can make a single app that can work either with a
>> local DB or using ROP with a remote DB.
>>
>> For the ObjectContext it looks like as long as the code uses the
>> ObjectContext interface that should allow to code to run against either
>> and
>> there doesn't seem to be much loss of functionality from just using the
>> interface.
>>
>> But for the entities it looks like the client should use PersistentObject
>> subclasses and the server should use CayenneDataObject subclasses, and
>> PersistentObject is missing some useful methods.  So I'm wondering what in
>> CayenneDataObject prevents it from being used on the client, or what in
>> PersistentObject prevents it from being a good fit on the server.  What is
>> the best way to end up with a single set of classes that supports both?
>>
>
>
> We have had the exact same issues and ended up with separate classes for
> client and for server. And of course plenty of interfaces and helper classes
> to share common business logic. We are also thinking about creating a single
> user application with the server and client code merged into one
> application: when we get around to that, I think the approach will be to
> continue to use ROP, but short circuit the connection between client and
> server so it doesn't need to travel over Hessian/Jetty. Hopefully we'll be
> working on that in a month or so.
>
> Regards
> Ari
>
>
>
> --
> -------------------------->
> Aristedes Maniatis
> GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A
>

Re: Use same entity classes for server and ROP client

Posted by Aristedes Maniatis <ar...@maniatis.org>.
On 21/09/11 12:37 PM, John Huss wrote:
> I'm try to find out if I can make a single app that can work either with a
> local DB or using ROP with a remote DB.
>
> For the ObjectContext it looks like as long as the code uses the
> ObjectContext interface that should allow to code to run against either and
> there doesn't seem to be much loss of functionality from just using the
> interface.
>
> But for the entities it looks like the client should use PersistentObject
> subclasses and the server should use CayenneDataObject subclasses, and
> PersistentObject is missing some useful methods.  So I'm wondering what in
> CayenneDataObject prevents it from being used on the client, or what in
> PersistentObject prevents it from being a good fit on the server.  What is
> the best way to end up with a single set of classes that supports both?


We have had the exact same issues and ended up with separate classes for client and for server. And of course plenty of interfaces and helper classes to share common business logic. We are also thinking about creating a single user application with the server and client code merged into one application: when we get around to that, I think the approach will be to continue to use ROP, but short circuit the connection between client and server so it doesn't need to travel over Hessian/Jetty. Hopefully we'll be working on that in a month or so.

Regards
Ari



-- 
-------------------------->
Aristedes Maniatis
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A

Re: Use same entity classes for server and ROP client

Posted by Mike Kienenberger <mk...@gmail.com>.
I haven't used ROP, so I can't really speak to it.

However, you should be able to solve the issue of having a single set
of classes that supports both in a couple of different ways.

1) You can create a wrapper object which will be your single set of
classes, and have it delegate to the appropriate class.

2) You can create an interface, and have both sets of objects implement it.

In both cases, I would use code generation to create the wrapper or interface.

I have used both methods in the past.   The project I am working on
right now uses a wrapper approach for JPA and stub testing objects. In
the original project, there was a generated Cayenne wrapped object
instead of a JPA object, but the two frameworks are different enough
that maintaining the unused Cayenne classes was deemed impractical at
some point.

I think for an all Cayenne project, the interface is likely to be
easier.  I have used this methodology to write unit tests for projects
involving Cayenne, although, if I remember right, my test classes
extended from the Cayenne objects.

You will likely need to add a "getWrappedClass()" kind of method in
either case for those times when you need to pass the real class to a
query or something else.

On Tue, Sep 20, 2011 at 10:37 PM, John Huss <jo...@gmail.com> wrote:
> I'm try to find out if I can make a single app that can work either with a
> local DB or using ROP with a remote DB.
>
> For the ObjectContext it looks like as long as the code uses the
> ObjectContext interface that should allow to code to run against either and
> there doesn't seem to be much loss of functionality from just using the
> interface.
>
> But for the entities it looks like the client should use PersistentObject
> subclasses and the server should use CayenneDataObject subclasses, and
> PersistentObject is missing some useful methods.  So I'm wondering what in
> CayenneDataObject prevents it from being used on the client, or what in
> PersistentObject prevents it from being a good fit on the server.  What is
> the best way to end up with a single set of classes that supports both?
>
> Thanks,
> John
>