DataType
IDataType is datatype interface for basic types. It mandates following properties: Nullable, Referable and Description.
/// <summary>Root interface for datatype interfaces</summary>
public interface IDataType : IDataTypeBase, INominable, IAnnotable, IDefaultValued<object?>
{
/// <summary>The container for this datatype has capability for holding non-assigned state distinction.</summary>
bool Nullable { get; set; }
/// <summary>Is instance of the type referable by identity.</summary>
bool Referable { get; set; }
/// <summary>Datatype description</summary>
[IgnoreCompare] string? Description { get; set; }
}
INominable exposes Name property.
/// <summary>Contains Name property.</summary>
public interface INominable
{
/// <summary>Object name</summary>
IIdentity? Name { get; set; }
}
IAnnotable exposes Annotations property.
/// <summary>Annotable type</summary>
public interface IAnnotable
{
/// <summary>Annotations, typically <see cref="Attribute"/>s or strings, but always easily serializable.</summary>
object[] Annotations { get; set; }
}
IDefaultValued exposes the DefaultValue property.
/// <summary>Contains default value property.</summary>
public interface IDefaultValued<T>
{
/// <summary>Possible default value of <typeparamref name="T"/>.</summary>
T DefaultValue { get; set; }
}
IDataType is base interface for third party datatypes with name, description and nullability and referability capabilities.
public interface IMessageType : IDataType
{
IDataTypeBase Request { get; set; }
IDataTypeBase Response { get; set; }
}
DataType is the base class for third party classes.
public class MessageType : DataType, IMessageType
{
/// <summary>Interface Type</summary>
[IgnoreDataMember] public override Type IntfType { get => typeof(IMessageType); set => CoreMessages.Instance.BadReadOnly.Throw(); }
[IgnoreDataMember] protected IDataTypeBase request = null!;
[IgnoreDataMember] protected IDataTypeBase response = null!;
public IDataTypeBase Request { get => request; set => this.AssertWritable().request = value; }
public IDataTypeBase Response { get => response; set => this.AssertWritable().response = value; }
/// <summary>Check validation.</summary>
protected override void InternalValidate(ref StructList1<IMessage> errorCodes)
{
// Call base implementation
base.InternalValidate(ref errorCodes);
// Test properties
if (Request == null) errorCodes.Add(DataTypeMessages.Instance.BadFieldNotFound.New(nameof(Request)));
if (Response == null) errorCodes.Add(DataTypeMessages.Instance.BadFieldNotFound.New(nameof(Response)));
}
}
Values are assignable with initializer.
// Create message description
IMessageType messageType = new MessageType
{
Name = Identities.CreateName("LookupMessage"),
Description = "Table lookup message",
Annotations = new object[0],
DefaultValue = null,
Nullable = false,
Referable = true,
Request = DataTypes.String,
Response = DataTypes.String
}
.SetReadOnly();
And values are assignable with extension method setters.
// Create message description
IMessageType messageType = new MessageType
{
Request = DataTypes.String,
Response = DataTypes.String
}
.SetName(Identities.CreateName("LookupMessage"))
.SetDescription("Table lookup message")
.SetAnnotations(new object[0])
.SetDefaultValue<MessageType, object?>(null)
.SetNullable(false)
.SetReferable(true)
.SetReadOnly();
Full Example
Full example
using System;
using System.Runtime.Serialization;
using Avalanche.DataType;
using Avalanche.Identity;
using Avalanche.Utilities;
using Avalanche.Message;
public class datatype
{
public static void Run()
{
{
// <01>
// Create message description
IMessageType messageType = new MessageType
{
Name = Identities.CreateName("LookupMessage"),
Description = "Table lookup message",
Annotations = new object[0],
DefaultValue = null,
Nullable = false,
Referable = true,
Request = DataTypes.String,
Response = DataTypes.String
}
.SetReadOnly();
// </01>
}
{
// <02>
// Create message description
IMessageType messageType = new MessageType
{
Request = DataTypes.String,
Response = DataTypes.String
}
.SetName(Identities.CreateName("LookupMessage"))
.SetDescription("Table lookup message")
.SetAnnotations(new object[0])
.SetDefaultValue<MessageType, object?>(null)
.SetNullable(false)
.SetReferable(true)
.SetReadOnly();
// </02>
}
}
// <100>
public interface IMessageType : IDataType
{
IDataTypeBase Request { get; set; }
IDataTypeBase Response { get; set; }
}
// </100>
// <101>
public class MessageType : DataType, IMessageType
{
/// <summary>Interface Type</summary>
[IgnoreDataMember] public override Type IntfType { get => typeof(IMessageType); set => CoreMessages.Instance.BadReadOnly.Throw(); }
[IgnoreDataMember] protected IDataTypeBase request = null!;
[IgnoreDataMember] protected IDataTypeBase response = null!;
public IDataTypeBase Request { get => request; set => this.AssertWritable().request = value; }
public IDataTypeBase Response { get => response; set => this.AssertWritable().response = value; }
/// <summary>Check validation.</summary>
protected override void InternalValidate(ref StructList1<IMessage> errorCodes)
{
// Call base implementation
base.InternalValidate(ref errorCodes);
// Test properties
if (Request == null) errorCodes.Add(DataTypeMessages.Instance.BadFieldNotFound.New(nameof(Request)));
if (Response == null) errorCodes.Add(DataTypeMessages.Instance.BadFieldNotFound.New(nameof(Response)));
}
}
// </101>
}