jeudi 25 juin 2015

DAO persistence: only one method to store a complex data object?

Here we have:

public final class Product {

 Integer productId;
 String description;
 ...

 public Product(final Integer productId, final String descripcion, ...) {
    this.product_id = productId;
    this.description = description;
    ...
 }

 public Integer getProductId() { ... }
 public String getDescription() {...}

 ...

}

An then:

public final ProductDetails {

 Integer productId;
 String serialNumber;
 ...
 public ProductDatails(final Integer productId, final serialNumber, ...){ ... }
 public Intger getProductId() { ... }
 public String getSerialNumber { ... }
 ...

}

So, in order to build a DAO which persists the data the two classes contain, the question is that if it is a good practice to do as follows:

    public Interface IProductDAO {

      public void saveProduct(final Product product);
      public void saveProductDetails(final ProductDetails productDetails);

    }

or:

public Interface IProductDAO {
 public void saveProduct(final Product product, final ProductDetails productDetails);

I have been taking in consideration to refactor the two classes into one as follows:

public final class Product {

 Integer productId;
 String description;
 ProductDetails productDetails;
 ...

 public Product(final productId productId, final String description, final ProductDetails productDetails,...) {

   if(productId!=productDetails.getProductId())
      throw new IllegalArgumentException("ProductID is not the same");

   ...

 }

}

So:

 public Interface IProductDAO {            
      public void saveProduct(final Product product);                               
 }

I would like to know which one is the most 'natural' approach according best software development practices. If there are other approaches, they will be welcomed as well.

Aucun commentaire:

Enregistrer un commentaire