CleanArchitecture icon indicating copy to clipboard operation
CleanArchitecture copied to clipboard

Entities shouldn't depend on database?

Open rongi opened this issue 7 years ago • 5 comments
trafficstars

Your entities looks like database object, they have table names, database ids, etc. Aren't entities supposed to be most abstract type of data, not dependent in any way on your persistence solution?

http://cleancodejava.com/uncle-bob-payroll-case-study-full-implementation/

"Entities are not the same as JPA Entities, they are high level business rules, policies, and should be independent from any other concerns."

It looks like entities here are JPA Entities.

rongi avatar Mar 22 '18 00:03 rongi

Thanks for raising this issue. Not entirely sure we're talking about the same here. Entities (business objects) like Item are decoupled from JPA entities (such as ItemEntity).

There's a similarity between Item and ItemEntity as they represent the same business data and there's no need for a fancy application model that makes it hard to correlate with its persistent representation. The application model does not contain table names, database ids and such.

Care to elaborate further on:

Your entities looks like database object, they have table names, database ids, etc. Aren't entities supposed to be most abstract type of data

That's the bit I'm currently not following.

mp911de avatar Mar 22 '18 08:03 mp911de

Thank you for your answer Mark. It's clear now. I thought that Item objects are models, because they are inside applicationmodel package, and ItemEntity objects are entities because they are inside entity package.

Now it's clear that Item is an entity and ItemEntity is a database object. Do you think it's a good idea to rename the packages to prevent that kind of confusion in the future?

rongi avatar Mar 22 '18 08:03 rongi

In general, there a few terms to describe the same thing and overlapping terms. Talking about an entity without further context lets one think about JPA entities. Entities within the hexagonal architecture are a different thing.

I'm happy to improve. In general, one wants to have a properly named application model without prefixes/suffixes as that's where a fair amount of focus lies on. I used …Entity within the JPA module to distinguish between the application model entity and JPA entity lacking creativity 🙃

Do you have a suggestion how to improve naming?

mp911de avatar Mar 22 '18 09:03 mp911de

I like the class naming. I'm talking about renaming the java packages where the classes are located. applicationmodel -> entity, entity -> jpaentity or dbmodels or something like that.

rongi avatar Mar 22 '18 11:03 rongi

applicationmodel -> entity

Makes sense.

entity -> `jpaentity``

Moving all classes and interfaces to their parent package (.jpa) might be even the better way to go.

mp911de avatar Mar 22 '18 13:03 mp911de