Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>CSM_Foundation_Core.UnitTests</RootNamespace>
<RootNamespace>Unit_Tests</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
Expand Down
5 changes: 3 additions & 2 deletions CSM Foundation Core.UnitTests/Utils/SystemUtils_UnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CSM_Foundation_Core.Utils;
using CSM_Foundation_Core;
using CSM_Foundation_Core.Core.Utils;

namespace CSM_Foundation_Core.UnitTests.Utils;
namespace Unit_Tests.Utils;


/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions CSM Foundation Core.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36310.24
# Visual Studio Version 18
VisualStudioVersion = 18.0.11222.15 d18.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSM Foundation Core", "CSM Foundation Core\CSM Foundation Core.csproj", "{E39161C8-353E-40B7-B1E0-ED3E17DCC51F}"
EndProject
Expand All @@ -11,7 +11,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.gitignore = .gitignore
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSM Foundation Core.UnitTests", "CSM Foundation Core.UnitTests\CSM Foundation Core.UnitTests.csproj", "{A64FDEEE-0D9E-4311-A8F1-92F389F0285D}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unit Tests", "CSM Foundation Core.UnitTests\Unit Tests.csproj", "{A64FDEEE-0D9E-4311-A8F1-92F389F0285D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace CSM_Foundation_Core;
namespace CSM_Foundation_Core.Abstractions.Bases;

/// <summary>
/// Represents an inheritance link between datasource objects
/// that need specific equality comparisson between their properties.
/// </summary>
public abstract class BObject<TObject> {
public abstract class ObjectBase<TObject> {

public override bool Equals(object? comparer) {
PropertyInfo[] exceptions = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CSM_Foundation_Core;
namespace CSM_Foundation_Core.Abstractions.Interfaces;

/// <summary>
/// Represents a disposition manager wich stores objects in their context to be disposed at the end of a process.
Expand Down
15 changes: 15 additions & 0 deletions CSM Foundation Core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# CSM Foundation Core CHANGELOG

## [2.0.0] - 24.11-2025

### Changed

- Renamed and reorganized the project structure to CSM Foundation Core to better reflect its purpose as a foundational library for CSM systems.
- Changed concept from [Exception] to [Error].

#### Dependencies

| Package | Previous Version | New Version |
|:----------------------------------------|:----------------:|:---------------:|
| | | |

### Removed

## [1.3.0] - 27.08-2025

### Added
Expand Down
2 changes: 1 addition & 1 deletion CSM Foundation Core/CSM Foundation Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>CSM_Foundation_Core</RootNamespace>
<PackageId>CSM.Foundation.Core</PackageId>
<Version>1.3.0</Version>
<Version>2.0.0</Version>
<Authors>Renato Urrea</Authors>
<Company>Cosmos (CSM)</Company>
<RepositoryUrl>https://github.com/Cosmos-CSM/csm_foundation_core</RepositoryUrl>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,113 +1,96 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace CSM_Foundation_Core.Convertion;
using CSM_Foundation_Core.Convertion.Abstractions.Interfaces;
using CSM_Foundation_Core.Core.Exceptions;

/// <summary>
/// [Abstraction] for serialization/deseralization custom converters.
/// </summary>
namespace CSM_Foundation_Core.Convertion.Abstractions.Bases;

/// <inheritdoc cref="IConverter{TBase}"/>
/// <typeparam name="TBase">
/// Type of the common inherited interface/class from the variants.
/// </typeparam>
public abstract class BConverter<TBase>
public abstract class ConverterBase<TBase>
: JsonConverter<TBase>, IConverter<TBase>
where TBase : IConverterVariation {
where TBase : IConverterVariant {

/// <summary>
/// Stores all the possible variations classes types to find the correct one.
/// </summary>
public Type[] Variations { get; init; }
public Type[] Variants { get; init; }

/// <summary>
/// Creates a new <see cref="BConverter{TBase}"/> instance.
/// Creates a new instance.
/// </summary>
public BConverter(Type[] variations) {
Variations = variations;
/// <param name="variants">
/// Converter <typeparamref name="TBase"/> known variants.
/// </param>
public ConverterBase(Type[] variants) {
Variants = variants;

ValidateVariations();
}

#region Private Methods

/// <summary>
/// Validates if the configured <see cref="Variations"/> correctly inherit from the <typeparamref name="TBase"/>.
/// Validates if the configured <see cref="Variants"/> correctly inherit from the <typeparamref name="TBase"/>.
/// </summary>
/// <exception cref="XBConverter">
/// <exception cref="ConverterBaseError">
/// Thrown when a wrong variation is found.
/// </exception>
void ValidateVariations() {
if (Variations.Length == 0) return;
if (Variants.Length == 0) return;

IEnumerable<Type> wrongTypes = Variations.Where(
IEnumerable<Type> wrongTypes = Variants.Where(
(variation) => {
return !variation.IsAssignableTo(typeof(TBase));
}
);

if (wrongTypes.Any()) {
throw new XBConverter(
XBConverterEvents.WRONG_VARIATIONS,
throw new ConverterBaseError(
ConverterBaseErrorEvents.WRONG_VARIANTS,
[.. wrongTypes]
);
}
}

#endregion

#region JSON Converter Methods

/// <summary>
///
/// </summary>
/// <param name="reader"></param>
/// <param name="typeToConvert"></param>
/// <param name="options"></param>
/// <returns></returns>
/// <exception cref="XBConverter"></exception>
public override TBase? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
JsonDocument document = JsonDocument.ParseValue(ref reader);
JsonElement element = document.RootElement;

// Determine the type from the Discriminator property.
string? discriminator;
string discriminatorToken = nameof(IConverterVariation.Discriminator);
string discriminatorToken = nameof(IConverterVariant.Discriminator);
try {
discriminator = element.GetProperty(discriminatorToken).GetString();
} catch {
discriminator = element.GetProperty(discriminatorToken.ToLower()).GetString();
}

if (string.IsNullOrWhiteSpace(discriminator)) {
throw new XBConverter(XBConverterEvents.NO_DISCRIMINATOR);
throw new ConverterBaseError(ConverterBaseErrorEvents.NO_DISCRIMINATOR);
}

foreach (Type variation in Variations) {
foreach (Type variation in Variants) {
if (discriminator == variation.GetType().Name) {
return (TBase?)element.Deserialize(variation.GetType(), options);
}
}

throw new XBConverter(
XBConverterEvents.NO_VARIATION,
throw new ConverterBaseError(
ConverterBaseErrorEvents.NO_VARIATION,
discriminator: discriminator
);
}

/// <summary>
///
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="options"></param>
public override void Write(Utf8JsonWriter writer, TBase value, JsonSerializerOptions options) {
foreach (Type variation in Variations) {
foreach (Type variation in Variants) {

if (value.GetType().GUID == variation.GUID) {
JsonSerializer.Serialize(writer, variation, options);
}
}

throw new XBConverter(XBConverterEvents.NO_VARIATION);
throw new ConverterBaseError(ConverterBaseErrorEvents.NO_VARIATION);
}

#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using CSM_Foundation_Core.Convertion.Abstractions.Interfaces;

namespace CSM_Foundation_Core.Convertion.Abstractions.Bases;

/// <inheritdoc cref="IConverterVariant"/>
public abstract class ConverterVariantBase
: IConverterVariant {

public string Discriminator { get; init; }

/// <summary>
/// Creates a new instance.
/// </summary>
public ConverterVariantBase() {
Discriminator = $"{GetType().GUID}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace CSM_Foundation_Core.Convertion.Abstractions.Interfaces;

/// <summary>
/// Represents a JSON converter.
/// </summary>
/// <typeparam name="TBase">
/// Type of the common inherited interface/class from the variants.
/// </typeparam>
public interface IConverter<TBase> {

/// <summary>
/// Converter handled derived types from <typeparamref name="TBase"/>.
/// </summary>
public Type[] Variants { get; }
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;

namespace CSM_Foundation_Core.Convertion;
namespace CSM_Foundation_Core.Convertion.Abstractions.Interfaces;

/// <summary>
/// Interface to determine a variation to the <see cref="BConverter{T}"/> implementation.
/// Represents a <see cref="IConverter{TBase}"/> handled variant.
/// </summary>
public interface IConverterVariation {
public interface IConverterVariant {

/// <summary>
/// Unique operation time variation identification for transaction convertions.
/// Unique runtime variation identification for transaction convertions.
///
/// <para>
/// Additional has <see cref="JsonPropertyOrderAttribute"/> set as 0 to always be the first
/// property to be converted.
/// </para>
/// </summary>
[JsonPropertyOrder(0), NotMapped]
string Discriminator { get; init; }
string Discriminator { get; }
}
23 changes: 0 additions & 23 deletions CSM Foundation Core/Convertion/BConverterVariation.cs

This file was deleted.

17 changes: 0 additions & 17 deletions CSM Foundation Core/Convertion/IConverter.cs

This file was deleted.

51 changes: 0 additions & 51 deletions CSM Foundation Core/Convertion/XBConverter.cs

This file was deleted.

Loading
Loading