Mod4j: Modeling for Java using Domain Specific Languages

translator fromDto methode vereist een "target".

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Not A Bug
  • Affects Version/s: None
  • Fix Version/s: 1.1.0
  • Component/s: DSL for Services
  • Labels:
    None
  • Number of attachments :
    0

Description

Waarom vereist deze methode als 2de parameter een "target"? Dit heeft volgens mij geen nut want in de fromDto methode wordt wanneer deze null is alsnog geinstantieerd en gebruikt als return value.

medewerker = theMedewerkerDtoTranslator.fromDto(medewerkerDto, medewerker);

Activity

Hide
Johan Vogelzang added a comment -

Typically the fromDto method is used in two situations:

  • For instance if you have a new CustomerDto that you want to store. Here fromDto must create a new Customer.
  • If Your CustomerDto contains new data for a an existing Customer, you need to update this Customer. Here the fromDto method must not create a new Customer, therefor a existing Customer can be passed as parameter (target). In the code sample below you can see this usage (borrowed from the RecordShop). But maybe you need to do this your self, for instance in the implementation of a custom service mehtod.

For the first situation we could generated a overloaded fromDto method that takes only a dto as parameter, so you don't need pass null's.

/**
 * {@inheritDoc}
 */
public SimpleCustomerDto updateCustomer(SimpleCustomerDto object) {

   Customer result = null;
       try {
		result = theCustomerServiceModelDomainService.readCustomer(object.getId());
	} catch (IllegalArgumentException e) {
		throw new ServiceException(
			"Can not update Customer for SimpleCustomerDto", e);
	}
	if (result == null)
		throw new ServiceException(
				"Can not update Customer for SimpleCustomerDto, because the corresponding Customer does not exist.");

	result = theSimpleCustomerDtoTranslator.fromDto(object, result);
	result = theCustomerServiceModelDomainService.updateCustomer(result);

	return theSimpleCustomerDtoTranslator.toDto(result);
}
Show
Johan Vogelzang added a comment - Typically the fromDto method is used in two situations:
  • For instance if you have a new CustomerDto that you want to store. Here fromDto must create a new Customer.
  • If Your CustomerDto contains new data for a an existing Customer, you need to update this Customer. Here the fromDto method must not create a new Customer, therefor a existing Customer can be passed as parameter (target). In the code sample below you can see this usage (borrowed from the RecordShop). But maybe you need to do this your self, for instance in the implementation of a custom service mehtod.
For the first situation we could generated a overloaded fromDto method that takes only a dto as parameter, so you don't need pass null's.
/**
 * {@inheritDoc}
 */
public SimpleCustomerDto updateCustomer(SimpleCustomerDto object) {

   Customer result = null;
       try {
		result = theCustomerServiceModelDomainService.readCustomer(object.getId());
	} catch (IllegalArgumentException e) {
		throw new ServiceException(
			"Can not update Customer for SimpleCustomerDto", e);
	}
	if (result == null)
		throw new ServiceException(
				"Can not update Customer for SimpleCustomerDto, because the corresponding Customer does not exist.");

	result = theSimpleCustomerDtoTranslator.fromDto(object, result);
	result = theCustomerServiceModelDomainService.updateCustomer(result);

	return theSimpleCustomerDtoTranslator.toDto(result);
}
Hide
Jos Warmer added a comment -

This is intended behaviour as Johan described.

Show
Jos Warmer added a comment - This is intended behaviour as Johan described.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: