GetPropertyValueOrException method

Object? GetPropertyValueOrException(
  1. PropertyDefinition propertyDefinition,
  2. OutParam<ServiceLocalException> serviceExceptionOutParam
)
Gets the property value. The property definition. Exception that would be raised if there's an error retrieving the property.

Implementation

/* private */
Object? GetPropertyValueOrException(
  PropertyDefinition propertyDefinition,
  OutParam<ServiceLocalException> serviceExceptionOutParam,
) {
  OutParam<Object> propertyValueOutParam = OutParam<Object>();
  propertyValueOutParam.param = null;
  serviceExceptionOutParam.param = null;

  if (propertyDefinition.Version!.index >
      Owner!.Service.RequestedServerVersion.index) {
    serviceExceptionOutParam.param = ServiceVersionException("""string.Format(
                                      Strings.PropertyIncompatibleWithRequestVersion,
                                      propertyDefinition.Name,
                                      propertyDefinition.Version)""");
    return null;
  }

  if (TryGetValue(propertyDefinition, propertyValueOutParam)) {
    // If the requested property is in the bag, return it.
    return propertyValueOutParam.param;
  } else {
    if (propertyDefinition.HasFlagWithoutExchangeVersion(
      PropertyDefinitionFlags.AutoInstantiateOnRead,
    )) {
      // The requested property is an auto-instantiate-on-read property
      ComplexPropertyDefinitionBase complexPropertyDefinition =
          propertyDefinition as ComplexPropertyDefinitionBase;

      ComplexProperty propertyValue =
          complexPropertyDefinition.CreatePropertyInstance(Owner);
      propertyValueOutParam.param = propertyValue;

      InitComplexProperty(propertyValue);
      _properties[propertyDefinition] = propertyValue;
    } else {
      // If the property is not the Id (we need to let developers read the Id when it's null) and if has
      // not been loaded, we throw.
      if (propertyDefinition != Owner!.GetIdPropertyDefinition()) {
        if (!IsPropertyLoaded(propertyDefinition)) {
          serviceExceptionOutParam.param =
              ServiceObjectPropertyException.withMessage(
                "Strings.MustLoadOrAssignPropertyBeforeAccess",
                propertyDefinition,
              );
          return null;
        }

        // Non-nullable properties (int, bool, etc.) must be assigned or loaded; cannot return null value.
        if (!propertyDefinition.IsNullable) {
          String errorMessage = _IsRequestedProperty(propertyDefinition)
              ? "Strings.ValuePropertyNotLoaded"
              : "Strings.ValuePropertyNotAssigned";
          serviceExceptionOutParam.param =
              ServiceObjectPropertyException.withMessage(
                errorMessage,
                propertyDefinition,
              );
        }
      }
    }

    return propertyValueOutParam.param;
  }
}