I'm using Jackson to parse JSON for my android app. I also intend to use it in my REST server too, so I'll be sharing my models between client and server.
I've created a POJO to model a domain object "Friend". When the client gets https://www.myserver.com/api/1/friend/1234 I want to return the serialised Friend with ID 1234, perhaps with one or 2 fields missing.
However, when a client gets https://www.myserver.com/api/1/friend/ I want to return all friend objects, but with less data that might be more appropriate to search results (e.g. just first name, last name and profile image, but excluding their list of friends, date of birth, etc.).
What pattern should I follow here so that I can represent the same underlying model in different ways depending on how it'll be accessed?
Inheritance can be an option in conjunction with #JsonIgnoreProperties.
You can have a class Friend and extend it to restrict what properties are to be serialized.
#JsonIgnoreProperties({ "friends", "dateOfBirth" })
class RestrictedFriend extends Friend {
}
See if you want to use Inheritance. Have a base class with fields that you want to share with everyone, and a sub-class which has more restricted data. Have two JSON APIs, one for public info, and one for public+secure info, and serialize the base class or sub-class object based on which API was called.
Related
I don't understand the difference between POJO and DTO in Java. I have read the article here: What is the Difference Between POJO (Plain Old Java Object) and DTO (Data Transfer Object)? . But I still don't understand the code implementation between them, what makes them different. Can you give the code example for each of them? Thank you so much before!
POJO means Plain Old Java Object. A POJO is an object that doesn't need to implement any specific interface or extend some specific class to be used by a framework. The term has been coined, if I remember correctly, to react against EJB1 and EJB2 which imposed a lot of constraints on the way you had to write beans to make them EJBs. POJO is more a marketing term than a technical term.
DTO means Data Transfer Object. A DTO is an object that is used to transfer data between layers of an application, typically over the network. It often consists in a POJO (i.e. a simple Java class) containing fields and getters.
A Plain Old Java Object or POJO is a term initially introduced to designate a simple lightweight Java object,
not implementing any javax.ejb interface in contrast to Entity beans
Now a days , POJO term is used for any simple object with no extra fancy annotation like third party annotations etc
DTO is an object whose responsibility is just to encapsulate data in a value object . It will just have instance members and corresponding getters and setters. Generally they used to represent the row in data store. While pojo include some more methods that doing some business processing on them
Actually, POJO is more general. Any class which has private serializable fields, getters and setter for each field and doesnt extend other class or implement an interface may be called POJO.
DTO is an object which uses some data object (for example, JPA entity object) to transfer data to another tier of your application (for example, if entity1 object has reference to other entity2, DTO can replace entity2 reference by its entity2.id.toString or other entity2 field).
I have a graph of domain objects and i need to build a DTO to send it to the view. How to design it properly? I see 2 options where can I put the DTO building code:
1) Into the DTO constructor. But then the domain object has to present all fields to DTO via getters so it's not a DDD.
public DTO(DomainObject domain) {
/// access internal fields of different domain object.
}
2) Into the domain object. There will be no problem with accessing fields but the domain object will grow very fast when new view are added.
public DTO1 createDTO1() {
...
}
public DTO2 createDTO1() {
...
}
// and so on...
How should I build DTOs properly?
I think there is a bigger issue at play here. You should not be querying your domain. Your domain should be focused on behaviour and, as such, will quite possibly not contain the data in a format suitable for a view, especially for display purposes.
If you are sending back your entire, say, Customer object to Edit then you are performing entity-based interactions that are very much data focused. You may want to try and place more attention on task-based interactions.
So to get data to your view I'd suggest a simple query layer. Quite often you will need some denormalized data to improve query performance and that will not be present in your domain anyway. If you do need DTOs then map them directly from your data source. If you can get away with a more generic data container structure then that is first prize.
Variants:
Constructor with simple types in DTO: public DTO(Long id, String title, int year, double price)
Separate class - converter with methods like: DTO1 createDTO1(DomainObject domain)
Framework for copy properties from one object to other, like Dozer: http://dozer.sourceforge.net/
1) ... the domain object has to present all fields to DTO via getters ...
2) ... the domain object will grow very fast ...
As you can see, the problem is that both alternatives are coupling your model with your DTOs, so you need to decouple them: introduce a layer between them in charge of performing the mapping/translation.
You may find this SO question useful.
domain object has to present all fields to DTO via getters so it's not
a DDD
Just because a domain object has getters doesn't make it anemic or anti-DDD. You can have getters and behavior.
Besides, since you want to publish to the View a DTO containing particular data, I can't see how this data can remain private inside the domain object. You have to expose it somehow.
Solution 2) breaks separation of concerns and layering (it's not a domain object's business to create a DTO) so I'd stick with 1) or one of #pasha701's alternatives.
While many stackoverflow answers regarding this question exists, rarely do they clearly distinguish the difference between the two. As a result, I am having confusion in understanding them.
Here I am working with this pattern
Referring to the figure 9.1, both business object and transfer object are used. While definition of both are given along the lines as:
generally considered to be a class that represents an Entity, e.g. a Book or a Store. Such a class has certain properties like price, colour, width, isbn number etc. In Java or .NET, it consists of so-called setters and getters
Now DTOs have same definition. It appears to me like a Bean representing an object. So in a standalone application what could possibly be business object and DTO. Please distinguish between the two in terms of the pattern link above.
A DTO is used to transport the data from one layer to other, (for example from the data access layer to the model).
A BO contains the Business Logic.
But the most important thing in this patterns is to separate the layer, in order to make the software more easily maintained.
For example, if you separate the data access layer, it doesn't matter if you are using a database to get the data, or a socket, or a plain text file separated with pipelines, you can change this and it will not affect the rest of the layers.
That article defines:
The BusinessObject represents the data client. It is the object that requires access to the data source to obtain and store data. A BusinessObject may be implemented as a session bean, entity bean, or some other Java object, in addition to a servlet or helper bean that accesses the data source.
and
This represents a Transfer Object used as a data carrier. The DataAccessObject may use a Transfer Object to return data to the client. The DataAccessObject may also receive the data from the client in a Transfer Object to update the data in the data source.
Surely you can see the difference between a class that wants to obtain and store data in a data source, and an object that simply carries data between layers?
One class Customers instantiates many other classes (e.g. CustomersFromMysql, CustomersFromPostgeSQL) that all query databases which give back customer names. Now these customer names come back as for example name = "John Doe", what my Customers class needs though is more than name, it also needs nameNoSpace = "JohnDoe" and nameInvertedComma = "Doe, John" for example.
I would programm a helper class Converter which has methods like invertName(name) and removeComma(name). Would I instantiate the converter in every class that queries the database (CustomersFromMysql, CustomersFromPostgeSQL) to give back all required variables or would I instantiate the Converter in the instantiating class Customers so when I get back results I iterate through my list and batch convert them?
Of course both works, but what is the way to go?
You should remember separation of duties in such cases. The database related classes should handle only the database specific aspects. Performing operations (calculations) on the retrieved data should be treated as business logic. So, if your Customers class already has some logic in it, it would be the perfect place for putting in the conversion routines. Neverthess, it really depends on where you think your logic belongs to.
It may also make sense to apply some naming conventions. In general you can distinguish between at least the different kinds of classes in case like the one you desribed in your question:
Data Access Objects (DAO); perform database opertions (your SQL classes)
Data Transfer Objects (DTO) or entities; represent the structure of your business objects
Business Logic; retrieve the DTO by using DAOs, perform some logic according to your requirements, push the DTO back into the database by using the DAOs again
I have a problem regarding Java custom serialization. I have a graph of objects and want to configure where to stop when I serialize a root object from client to server.
Let's make it a bit concrete, clear by giving a sample scenario. I have Classes of type
Company
Employee (abstract)
Manager extends Employee
Secretary extends Employee
Analyst extends Employee
Project
Here are the relations:
Company(1)---(n)Employee
Manager(1)---(n)Project
Analyst(1)---(n)Project
Imagine, I'm on the client side and I want to create a new company, assign it 10 employees (new or some existing) and send this new company to the server. What I expect in this scenario is to serialize the company and all bounding employees to the server side, because I'll save the relations on the database. So far no problem, since the default Java serialization mechanism serializes the whole object graph, excluding the field which are static or transient.
My goal is about the following scenario. Imagine, I loaded a company and its 1000 employees from the server to the client side. Now I only want to rename the company's name (or some other field, that directly belongs to the company) and update this record. This time, I want to send only the company object to the server side and not the whole list of employees (I just update the name, the employees are in this use case irrelevant). My aim also includes the configurability of saying, transfer the company AND the employees but not the Project-Relations, you must stop there.
Do you know any possibility of achieving this in a generic way, without implementing the writeObject, readObject for every single Entity-Object? What would be your suggestions?
I would really appreciate your answers. I'm open to any ideas and am ready to answer your questions in case something is not clear.
You can make another class (a Data-Transfer-Object) where you have only the fields you want to transfer.
A way of custom serialization is implementing Externalizable
I would say the short answer to your question is no, such varied logic for serialization can't be easily implemented without writing the serialization yourself. That said an alternative might be to write several serializer/deserializer pairs (XML, JSON, whatever your favorite format, instead of standard yusing the built in serialization). and then to run your objects through those pairs sending some kind of meta-information preamble.
for example following your scenarios above you may have these pairs of (de)serialization mechanisms
(de)serializeCompany(Company c) - for the base company information
(de)serializeEmployee(Employee e) - for an employee's information
(de)serializeEmployee(Company c) - the base information of employees in a company
(de)serializeRelationships(Company c) - for the project relationships
For XML each of these can generate a dom tree, and then you place them all in a root node containing metainformation i.e.
<Company describesEmployees="true" describeRelationships="false">
[Elements from (de)serializeCompany]
[Elements from (de)serializeEmployee(Company c)]
</Company>
One potential "gotcha" with this approach is making sure you do the deserialization in the correct order depending on your model (i.e. make sure you deserialize the company first, then the employees, then the relationships). But this approach should afford you the ability to only write the "actual" serialization once, and then you can build your different transport models based on compositions of these pieces.
You could take an object swizzling approach where you send a "stub" object over the wire to your client.
Pros
The same object graph is logically available client-side without the overhead of serializing / deserializing unnecessary data.
Full / stub implementations can be swapped in as necessary without your client code having to change.
Cons
The overhead in calling getters which result in dynamically loading additional attributes via a call to the server is hidden from the client, which can be problematic if you do not control the client code; e.g. An unwitting user could be making an expensive call many times in a tight loop.
If you decide to cache data locally on the client-side you need to ensure it stays in-sync with the server.
Example
/**
* Lightweight company stub that only serializes the company name.
* The collection of employees is fetched on-demand and cached locally.
* The service responsible for returning employees must be "installed"
* client-side when the object is first deserialized.
*/
public class CompanyStub implements Company, Serializable {
private final String name;
private transient Set<Employee> employees;
private Service service;
public Service getService() {
return service;
}
public void setService(Service service) {
this.service = service;
}
public String getName() {
return name;
}
public Set<? extends Employee> getEmployees() {
if (employees == null) {
// Employees not loaded so load them now.
this.employees = server.getEmployeesForCompany(name);
}
return employees;
}
}