RSCG – DomainPrimitives
RSCG – DomainPrimitives
name | DomainPrimitives |
nuget |
https://www.nuget.org/packages/AltaSoft.DomainPrimitives.Generator https://www.nuget.org/packages/AltaSoft.DomainPrimitives.Abstractions |
link | https://github.com/altasoft/DomainPrimitives |
author | Alta Software – Teimuraz Nikolaishvili |
One of the most complete and mature libraries for DomainPrimitives in .NET
This is how you can use DomainPrimitives .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net8.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="AltaSoft.DomainPrimitives.Abstractions" Version="1.0.3" /> <PackageReference Include="AltaSoft.DomainPrimitives.Generator" Version="1.0.3" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="all" ExcludeAssets="runtime" /> </ItemGroup> <PropertyGroup> <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath> </PropertyGroup> <PropertyGroup> <!--<DomainPrimitiveGenerator_GenerateJsonConverters>false</DomainPrimitiveGenerator_GenerateJsonConverters>--> <!--<DomainPrimitiveGenerator_GenerateTypeConverters>false</DomainPrimitiveGenerator_GenerateTypeConverters>--> <DomainPrimitiveGenerator_GenerateSwaggerConverters>false</DomainPrimitiveGenerator_GenerateSwaggerConverters> </PropertyGroup> <ItemGroup> <!--<CompilerVisibleProperty Include="DomainPrimitiveGenerator_GenerateTypeConverters" />--> <!--<CompilerVisibleProperty Include="DomainPrimitiveGenerator_GenerateJsonConverters" />--> <CompilerVisibleProperty Include="DomainPrimitiveGenerator_GenerateSwaggerConverters" /> </ItemGroup> </Project>
The code that you will use is
// See https://aka.ms/new-console-template for more information using DomainPrimitives; var year = new YearDate(1970); var month = new MonthDate(4); var day = new DayDate(16); year += 1; var p=new Person(year,month,day); Console.WriteLine(p.DOB);
using AltaSoft.DomainPrimitives.Abstractions; namespace DomainPrimitives; public readonly partial record struct YearDate : IDomainValue<int> { public static void Validate(int value) { if (value <= 0) throw new InvalidDomainValueException("year must be positive"); } public static int Default => 1; } public readonly partial record struct MonthDate : IDomainValue<int> { public static void Validate(int value) { if (value <= 0) throw new InvalidDomainValueException("year must be positive"); } public static int Default => 1; } public readonly partial record struct DayDate : IDomainValue<int> { public static void Validate(int value) { if (value <= 0) throw new InvalidDomainValueException("year must be positive"); } public static int Default => 1; }
namespace DomainPrimitives; internal class Person { public Person(YearDate year,MonthDate month,DayDate day) { DOB = new DateOnly(year,month,day); } public DateOnly DOB { get; private set; } }
The code that is generated is
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a AltaSoft.DomainPrimitives.Generator v1.0.0 // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ #nullable enable using System; using System.Numerics; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Text.Json.Serialization; using DomainPrimitives.Converters; using System.ComponentModel; namespace DomainPrimitives; [JsonConverter(typeof(DayDateJsonConverter))] [TypeConverter(typeof(DayDateTypeConverter))] [DebuggerDisplay("{_valueOrDefault}")] public readonly partial record struct DayDate : IAdditionOperators<DayDate, DayDate, DayDate>, ISubtractionOperators<DayDate, DayDate, DayDate>, IMultiplyOperators<DayDate, DayDate, DayDate>, IDivisionOperators<DayDate, DayDate, DayDate>, IModulusOperators<DayDate, DayDate, DayDate>, IComparisonOperators<DayDate, DayDate, bool>, IComparable, IComparable<DayDate>, IParsable<DayDate>, IConvertible { private int _valueOrDefault => _isInitialized ? _value : Default; [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly int _value; [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly bool _isInitialized; /// <summary> /// Initializes a new instance of the <see cref="DayDate"/> class by validating the specified <see cref="int"/> value using <see cref="Validate"/> static method. /// </summary> /// <param name="value">The value to be validated..</param> public DayDate(int value) { Validate(value); _value = value; _isInitialized = true; } [Obsolete("Domain primitive cannot be created using empty Ctor", true)] public DayDate() : this(Default) { } /// <summary> /// <summary>Implicit conversion from <see cref = "int"/> to <see cref = "DayDate"/></summary> /// </summary> public static implicit operator DayDate(int value) => new(value); /// <summary> /// <summary>Implicit conversion from <see cref = "int?"/> to <see cref = "DayDate?"/></summary> /// </summary> [return: NotNullIfNotNull(nameof(value))] public static implicit operator DayDate?(int? value) => value is null ? null : new(value.Value); /// <summary> /// <summary>Implicit conversion from <see cref = "DayDate"/> to <see cref = "int"/></summary> /// </summary> public static implicit operator int(DayDate value) => (int)value._valueOrDefault; /// <inheritdoc/> public static DayDate operator +(DayDate left, DayDate right) => new(left._valueOrDefault + right._valueOrDefault); /// <inheritdoc/> public static DayDate operator -(DayDate left, DayDate right) => new(left._valueOrDefault - right._valueOrDefault); /// <inheritdoc/> public static DayDate operator *(DayDate left, DayDate right) => new(left._valueOrDefault * right._valueOrDefault); /// <inheritdoc/> public static DayDate operator /(DayDate left, DayDate right) => new(left._valueOrDefault / right._valueOrDefault); /// <inheritdoc/> public static DayDate operator %(DayDate left, DayDate right) => new(left._valueOrDefault % right._valueOrDefault); /// <inheritdoc/> public int CompareTo(object? value) { if (value is null) return 1; if (value is DayDate c) return CompareTo(c); throw new ArgumentException("Object is not a DayDate", nameof(value)); } /// <inheritdoc/> public int CompareTo(DayDate other) => _valueOrDefault.CompareTo(other._valueOrDefault); /// <inheritdoc/> public static bool operator <(DayDate left, DayDate right) => left._valueOrDefault < right._valueOrDefault; /// <inheritdoc/> public static bool operator <=(DayDate left, DayDate right) => left._valueOrDefault <= right._valueOrDefault; /// <inheritdoc/> public static bool operator >(DayDate left, DayDate right) => left._valueOrDefault > right._valueOrDefault; /// <inheritdoc/> public static bool operator >=(DayDate left, DayDate right) => left._valueOrDefault >= right._valueOrDefault; /// <inheritdoc/> public static DayDate Parse(string s, IFormatProvider? provider) => int.Parse(s, provider); /// <inheritdoc/> public static bool TryParse(string? s, IFormatProvider? provider, out DayDate result) { if (int.TryParse(s, provider, out var value)) { result = new DayDate(value); return true; } result = default; return false; } /// <inheritdoc/> public override string ToString() => _valueOrDefault.ToString(); /// <inheritdoc/> TypeCode IConvertible.GetTypeCode() => ((IConvertible)_valueOrDefault).GetTypeCode(); /// <inheritdoc/> bool IConvertible.ToBoolean(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToBoolean(provider); /// <inheritdoc/> byte IConvertible.ToByte(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToByte(provider); /// <inheritdoc/> char IConvertible.ToChar(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToChar(provider); /// <inheritdoc/> DateTime IConvertible.ToDateTime(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToDateTime(provider); /// <inheritdoc/> decimal IConvertible.ToDecimal(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToDecimal(provider); /// <inheritdoc/> double IConvertible.ToDouble(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToDouble(provider); /// <inheritdoc/> short IConvertible.ToInt16(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToInt16(provider); /// <inheritdoc/> int IConvertible.ToInt32(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToInt32(provider); /// <inheritdoc/> long IConvertible.ToInt64(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToInt64(provider); /// <inheritdoc/> sbyte IConvertible.ToSByte(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToSByte(provider); /// <inheritdoc/> float IConvertible.ToSingle(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToSingle(provider); /// <inheritdoc/> string IConvertible.ToString(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToString(provider); /// <inheritdoc/> object IConvertible.ToType(Type conversionType, IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToType(conversionType, provider); /// <inheritdoc/> ushort IConvertible.ToUInt16(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToUInt16(provider); /// <inheritdoc/> uint IConvertible.ToUInt32(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToUInt32(provider); /// <inheritdoc/> ulong IConvertible.ToUInt64(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToUInt64(provider); }
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a AltaSoft.DomainPrimitives.Generator v1.0.0 // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ #nullable enable using DomainPrimitives; using System; using System.Text.Json; using System.Text.Json.Serialization; using System.Globalization; using System.Text.Json.Serialization.Metadata; using AltaSoft.DomainPrimitives.Abstractions; namespace DomainPrimitives.Converters; /// <summary> /// JsonConverter for <see cref = "DayDate"/> /// </summary> public sealed class DayDateJsonConverter : JsonConverter<DayDate> { /// <inheritdoc/> public override DayDate Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { try { return JsonInternalConverters.Int32Converter.Read(ref reader, typeToConvert, options); } catch (InvalidDomainValueException ex) { throw new JsonException(ex.Message); } } /// <inheritdoc/> public override void Write(Utf8JsonWriter writer, DayDate value, JsonSerializerOptions options) { JsonInternalConverters.Int32Converter.Write(writer, (int)value, options); } /// <inheritdoc/> public override DayDate ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { try { return JsonInternalConverters.Int32Converter.ReadAsPropertyName(ref reader, typeToConvert, options); } catch (InvalidDomainValueException ex) { throw new JsonException(ex.Message); } } /// <inheritdoc/> public override void WriteAsPropertyName(Utf8JsonWriter writer, DayDate value, JsonSerializerOptions options) { JsonInternalConverters.Int32Converter.WriteAsPropertyName(writer, (int)value, options); } }
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a AltaSoft.DomainPrimitives.Generator v1.0.0 // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ #nullable enable using DomainPrimitives; using System; using System.ComponentModel; using System.Globalization; using AltaSoft.DomainPrimitives.Abstractions; namespace DomainPrimitives.Converters; /// <summary> /// TypeConverter for <see cref = "DayDate"/> /// </summary> public sealed class DayDateTypeConverter : Int32Converter { /// <inheritdoc/> public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) { var result = base.ConvertFrom(context, culture, value); if (result is null) return null; try { return new DayDate((int)result); } catch (InvalidDomainValueException ex) { throw new FormatException("Cannot parse DayDate", ex); } } }
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a AltaSoft.DomainPrimitives.Generator v1.0.0 // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ #nullable enable using System; using System.Numerics; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Text.Json.Serialization; using DomainPrimitives.Converters; using System.ComponentModel; namespace DomainPrimitives; [JsonConverter(typeof(MonthDateJsonConverter))] [TypeConverter(typeof(MonthDateTypeConverter))] [DebuggerDisplay("{_valueOrDefault}")] public readonly partial record struct MonthDate : IAdditionOperators<MonthDate, MonthDate, MonthDate>, ISubtractionOperators<MonthDate, MonthDate, MonthDate>, IMultiplyOperators<MonthDate, MonthDate, MonthDate>, IDivisionOperators<MonthDate, MonthDate, MonthDate>, IModulusOperators<MonthDate, MonthDate, MonthDate>, IComparisonOperators<MonthDate, MonthDate, bool>, IComparable, IComparable<MonthDate>, IParsable<MonthDate>, IConvertible { private int _valueOrDefault => _isInitialized ? _value : Default; [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly int _value; [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly bool _isInitialized; /// <summary> /// Initializes a new instance of the <see cref="MonthDate"/> class by validating the specified <see cref="int"/> value using <see cref="Validate"/> static method. /// </summary> /// <param name="value">The value to be validated..</param> public MonthDate(int value) { Validate(value); _value = value; _isInitialized = true; } [Obsolete("Domain primitive cannot be created using empty Ctor", true)] public MonthDate() : this(Default) { } /// <summary> /// <summary>Implicit conversion from <see cref = "int"/> to <see cref = "MonthDate"/></summary> /// </summary> public static implicit operator MonthDate(int value) => new(value); /// <summary> /// <summary>Implicit conversion from <see cref = "int?"/> to <see cref = "MonthDate?"/></summary> /// </summary> [return: NotNullIfNotNull(nameof(value))] public static implicit operator MonthDate?(int? value) => value is null ? null : new(value.Value); /// <summary> /// <summary>Implicit conversion from <see cref = "MonthDate"/> to <see cref = "int"/></summary> /// </summary> public static implicit operator int(MonthDate value) => (int)value._valueOrDefault; /// <inheritdoc/> public static MonthDate operator +(MonthDate left, MonthDate right) => new(left._valueOrDefault + right._valueOrDefault); /// <inheritdoc/> public static MonthDate operator -(MonthDate left, MonthDate right) => new(left._valueOrDefault - right._valueOrDefault); /// <inheritdoc/> public static MonthDate operator *(MonthDate left, MonthDate right) => new(left._valueOrDefault * right._valueOrDefault); /// <inheritdoc/> public static MonthDate operator /(MonthDate left, MonthDate right) => new(left._valueOrDefault / right._valueOrDefault); /// <inheritdoc/> public static MonthDate operator %(MonthDate left, MonthDate right) => new(left._valueOrDefault % right._valueOrDefault); /// <inheritdoc/> public int CompareTo(object? value) { if (value is null) return 1; if (value is MonthDate c) return CompareTo(c); throw new ArgumentException("Object is not a MonthDate", nameof(value)); } /// <inheritdoc/> public int CompareTo(MonthDate other) => _valueOrDefault.CompareTo(other._valueOrDefault); /// <inheritdoc/> public static bool operator <(MonthDate left, MonthDate right) => left._valueOrDefault < right._valueOrDefault; /// <inheritdoc/> public static bool operator <=(MonthDate left, MonthDate right) => left._valueOrDefault <= right._valueOrDefault; /// <inheritdoc/> public static bool operator >(MonthDate left, MonthDate right) => left._valueOrDefault > right._valueOrDefault; /// <inheritdoc/> public static bool operator >=(MonthDate left, MonthDate right) => left._valueOrDefault >= right._valueOrDefault; /// <inheritdoc/> public static MonthDate Parse(string s, IFormatProvider? provider) => int.Parse(s, provider); /// <inheritdoc/> public static bool TryParse(string? s, IFormatProvider? provider, out MonthDate result) { if (int.TryParse(s, provider, out var value)) { result = new MonthDate(value); return true; } result = default; return false; } /// <inheritdoc/> public override string ToString() => _valueOrDefault.ToString(); /// <inheritdoc/> TypeCode IConvertible.GetTypeCode() => ((IConvertible)_valueOrDefault).GetTypeCode(); /// <inheritdoc/> bool IConvertible.ToBoolean(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToBoolean(provider); /// <inheritdoc/> byte IConvertible.ToByte(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToByte(provider); /// <inheritdoc/> char IConvertible.ToChar(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToChar(provider); /// <inheritdoc/> DateTime IConvertible.ToDateTime(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToDateTime(provider); /// <inheritdoc/> decimal IConvertible.ToDecimal(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToDecimal(provider); /// <inheritdoc/> double IConvertible.ToDouble(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToDouble(provider); /// <inheritdoc/> short IConvertible.ToInt16(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToInt16(provider); /// <inheritdoc/> int IConvertible.ToInt32(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToInt32(provider); /// <inheritdoc/> long IConvertible.ToInt64(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToInt64(provider); /// <inheritdoc/> sbyte IConvertible.ToSByte(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToSByte(provider); /// <inheritdoc/> float IConvertible.ToSingle(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToSingle(provider); /// <inheritdoc/> string IConvertible.ToString(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToString(provider); /// <inheritdoc/> object IConvertible.ToType(Type conversionType, IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToType(conversionType, provider); /// <inheritdoc/> ushort IConvertible.ToUInt16(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToUInt16(provider); /// <inheritdoc/> uint IConvertible.ToUInt32(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToUInt32(provider); /// <inheritdoc/> ulong IConvertible.ToUInt64(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToUInt64(provider); }
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a AltaSoft.DomainPrimitives.Generator v1.0.0 // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ #nullable enable using DomainPrimitives; using System; using System.Text.Json; using System.Text.Json.Serialization; using System.Globalization; using System.Text.Json.Serialization.Metadata; using AltaSoft.DomainPrimitives.Abstractions; namespace DomainPrimitives.Converters; /// <summary> /// JsonConverter for <see cref = "MonthDate"/> /// </summary> public sealed class MonthDateJsonConverter : JsonConverter<MonthDate> { /// <inheritdoc/> public override MonthDate Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { try { return JsonInternalConverters.Int32Converter.Read(ref reader, typeToConvert, options); } catch (InvalidDomainValueException ex) { throw new JsonException(ex.Message); } } /// <inheritdoc/> public override void Write(Utf8JsonWriter writer, MonthDate value, JsonSerializerOptions options) { JsonInternalConverters.Int32Converter.Write(writer, (int)value, options); } /// <inheritdoc/> public override MonthDate ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { try { return JsonInternalConverters.Int32Converter.ReadAsPropertyName(ref reader, typeToConvert, options); } catch (InvalidDomainValueException ex) { throw new JsonException(ex.Message); } } /// <inheritdoc/> public override void WriteAsPropertyName(Utf8JsonWriter writer, MonthDate value, JsonSerializerOptions options) { JsonInternalConverters.Int32Converter.WriteAsPropertyName(writer, (int)value, options); } }
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a AltaSoft.DomainPrimitives.Generator v1.0.0 // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ #nullable enable using DomainPrimitives; using System; using System.ComponentModel; using System.Globalization; using AltaSoft.DomainPrimitives.Abstractions; namespace DomainPrimitives.Converters; /// <summary> /// TypeConverter for <see cref = "MonthDate"/> /// </summary> public sealed class MonthDateTypeConverter : Int32Converter { /// <inheritdoc/> public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) { var result = base.ConvertFrom(context, culture, value); if (result is null) return null; try { return new MonthDate((int)result); } catch (InvalidDomainValueException ex) { throw new FormatException("Cannot parse MonthDate", ex); } } }
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a AltaSoft.DomainPrimitives.Generator v1.0.0 // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ #nullable enable using System; using System.Numerics; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Text.Json.Serialization; using DomainPrimitives.Converters; using System.ComponentModel; namespace DomainPrimitives; [JsonConverter(typeof(YearDateJsonConverter))] [TypeConverter(typeof(YearDateTypeConverter))] [DebuggerDisplay("{_valueOrDefault}")] public readonly partial record struct YearDate : IAdditionOperators<YearDate, YearDate, YearDate>, ISubtractionOperators<YearDate, YearDate, YearDate>, IMultiplyOperators<YearDate, YearDate, YearDate>, IDivisionOperators<YearDate, YearDate, YearDate>, IModulusOperators<YearDate, YearDate, YearDate>, IComparisonOperators<YearDate, YearDate, bool>, IComparable, IComparable<YearDate>, IParsable<YearDate>, IConvertible { private int _valueOrDefault => _isInitialized ? _value : Default; [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly int _value; [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly bool _isInitialized; /// <summary> /// Initializes a new instance of the <see cref="YearDate"/> class by validating the specified <see cref="int"/> value using <see cref="Validate"/> static method. /// </summary> /// <param name="value">The value to be validated..</param> public YearDate(int value) { Validate(value); _value = value; _isInitialized = true; } [Obsolete("Domain primitive cannot be created using empty Ctor", true)] public YearDate() : this(Default) { } /// <summary> /// <summary>Implicit conversion from <see cref = "int"/> to <see cref = "YearDate"/></summary> /// </summary> public static implicit operator YearDate(int value) => new(value); /// <summary> /// <summary>Implicit conversion from <see cref = "int?"/> to <see cref = "YearDate?"/></summary> /// </summary> [return: NotNullIfNotNull(nameof(value))] public static implicit operator YearDate?(int? value) => value is null ? null : new(value.Value); /// <summary> /// <summary>Implicit conversion from <see cref = "YearDate"/> to <see cref = "int"/></summary> /// </summary> public static implicit operator int(YearDate value) => (int)value._valueOrDefault; /// <inheritdoc/> public static YearDate operator +(YearDate left, YearDate right) => new(left._valueOrDefault + right._valueOrDefault); /// <inheritdoc/> public static YearDate operator -(YearDate left, YearDate right) => new(left._valueOrDefault - right._valueOrDefault); /// <inheritdoc/> public static YearDate operator *(YearDate left, YearDate right) => new(left._valueOrDefault * right._valueOrDefault); /// <inheritdoc/> public static YearDate operator /(YearDate left, YearDate right) => new(left._valueOrDefault / right._valueOrDefault); /// <inheritdoc/> public static YearDate operator %(YearDate left, YearDate right) => new(left._valueOrDefault % right._valueOrDefault); /// <inheritdoc/> public int CompareTo(object? value) { if (value is null) return 1; if (value is YearDate c) return CompareTo(c); throw new ArgumentException("Object is not a YearDate", nameof(value)); } /// <inheritdoc/> public int CompareTo(YearDate other) => _valueOrDefault.CompareTo(other._valueOrDefault); /// <inheritdoc/> public static bool operator <(YearDate left, YearDate right) => left._valueOrDefault < right._valueOrDefault; /// <inheritdoc/> public static bool operator <=(YearDate left, YearDate right) => left._valueOrDefault <= right._valueOrDefault; /// <inheritdoc/> public static bool operator >(YearDate left, YearDate right) => left._valueOrDefault > right._valueOrDefault; /// <inheritdoc/> public static bool operator >=(YearDate left, YearDate right) => left._valueOrDefault >= right._valueOrDefault; /// <inheritdoc/> public static YearDate Parse(string s, IFormatProvider? provider) => int.Parse(s, provider); /// <inheritdoc/> public static bool TryParse(string? s, IFormatProvider? provider, out YearDate result) { if (int.TryParse(s, provider, out var value)) { result = new YearDate(value); return true; } result = default; return false; } /// <inheritdoc/> public override string ToString() => _valueOrDefault.ToString(); /// <inheritdoc/> TypeCode IConvertible.GetTypeCode() => ((IConvertible)_valueOrDefault).GetTypeCode(); /// <inheritdoc/> bool IConvertible.ToBoolean(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToBoolean(provider); /// <inheritdoc/> byte IConvertible.ToByte(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToByte(provider); /// <inheritdoc/> char IConvertible.ToChar(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToChar(provider); /// <inheritdoc/> DateTime IConvertible.ToDateTime(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToDateTime(provider); /// <inheritdoc/> decimal IConvertible.ToDecimal(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToDecimal(provider); /// <inheritdoc/> double IConvertible.ToDouble(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToDouble(provider); /// <inheritdoc/> short IConvertible.ToInt16(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToInt16(provider); /// <inheritdoc/> int IConvertible.ToInt32(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToInt32(provider); /// <inheritdoc/> long IConvertible.ToInt64(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToInt64(provider); /// <inheritdoc/> sbyte IConvertible.ToSByte(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToSByte(provider); /// <inheritdoc/> float IConvertible.ToSingle(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToSingle(provider); /// <inheritdoc/> string IConvertible.ToString(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToString(provider); /// <inheritdoc/> object IConvertible.ToType(Type conversionType, IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToType(conversionType, provider); /// <inheritdoc/> ushort IConvertible.ToUInt16(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToUInt16(provider); /// <inheritdoc/> uint IConvertible.ToUInt32(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToUInt32(provider); /// <inheritdoc/> ulong IConvertible.ToUInt64(IFormatProvider? provider) => ((IConvertible)_valueOrDefault).ToUInt64(provider); }
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a AltaSoft.DomainPrimitives.Generator v1.0.0 // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ #nullable enable using DomainPrimitives; using System; using System.Text.Json; using System.Text.Json.Serialization; using System.Globalization; using System.Text.Json.Serialization.Metadata; using AltaSoft.DomainPrimitives.Abstractions; namespace DomainPrimitives.Converters; /// <summary> /// JsonConverter for <see cref = "YearDate"/> /// </summary> public sealed class YearDateJsonConverter : JsonConverter<YearDate> { /// <inheritdoc/> public override YearDate Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { try { return JsonInternalConverters.Int32Converter.Read(ref reader, typeToConvert, options); } catch (InvalidDomainValueException ex) { throw new JsonException(ex.Message); } } /// <inheritdoc/> public override void Write(Utf8JsonWriter writer, YearDate value, JsonSerializerOptions options) { JsonInternalConverters.Int32Converter.Write(writer, (int)value, options); } /// <inheritdoc/> public override YearDate ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { try { return JsonInternalConverters.Int32Converter.ReadAsPropertyName(ref reader, typeToConvert, options); } catch (InvalidDomainValueException ex) { throw new JsonException(ex.Message); } } /// <inheritdoc/> public override void WriteAsPropertyName(Utf8JsonWriter writer, YearDate value, JsonSerializerOptions options) { JsonInternalConverters.Int32Converter.WriteAsPropertyName(writer, (int)value, options); } }
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a AltaSoft.DomainPrimitives.Generator v1.0.0 // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ #nullable enable using DomainPrimitives; using System; using System.ComponentModel; using System.Globalization; using AltaSoft.DomainPrimitives.Abstractions; namespace DomainPrimitives.Converters; /// <summary> /// TypeConverter for <see cref = "YearDate"/> /// </summary> public sealed class YearDateTypeConverter : Int32Converter { /// <inheritdoc/> public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) { var result = base.ConvertFrom(context, culture, value); if (result is null) return null; try { return new YearDate((int)result); } catch (InvalidDomainValueException ex) { throw new FormatException("Cannot parse YearDate", ex); } } }
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/DomainPrimitives
Leave a Reply