0

Right Now in my application input data(City data) coming from database. I follow the below procedure for processing the input. - read the data from database and create the CityDTO object(Class=CityDTO) - map DTO to business object (CityBO)

But now, Client will send city data with Json file using webservice. Now i have CityJson object(Class = CityJSon) and i want to map CityJSon to CityBO

Please refer the below code snippet for understanding :

***** Database or DTO object *****

Public Class CityDTO {
    Public String name;
    public <List>AreaDTO;
}
Public Class AreaDTO {
    Public String name;
    public String zipcode;
}

***** Business or Domain object *****

Public Class CityBO{
    Public String name;
    public <List>AreaBO;
}
Public Class AreaBO{
    Public String name;
    public String zipcode;
}

***** Mapping of CycleDTO to CycleBO *****

Class DBmapper {
    public void CityBO mapToCityBO(CityDTO citydto){
        CityBO citybo =  new cityBO();
        citybo.setName(citydto.getName);
        citybo.setAreaBO(mapAreaBO(citydto.getAreaDTO));
    }

    public List<AreaBO>mapAreaBO(List<AreaBO> listOfAreadto){
        List<AreaBO> listOfAreaBO = new ArrayList<AreaBO>
        for(AreaDTO areadto : listOfAreadto){
            AreaBO areaBO = new AreaBO();
            areaBO.setName(areadto.getname());
            areaBO.setZipCode(areadto.getZipCode());
        }
    }
}

Now i have CityJson class/object and i want to map CityJson object to CityBO object in future i may have CityXML object which again need to map into CityBO.

Two or more differnt input object and wanted to map in same business object. How can i achive this so that if in future client add more input type then my application code must be maintained.?

Rubeena
  • 171
  • 1
  • 3
  • 11
  • Maybe you can the adapter pattern. https://java-design-patterns.com/patterns/adapter/ – Julio Daniel Reyes Sep 05 '19 at 15:00
  • The need for `CityJSon` is not clear. The json parser should be able to re-use the existing `CityDTO` class. Update your question to add some code to help clarify the issue. – Andrew S Sep 05 '19 at 15:41
  • Thank you for reply, at present we have little bit change in cityJson compared with cityDTO – Rubeena Sep 05 '19 at 16:10

2 Answers2

0

DTO is a concept while Json, Xml, binary and etc are serialization aspect of the communication which somehow is a detail. One simple way is to create the DTO class, then for each different type of serialization, convert the received data to the DTO object and then convert the DTO object to BO.

mehdi.loa
  • 579
  • 1
  • 5
  • 23
0

You won't need to map your persistence entities to DTOs and vice versa manually. There are many mapping frameworks you can use to accomplish it. For instance, I advise you to have a look at MapStruct, which is annotation-based and works as a Maven Annotation Processor. It integrates with in both CDI and Spring-based applications.

You also may want to consider Lombok to generate getters, setters, equals(), hashcode() and toString() methods for you.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • I am new in mapstruct framework anyhow i am able to execute the basic demo of mapstruct mapping, but i am stuck how to perform the mapping of fully associated object like City has Area, City has Park & Area has PinCode(multiple). I want to write separate method for each object mapping and because i need to do some customization while mapping. – Rubeena Sep 17 '19 at 12:44