lundi 27 mars 2017

How to manage high level of abstraction in the persistence layer?

I am looking for a way to build an abstraction layer between my business domain (or login) and the persistence layer.

Let me start with an example, lets say we have a user and we want to implement the following features.

User:

getById(String id);
getByName(String name);
getByAge(int age);
getByNameAndAgeLTE(String name, int age);
...

If we want to limit the number of methods to implement and to keep the logic separated from the db, we have to set some boundaries between those layers.

Searching the web, I have came across plenty of methods, such as query-object-pattern, active-record, dal and plenty of ORM examples.

I feel uncomfortable with the data query with all of the solutions I came to so far. Every each and one of them require the developer to write the tool specific API in its business domain.
For example, I liked the idea of active record (and any other ORM), but you have to write the queries using the library (or framework) specific API, and thus leak some separation between the layers.

If we take a look at some ORM (for example, morphia)

datastore.createQuery(User.class)
                     .field("name").equal("Rocky")
                     .field("age").lessThanOrEq(30)
                     .asList();

We see we have to know the Morphia API, and by that mixing the persistence layer with our logic layer. I can't think of a way to achieve this abstraction and separation, and I am very confused with all that information that I have read.
What are the best practices of querying a database? How does enterprise application access and query their data?

Aucun commentaire:

Enregistrer un commentaire