Data types using C#

Property Attributes

The following are property attributes grouped as:

  • Required attributes
  • Optional attributes
  • Validation attributes

Required Attributes

These are required attributes:

  • ImmutableFieldId Attribute
  • StoreFieldType Attribute

ImmutableFieldId Attribute

This attribute specifies a unique ID for the property. The ID should be unique for the running C1 CMS solution. The value of the argument should be a string representation of a GUID. This unique ID is used by C1 CMS to identify the property. This means that it is possible to rename the property and the type would still work. Though, when coding your own type and using the property name in code (your own or code that might use yours), the same compile rules apply as with normal C# interfaces. Example:

[ImmutableFieldId("{D261B424-3629-4e00-9D24-BDA763DE8DD8}")]

StoreFieldType Attribute

This attribute specifies the underlying type of a given property. This information is passed to the data provider (i.e. XML or SQL) and used by the data provider when a type needs to be created on the underlying data layer. The first argument is required and should be the value of PhysicalStoreFieldType. There are two optional arguments, the first one is the max length and can only be used with the first argument value of PhysicalStoreFieldType.String. The max length value specifies that the strings stored in the given property will never be longer than the specified max length. The second one is the numeric precision and numeric scale and these can only be used with the first argument value of PhysicalStoreFieldType.Decimal and specify the precision and scale of the decimal values that the property will hold. All properties can be made nullable by using the optional argument IsNullable.

Below is a total list of possible PhysicalStoreFieldTypes:

[StoreFieldType(PhysicalStoreFieldType.Boolean)]
bool IsChild { get; set; }
[StoreFieldType(PhysicalStoreFieldType.DateTime)]
DateTime Created { get; set; }
[StoreFieldType(PhysicalStoreFieldType.Decimal, 10, 2)]
decimal Price { get; set; }
[StoreFieldType(PhysicalStoreFieldType.Guid)]
Guid Id { get; set; }
[StoreFieldType(PhysicalStoreFieldType.Integer)]
int Level { get; set; }
[StoreFieldType(PhysicalStoreFieldType.LargeString)]
string Description { get; set; }
[StoreFieldType(PhysicalStoreFieldType.Long)]
long Distance { get; set; }
[StoreFieldType(PhysicalStoreFieldType.String, 128)]
string Name { get; set; }

Example of the nullable:

[StoreFieldType(PhysicalStoreFieldType.Guid, IsNullable=true)]
Guid Id { get; set; }

Optional Attributes

These are optional attributes:

  • DefaultFieldValue Attribute
  • ForeignKey Attribute
  • FunctionBasedNewInstanceDefaultFieldValue Attribute

DefaultFieldValue Attribute

This attribute is passed to the data provider and thereby the underlying data layer (i.e. SQL or XML). This is not used for giving your properties a value when an instance of your type is first created (DataConnection.New<T>()). For assigned values to properties of newly created (DataConnection.New<T>()) instances of your type, see the FunctionBasedNewInstanceDefaultFieldValue Attribute section for more information. But this is used if, for example, a new property is added to your type and the SQL data provider needs to extend the given table with a new column. Then all existing records will get the value of this attribute’s value. You can use one of the following attributes: DefaultFieldStringValueAttribute, DefaultFieldIntValueAttribute, DefaultFieldDecimalValueAttribute, DefaultFieldBoolValueAttribute, DefaultFieldGuidValueAttribute, DefaultFieldNewGuidValueAttribute or DefaultFieldNowDateTimeValueAttribute.

ForeignKey Attribute

This attribute can be used to specify a relation from your type to another existing data type. In C1 CMS, when given a data item where the type is “pointing” to one or more other types, it is easy to get the data items that are “pointed” to. Also, cascade deletes and reference integrity are performed when AddNew, Update or Delete is performed for types with references to other types.

The two primary and required arguments for this attribute are the type that your type is “pointing” to and the name of the key property of that type.

 

There are also four optional arguments. AllowCascadeDeletes can be specified to enable/disable cascade deletes. NullReferenceValue, NullReferenceValueType, NullableString are used to control the behavior of null references. NullReferenceValue specifies the value for a null reference. NullReferenceValueType specifies the type of the null reference value.

Ex: “{00000000-0000-0000-0000-000000000000}” as the value and typeof(Guid) as the value type

NullableString can be set to true to allow null to be used as a non-reference value.

For sample code, please see “Static IData Types”, Example #2.

FunctionBasedNewInstanceDefaultFieldValue Attribute

This attribute can be used to have C1 CMS assign a value to newly build instances of your data type. This is not the same as using the DefaultFieldValue attribute, see DefaultFieldValue Attribute for more information. The value of the argument to this attribute is a function markup call. A call to this function will be made when DataConnection.New<T>() is done. And the value of that function call will be assigned to the property where this attribute is specified.

Validation Attributes

Common for all validation attributes is that when adding or updating a data item, validation is done by C1 CMS and an exception is thrown if validation fails. When editing data on the client, these exceptions are shown as balloons if validation fails. Some of the validation attributes will result in client side validation.

NotNullValidator

Use this attribute to specify that the property (if nullable, including strings) shall have a value. This attribute will also be used by the client (client-side validation) so that the user will be shown a warning (“Required” in red) if no value is entered.

RegexValidator

Use this attribute to specify that property should conform to the given regular expression. This attribute will also be used by the client (client-side validation) so that the user will be shown a warning if the value does not conform to the given regular expression.

DecimalPrecisionValidatorAttribute

Use this attribute to specify that the property should conform to the given decimal precision.

GuidNotEmptyAttribute

Use this attribute to specify that empty GUIDs (0-guids) are not allowed as a value for the given property.

IntegerRangeValidatorAttribute

Use this attribute to specify the range that the value of the given property must lie within.

StringLengthValidatorAttribute

This attribute allows the length of the string-property value to lie within the range indicated by two numbers.

NullStringLengthValidatorAttribute

This attribute allows the string-property value to be a null string or a string the length of which lie within the range indicated by two numbers.