Let's say we are writing the order process of an online store. We have a
bunch of domain entities: product, discount, order, order item. These
entities have state (price, type of discount, etc), and behavior
(product can calculate its discount price, with the help of the
discount, order can calculate the grant total, etc).
The save is done by an OrderRepository. This is the gateway in the clean
architecture.
So, we have a bunch of domain object to persist, and it's time to create
an OrderRepository implementation.
First, we choose the technology for the order persistence. Of course, we
choose relational database, because of:
- Most of the developer familiar with it.
- There is plenty of know-how on the internet.
- The relational database is a proven technology.
- Most of the time a database can handle the given load.
In java, we have two possibilities to use a database: JDBC and JPA.
The JDBC implementation of the OrderRepository is straightforward: a
handful of prepared sql statements, and the JDBC API.
Advantage:
- Our business entities are independent from the underlying database
tables, we can change our database structure without affecting the
business entities.
Disadvantage:
- Requires a lot of code that map the business entities to the database
and vice versa. This is cumbersome and error prone.
In case of JPA, we have two options again:
We create JPA entities, and then we map the business entities to the JPA
entities. This solution has the same advantage and disadvantage. The
experience says that with this approach we just duplicates the classes,
because usually the JPA entities looks like as the business entities,
without any behavior.
We can use directly the business object as JPA entities. For this, we
need supply mapping metadata, what can be either annotation, or xml.
With this approach, we have the opposite advantage and disadvantage: We
don't have to write plumbing to java -> db map, but if we want change
the database structure, it might affects the domain objects. However, my
experience says that we don't change the database structure too much, so
we can live with the disadvantage.
So, we have a few alternatives, and it looks like all of them have the
same amount of advantages and disadvantages.
Now, everybody please look at her/his past experience, and answer this
question: How many times did I change underlying persistence technology
of a software? I doubt it that it was many times.
Therefore, I believe that putting the JPA annotations directly onto the
business entities, is a good trade-off. With this, we can eliminate the
tedious plumbing code write, yet we still have the flexibility to change
the the persistence technology.
Of course if the requirement is that our product have to support several
persistence technology (relational db, mongodb, csv, ldap, etc), then we
must write the mapping code by hand, but I thing usually this isn't the
case.
My motivation therefore: be practical, don't try to implement something
that you aren't gonna need it.
P.S.: No harm done.
On 2014.11.19. 20:40, Caio Fernando Bertoldi Paes de Andrade wrote:
> Sure, I look forward to hearing what others have to say too.
>
> Caio
>
> P.S.: Sorry about that, Péter. Thanks for the clarification.
>
>> On 19 of Nov, 2014, at 17:24, Böszörményi Péter <
zmble...@gmail.com
>> <mailto:
zmble...@gmail.com>> wrote:
>>
>> Hey, I like this answer. I like this kind of attitude. :) Right now,
>> I'm not in the position to give a proper answer, but I hope the
>> weekend will be more relaxed and then we can discuss this further.
>>
>> P.S.: You can use my first name, Böszörményi is my family name.
>>
>> On Nov 19, 2014 6:56 PM, "Caio Fernando Bertoldi Paes de Andrade"
>> <
caio...@icloud.com <mailto:
caio...@icloud.com>> wrote:
>>
>> Well, I always say, be pragmatic: TDD gives you all the necessary
>> decoupling, there’s no need to choose JPA before you build your
>> business logic. Just build your entities first and your
>> persistence mechanism afterwards, your logic will automatically be
>> decoupled from your persistence. If subclassing your entities for
>> persistence really annoys you, then you still can marshall fields
>> manually into your gateway classes.
>> I can recommend you to read all books from Uncle Bob. ;-)
>>
>> Caio
>>
>> P.S.: Böszörményi, sorry for copying your email’s structure, but I
>> found it just too funny to resist. :-P
>>
>>> On 19 of Nov, 2014, at 15:22, Böszörményi Péter
>>> <
zmble...@gmail.com <mailto:
zmble...@gmail.com>> wrote:
>>>
>>> Well, I always say, be pragmatic. The JPA gives all the neccesary
>>> abstraction, there is no need another layer that just copies
>>> data, and delegates method calls. Just put the proper logic into
>>> the JPA entites. If the JPA annotations really annoy you, then
>>> you still can put the mapping into xml files.
>>> I can recommend you to read the Real World Java EE Patterns from
>>> Adam Bien.
>>>
>>> On Nov 18, 2014 4:43 PM, "Sebastian Trueman"
>>> <
akri...@googlemail.com <mailto:
akri...@googlemail.com>> wrote:
>>>
>>> At the moment I'm thinking about an known problem. It's about
>>> putting business logic into jpa entities or not. There are 2
>>> ways to handle relating domain object logic:
>>>
>>> *1. put the logic into jpa entities. *
>>> If I choose this way I feel uncomfortable in different ways.
>>> At first I commit to a jpa solution. So I can't easily unplug
>>> jpa but I think its still ok. The second pain is mixing
>>> different areas of responsibility. One part of it is the
>>> persistence unit, that is responsible for storing data to the
>>> persistent storage with untouched clear domain logic. I think
>>> I'll profit of writing less code if I put all the strong
>>> related logic in the jpa entities and the further related
>>> logic in some interactors / usecase classes.
>>>
>>> *2. put the logic into pure domain logic entities*
>>> It feels more correct if I would put all the logic into
>>> separate entities. But it feels like doing some bad things
>>> with such a great ORM framework like hibernate. I also need
>>> to pipeline the entities to the jpa framework. I definitely
>>> have to write more code resulting in adding more complexity.
>>> The profit is a good separation of irresponsibles.
>>>
>>> Maybe you can give me some more pro and cons, maybe with some
>>> storys :). I also still know the statement of Martin Fowler
>>> <
http://www.martinfowler.com/bliki/AnemicDomainModel.html> ,
>>> who call variant 2 an anemic domain model.
>>>
>>> br
>>>
>>> --
>>> The only way to go fast is to go well.
>>> ---
>>> You received this message because you are subscribed to the
>>> Google Groups "Clean Code Discussion" group.
>>> To unsubscribe from this group and stop receiving emails from
>>> it, send an email to
>>>
clean-code-discu...@googlegroups.com
>>> <mailto:
clean-code-discu...@googlegroups.com>.
>>> <mailto:
clean-code...@googlegroups.com>.
>>> <mailto:
clean-code-discu...@googlegroups.com>.
>>> <mailto:
clean-code...@googlegroups.com>.
>> <mailto:
clean-code-discu...@googlegroups.com>.
>> <mailto:
clean-code...@googlegroups.com>.
>> <mailto:
clean-code-discu...@googlegroups.com>.
>> <mailto:
clean-code...@googlegroups.com>.
> <mailto:
clean-code-discu...@googlegroups.com>.
> <mailto:
clean-code...@googlegroups.com>.