Explicit Field Assignment

An explicit field assignment is one where fields with different names are matched. It is represented in the model by adding an assignable relationship between the two classes, and then adding attributes to be matched to the both sides of the assignment. Any fields which are not explicitly linked will be treated as default assignment fields.

The following classes are used to illustrate this.

In an assignable relationship between the two classes Address and BankBranchStruct fields can be explicitly mapped; e.g. BankBranchStruct.cityID matched with Address.cityCode. In Rational Software Architect this is shown in Role: fields (RoleA & RoleB) of the General tab of the assignable relationship with the linked pair, cityID in one Role field and cityCode in the other. All the other common fields (e.g. AddressLine1, etc.) are handled automatically by the generator.

For instance, the generated code without the explicit field assignment would appear as shown below:

public curam.util.testmodel.struct.BankBranchStruct
    assign(final curam.util.testmodel.struct.AddressDtls v)
{
  addressID = v.addressID;
  addressLine1 = v.addressLine1;
  addressLine2 = v.addressLine2;
  addressLine3 = v.addressLine3;
  addressLine4 = v.addressLine4;
  countryCode = v.countryCode;
  postalCode = v.postalCode;
  regionCode = v.regionCode;
  return this;
}

With the explicit field assignment the following code is then added to the assign method: cityID = v.cityCode. The handcrafted Java to assign these structures would be as follows:

BankBranchStruct dtls = new BankBranchStruct();
AddressDtls addressDtls = new AddressDtls();
dtls.addressLine1 = addressDtls.addressLine1;
dtls.addressLine2 = addressDtls.addressLine2;
dtls.addressLine3 = addressDtls.addressLine3;
dtls.addressLine4 = addressDtls.addressLine4;
dtls.cityID = addressDtls.cityCode;
dtls.countryCode = addressDtls.countryCode;
dtls.postalCode = addressDtls.postalCode;
dtls.regionCode = addressDtls.regionCode;

By using the generated assignment operator, these lines of code can be reduced to just one line as follows:

bankDtls.assign(addressDtls);