0

Our team are using Spring Boot 2 with sql2o as db library. In the paste in our services, for trivial methods, we simply call the repository and returns the model. For example, if I have a Supplier table, I had in the service

@Override
public List<Supplier> findAll() {
    return supplierRepository.findAll();
}

Now, since in our controllers we need 99% in the cases other objects correlated to the model, I would create a composite class that holds the model and other models. For example:

@Override
public List<UnknownName> findAll() {
    List<Supplier> suppliers = supplierRepository.findAll();

    List<UnknownName> res = new ArrayList<>();

    UnknownName unknownName;
    LegalOffice legalOffice;

    if (suppliers != null) {
        for (Supplier supplier in suppliers) {
            unknownName = new UnknownName();
            unknownName.setSupplier(supplier);
            legalOffice = legalOfficeService.findByIdlegaloffice(supplier.getLegalofficeid);
            unknownName.setLegalOffice(legalOffice);
            res.add(unknownName);
        }
    }

    return res;
}

What should the name of class UnknownName?

PS: I simplified the code for better redability, but I use a generic enrich() function that I call for all the find methods, so I don't have to dupe the code.

Marco Sulla
  • 15,299
  • 14
  • 65
  • 100

2 Answers2

1

I would recommend SupplierDto or SupplierLegalOfficeDto. DTO stands for Data Transfer Objects and it's commonly used for enriched models (more here).

Also you shouldn't check suppliers for null as repository always returns a non-null list.

Adrian
  • 2,984
  • 15
  • 27
  • Mh, I dont' know. DTO is for data transfer, and I'm not transferring data, I'm creating a composer. I did some research and I found this article: https://www.programering.com/a/MDM2kjNwATc.html Now I'm undecided between BO and VO. – Marco Sulla Apr 20 '19 at 13:10
  • `BO: The full name is Business object: service object The main role is to encapsulate business logic as an object. The object may include one or more other objects. For example, a resume, education experience, work experience, social relations and so on. We can put the education has a corresponding PO, experience a corresponding PO, social relations corresponding to a PO. BO object processing resumes to establish a corresponding resume, each BO contains the PO. Such processing business logic, we can according to the BO to handle. ` – Marco Sulla Apr 20 '19 at 13:10
  • `VO : The ViewObject presentation layer object The data object corresponding interface display. For a WEB page, an interface or SWT, SWING, with a VO object corresponding to the interface value. ` – Marco Sulla Apr 20 '19 at 13:11
  • I read https://www.experts-exchange.com/questions/28301770/BO-business-object-classes-vs-VO-value-object-classes-in-java.html and now I think the correct term is `VO`. – Marco Sulla Apr 20 '19 at 13:18
  • Ok, it seems that `VO` it's a term used also for simple value objects. I think I'll adopt the term [Aggregate of DDD](https://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks) – Marco Sulla Apr 20 '19 at 13:28
1

In the end, I adopted the suffix Aggregator, following the Domain-driven design wording.

Marco Sulla
  • 15,299
  • 14
  • 65
  • 100