lundi 20 avril 2020

Best practices with a domain object colletion in DDD

I have the following class:

public class DomainClass {
    private Integer value;
    private Integer total;
    private Integer month;
    public Double getPercent(){/*Business logic*/}
}

I want to do the same getPercent operation with a list of DomainClass objects without repeat code. I got 2 ideas to handle that but I don't know if they're good ones.

Idea 1 - Create a service and iterate the list:

public class DomainObjectService{
....
public Double getPercent(List<DomainClass> list){
    double value, total;
    for(DomainClass d : list){
        value += d.getValue();
        total += d.getTotal();
    }
    // do percent operation
}

Idea 2 - Query operation at database, fill object and call the business method

Idea 1 - Create a service and iterate the list:

public class DomainObjectService{
....
public Double getPercent(){
    double value, total;
    .... query data with sql and set the above variables

    // do percent operation
    return new DomainBusiness(value, total).getPercentage();
}

I'm reading that in DDD a entity should handle itself logic but in this case of a collection operation how it should be treated?

Well, if my DDD base knowledge is wrong. I would like to know good article/books/example of DDD in java.

Aucun commentaire:

Enregistrer un commentaire