Java EE Design Assistance Required - java

I am using MVC pattern in my web application. In which I have three layers
Control Layer
Manager Layer
Dao Layer
And I am using DTOs from control layer to manger and then to Dao layer and same as opposite.
My question is that what is the main purpose of DTO?
Can I use DTOs to map our relational database table or should I go with 'Bean'?
If I use DTOs between layers then how can I represent a database table in an object because DTOs among layers can contain properties which are not related to the database table.

There is no problem in using DTOs and map them to your database tables. But you'll have to do the mapping by yourself (using JDBC, Spring JDBC, etc).
Another option is to use an ORM to do the mapping of your DTOs to database. You can even create properties that are not mapped to your tables. Take a look at JPA.
The choice between those two options is something personal. The first will be more laborous at first, while the second option have a bigger learning curve. If you are well versed into SQL, I would go with JDBC.

My question is that what is the main
purpose of DTO?
The main purpose of a DTO is to transmit data between two layers. It has no real functionality other than to act as a basket for shipping data.
That's why they call it a Data Transfer Object.
Can i use DTO's to map our relational
database table or should i go with
'Bean'?
Whether you decide to use JavaBean formatted accessors or your own accessors really doesn't matter with a DTO. Both sides of the transfer must be in agreement; but, if you have a setName(...) setter or a name(...) setter it will not affect functionality.
Although it may not matter in a functionality sense, it is best to stick to established naming conventions for ease of revisiting the code and lack of confusion when training new maintainers. Also, a few libraries might assume you are using bean conventions (or require them). If you are uncertain, best to stick with standard JavaBean conventions, as your new conventions are probably not as tested (or as formal).
If i use DTO's between layers then how
can i represent a database table in
object because DTO's among layers can
contain properties which are not
related to database table.
DTOs have nothing to do with database tables. Don't make your DTO look like your database table unless it's the most natural thing to do.

The main purpose of DTO's is to reduce the overhead when transferring data across layers.
If you didnt have a DTO, what you would have is a class containing data as well as logic which would be getting passed across layers. Using DTO's ensures that you pass only what is needed i.e the data across layers.
Definitely, you do have the option of mapping your DTO's to your database tables and having the bean design which more closely represents the domain objects.
That is one way of doing it.
Conversely, depending on your database design, your DTO's could be more in line with your actual business entities - just without the logic

My question is that what is the main
purpose of DTO?
Like the expansion (Data Transfer Object) implies, DTOs are meant to transfer structured data across various tiers. DTOs enable you to decouple the protocol specific implementations that represent data, so that data from different sources can be abstracted before communication across tiers.
For example, DTOs will allow you to decouple the data present in a HttpServletRequest object from its internal storage, so that you can send the data to a service in the business logic layer. The same applies for DTOs used to abstract the results obtained from a SQL query and residing in a ResultSet object. In short, DTOs allow you to transmit data without holding onto the source - you can forget about the HTTP response and the JDBC connections, while you work on the data.
Can i use DTO's to map our relational
database table or should i go with
'Bean'?
You can adopt the second approach of using Beans. In fact, with JPA you do not require your DTOs at all. The JPA managed beans themselves represent data in various tables, and can be de-linked from the database state, so that you can use them for data transmission.
If i use DTO's between layers then how
can i represent a database table in
object because DTO's among layers can
contain properties which are not
related to database table.
That depends on how you want to couple the DTO with the database table. It is preferable to have a one-to-one mapping between the DTO and the database table, and choose another DTO for the purpose of transmitting properties not related to the table. After all, DTOs like every other object should have a single responsibility. If the responsibility is to reflect a database table, then it should contain other "irrelevant" properties.
To extend the recommendation of using JPA in this context, it is poor design to have unrelated attributes in a JPA entity, especially when that unrelated attribute should be marked as transient and adds no value to the behavior of the entity.

Related

Domain vs DTO on database entity relations

I have three tables on a database, and one old and good query that joins the interesting columns for the usecase on a DTO object after use a RowMapper. Classical Java approach.
But I am wondering, how can I convert this into a DDD implementation? How can I use entities for solve the problem without having 3 separated queries to create the models with it's fields, and then create a DTO just for create the object with the desired columns?
What I am missing? Or should be better the already implemented way?
You cannot convert that model to DDD.
DDD is about creating a business model in a separate layer: the domain layer. The purpose of this layer is to verify that any state-changing operations does not violate your business rules. How you model your domain entities must be driven by the use case you are handling, not how the data is stored: the domain layer is said to be persistence agnostic. It doesn't matter whether the data is stored in one or multiple stores, RDBMS, nosql, files, etc ... that's ther purpose of the infrastructure layer.
Domain object are read by a class called a repository. The repository behaves like an in memory collection of domain object, but actually re-hydrates objects from the persistence store(s). It operates as a mediator between your persistence model and your domain model. Since models may differ, the repository can flatten relations, join data from different sources, and ignore some columns if needed.
When querying your state, you don't need to use the domain layer at all. The purpose of that layer is to validate state-changing operation, business-wise, but this is unnecessary when querying the state. In that situation, you can implement classes, called mediators, that will query your database and produce presentation layer DTO. Like the repositories, mediators are adapters between your persistence model and your presentation model. It can join data from multiple persistence stores, flatten relations, and ignore columns as well.
Whether to use an ORM, or not, for accessing the database, is an implementation detail of your infrastructure layer. It has nothing to do with domain modeling, which is about your use cases and business rules validation.

JPA annotations in entity layer according to Uncle Bob

According to Uncle Bob, the entity layer should know nothing about the database.
What about the JPA annotations then? Don't they violate the architecture proposed by the author? If they do, how to use the entities with JPA?
In a non-ORM world, a clean architecture would (or could) involve having DAO interfaces, with DAO implementations knowing how to retrieve the data from a database (or any other source), and convert and return it as a domain object. An upper layer would then use the DAOs (through the interfaces) to retrieve those objects.
This would allow you to for example create different DAO implementations for different databases, and you could change databases without bothering the rest of the software.
In a JPA/ORM world you can bypass a lot of that if you choose. You can use entity classes as domain objects, create the entity classes in a database agnostic way (i.e. not using any database specific NativeQueries for example). Now that your entities are database agnostic, you can just use NamedQueries in your service layer instead of creating DAOs.
In the end you need to have some layer which knows about the database, but in JPA's case it doesn't even hold true. Your entities are Java objects and it's the JPA implementation layer that is responsible for converting them to and from the database.
In conclusion: there are very few universal truths in software development and you can talk to a dozen uncles and hear a dozen versions of essentially the same "story".

Persistence entities as data transfer objects

I have some persistence capable Java objects with all the #annotations in my web application. All these objects reside in a data layer. Is it a best practice to use these persistence objects as data transfer objects?
For example, if I want to pass back the data fetched from data store, should I directly return those persistence objects or should I manually copy data to a intermediary DTO and pass it back to other layers? Which approach would you suggest?
I would say that it is OK to do so (in fact the major advantage for these ORMs were to use these domain objects in different layers w/o having unnecessary DTOs) if you follow the following guidelines:
You don't extend the session boundaries i.e., any changes related to database should always be done using the Data Access Layer you have defined and not through these passed objects in other layers.
Any data that you need in other layer (layers above the Data Access Layer like Business Logic Layer and Presentation Layer) is pre-populated in these objects otherwise you will get exceptions according to the ORM behavior.
Don't extend the session boundaries for resolving the issue mentioned in No. 2
If you are absolutely certain no copies will end up in the Session storage or will be persisted/migrated to another instance/whatever, there is no need to do it.
If you need to keep these objects over multiple sessions/requests then it makes sense.
Another use-case is when you need to decouple loginc and persistence layer very thoroughly (i.e. to swap different persistence layers) then the coupling through the annotations could be troublesome.
I have never needed the extra level of abstraction provided by copying the persistence instance to a different DTO class.

DAO design pattern and using it across multiple tables

I'm looking for feedback on the Data Access Object design pattern and using it when you have to access data across multiple tables. It seems like that pattern, which has a DAO for each table along with a Data Transfer Object (DTO) that represents a single row, isn't too useful for when dealing with data from multiple tables. I was thinking about creating a composite DAO and corresponding DTO that would return the result of, let's say performing a join on two tables. This way I can use SQL to grab all the data instead of first grabbing data from one using one DAO and than the second table using the second DAO, and than composing them together in Java.
Is there a better solution? And no, I'm not able to move to Hibernate or another ORM tool at the moment. Just straight JDBC for this project.
I would agree with your approach. My DAOs tend to be aligned more at the object level, rather than from a DB Table perspective. I may manage more than one object through a DAO, but they will very likely be closely related. There is no reason not to have SQL accessing two tables living in one DAO.
And for the record, I have banished the acronym DTO from my vocabulary and code.
Ideally, how you store your data in a database, and then how you access them, should be derived from the nature of the relationship among the domain entities in your domain model. That is, Relational Model should follow from Domain Model. For example, if you have two entities, say, User and Address.
Scenario #1: Address are never accessed independently, they are always an attribute of User.
In this case, Address is a Value Object and User is an Entity, and there are guides on how to store this relationship. One way is to store Address attributes of Address alongside of attributes of User, in a single table. In this case, UserDao will handle both objects.
Scenario #2: Address can be associated to a User, but also can be separate on its own, an entity.
In this case, an approach different from the first one is needed. You may have a separate DAO and table for the Address type.
My point is, that more often this important idea is ignored that Domain Model should be the core of the application, driving other layers.
For instance, if your domain model is properly define and you are well aware of the type of entities you have and the relationship among them, then your persistence (relational tables and their relationships, your DAOs, etc) will evolve as a very logical consequence of what you have in the domain model.
In other words, if you spend some time studying your model, you will be able to trace your problem in determining how to organize your DAOs to a place in the domain model. If you can clearly define the type of the objects and the nature of relationship among them in the domain model, it will, help you resolve your problem in DAL layer.

Why are data transfer objects (DTOs) an anti-pattern?

I've recently overheard people saying that data transfer objects (DTOs) are an anti-pattern.
Why? What are the alternatives?
Some projects have all data twice. Once as domain objects, and once as data transfer objects.
This duplication has a huge cost, so the architecture needs to get a huge benefit from this separation to be worth it.
DTOs are not an anti-pattern. When you're sending some data across the wire (say, to an web page in an Ajax call), you want to be sure that you conserve bandwidth by only sending data that the destination will use. Also, often it is convenient for the presentation layer to have the data in a slightly different format than a native business object.
I know this is a Java-oriented question, but in .NET languages anonymous types, serialization, and LINQ allow DTOs to be constructed on-the-fly, which reduces the setup and overhead of using them.
"DTO an AntiPattern in EJB 3.0" (original link currently offline) says:
The heavy weight nature of Entity
Beans in EJB specifications prior to
EJB 3.0, resulted in the usage of
design patterns like Data Transfer
Objects (DTO). DTOs became the
lightweight objects (which should have
been the entity beans themselves in
the first place), used for sending the
data across the tiers... now EJB 3.0
spec makes the Entity bean model same
as Plain old Java object (POJO). With
this new POJO model, you will no
longer need to create a DTO for each
entity or for a set of entities... If
you want to send the EJB 3.0 entities
across the tier make them just
implement java.io.Serialiazable
OO purists would say that DTO is anti-pattern because objects become data table representations instead of real domain objects.
I don't think DTOs are an anti-pattern per se, but there are antipatterns associated with the use of DTOs. Bill Dudney refers to DTO explosion as an example:
http://www.softwaresummit.com/2003/speakers/DudneyJ2EEAntiPatterns.pdf
There are also a number of abuses of DTOs mentioned here:
http://anirudhvyas.com/root/2008/04/19/abuses-of-dto-pattern-in-java-world/
They originated because of three tier systems (typically using EJB as technology) as a means to pass data between tiers. Most modern day Java systems based on frameworks such as Spring take a alternative simplified view using POJOs as domain objects (often annotated with JPA etc...) in a single tier... The use of DTOs here is unnecessary.
Some consider DTOs an anti-pattern due to their possible abuses. They're often used when they shouldn't be/don't need to be.
This article vaguely describes some abuses.
The question should not be "why", but "when".
Definitively it's anti-pattern when only result of using it is higher cost - run-time or maintenance. I worked on projects having hundreds of DTOs identical to database entity classes. Each time you wanted to add a single field you ad to add id like four times - to DTO, to entity, to conversion from DTO to domain classes or entities, the inverse conversion, ... You forgot some of the places and data got inconsistent.
It's not anti-pattern when you really need different representation of domain classes - flatter, richer, ...
Personally I start with a domain class and pass it around, with proper checks at the right places. I can annotate and/or add some "helper" classes to make mappings to database, to serialization formats like JSON or XML ... I can always split a class to two if I feel the need.
It's about your point of view - I prefer to look at a domain object as a single object playing various roles, instead of multiple objects created from each other. If the only role an object is to transport data, then it's DTO.
If you're building a distributed system, then DTOs are certainly not an anti pattern. Not everyone will develop in that sense, but if you have a (for example) Open Social app all running off JavaScript.
It will post a load of data to your API. This is then deserialized into some form of object, typically a DTO/Request object. This can then be validated to ensure the data entered is correct before being converted into a model object.
In my opinion, it's seen as an anti-pattern because it's mis-used. If you're not build a distributed system, chances are you don't need them.
DTO becomes a necessity and not an ANTI-PATTERN when you have all your domain objects load associated objects EAGERly.
If you don't make DTOs, you will have unnecessary transferred objects from your business layer to your client/web layer.
To limit overhead for this case, rather transfer DTOs.
I think the people mean it could be an anti-pattern if you implement all remote objects as DTOs. A DTO is merely just a set of attributes and if you have big objects you would always transfer all the attributes even if you do not need or use them. In the latter case prefer using a Proxy pattern.
The intention of a Data Transfer Object is to store data from different sources and then transfer it into a database (or Remote Facade) at once.
However, the DTO pattern violates the Single Responsibility Principle, since the DTO not only stores data, but also transfers it from or to the database/facade.
The need to separate data objects from business objects is not an antipattern, since it is probably required to separate the database layer anyway.
Instead of DTOs you should use the Aggregate and Repository Patterns, which separates the collection of objects (Aggregate) and the data transfer (Repository).
To transfer a group of objects you can use the Unit Of Work pattern, that holds a set of Repositories and a transaction context; in order to transfer each object in the aggregate separately within the transaction.

Categories