Wednesday, August 15, 2007

Properly modeling and implementing the modify method

Implementing the modify method can be a bit tricky. A standardized modify method has to deal with optimistic locking and business level validation.

Here is the general pattern

Model

In your model you should have the three standard layers: Facade, Service Layer and Entity. The following are the class diagrams for each layer:

Entity:

Service Layer:

One note, if the sl.SampleDetails should not modify every field of Sample, then model the following association instead:

Facade:

Code

In the sl.MaintainSample there is a modifySample method that looks like this:

public ValidationResult modifySample(SampleDetails sampleDetails) {
  SampleKey key = new SampleKey();
  key.sampleID = sampleDetails.dtls.sampleID;
  Sample sampleObj = SampleFactory.newInstance();
 
  // Read for update
  SampleDtls dtls = sampleObj.read(key, true);
 
  // Perform validation here
  // For now everything passes.
  ValidationResult validationResult = new ValidationResult();
  validationResult.validInd = true;
 
  dtls.assign(sampleDetails.dtls);
  // If you are not aggregating the entity directy then use
  // dtls.assign(sampleDetails);
 
  sampleObj.modify(key, dtls);
 
  return validationResult;
}

In the facade, there is a modifySample method as well which looks like this:
public ModifySampleResult modifySample(ModifySampleDetails details) {
  SecurityImplementationFactory.register();
  ModifySampleResult result = new ModifySampleResult();
  result.validationResults = 
    MaintainSampleFactory.newInstance().modifySample(details.details);
 
  return result;
}

In the UIM you need to make sure to pass in the versionNo field as well if you are using Optimistic Locking.
<CONNECT>
  <SOURCE NAME="DISPLAY" PROPERTY="versionNo" />
  <TARGET NAME="ACTION" PROPERTY="versionNo" />
</CONNECT>

0 comments: