diff --git a/CSM Foundation Core.sln b/CSM Foundation Core.sln index c9b59dd..01bb2e7 100644 --- a/CSM Foundation Core.sln +++ b/CSM Foundation Core.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 18 -VisualStudioVersion = 18.0.11222.15 d18.0 +VisualStudioVersion = 18.0.11222.15 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 diff --git a/CSM Foundation Core/Abstractions/Bases/ObjectBase.cs b/CSM Foundation Core/Abstractions/Bases/ObjectBase.cs index 8bd02c9..2635ed1 100644 --- a/CSM Foundation Core/Abstractions/Bases/ObjectBase.cs +++ b/CSM Foundation Core/Abstractions/Bases/ObjectBase.cs @@ -8,8 +8,9 @@ namespace CSM_Foundation_Core.Abstractions.Bases; /// Represents an inheritance link between datasource objects /// that need specific equality comparisson between their properties. /// -public abstract class ObjectBase { +public class ObjectBase { + /// public override bool Equals(object? comparer) { PropertyInfo[] exceptions = []; @@ -77,7 +78,7 @@ public override bool Equals(object? comparer) { return true; } - + /// public override string ToString() { Dictionary jsonReference = []; PropertyInfo[] propReferences = GetType().GetProperties(); @@ -93,6 +94,8 @@ public override string ToString() { } ); } + + /// public override int GetHashCode() { return base.GetHashCode(); } @@ -106,7 +109,7 @@ public override int GetHashCode() { /// /// Property reflected info. /// - /// + /// /// If the Property wasn't find. /// public PropertyInfo GetProperty(string name) { @@ -117,18 +120,4 @@ PropertyInfo tracedProperty return tracedProperty; } - - public TObject Clone() { - Type ObjectType = typeof(TObject); - - TObject? Cloned = (TObject?)Activator.CreateInstance(ObjectType) - ?? throw new Exception("PENDING WRONG ACTIVATION"); - PropertyInfo[] ObjectPropertiesInfo = ObjectType.GetProperties(); - foreach (PropertyInfo PropertyInfo in ObjectPropertiesInfo) { - object? OriginalValue = PropertyInfo.GetValue(this); - PropertyInfo.SetValue(Cloned, OriginalValue); - } - - return Cloned.GetHashCode() == GetHashCode() ? throw new Exception("PEDNING SAME HASH") : Cloned; - } } diff --git a/CSM Foundation Core/CHANGELOG.md b/CSM Foundation Core/CHANGELOG.md index 421d6d8..26e3ad9 100644 --- a/CSM Foundation Core/CHANGELOG.md +++ b/CSM Foundation Core/CHANGELOG.md @@ -1,5 +1,49 @@ # CSM Foundation Core CHANGELOG + +## [x.x.x] - xx.xx-xxxx + +### Added + +- Added [ResourceType] enum to handle the resource types in CSM systems. +- Added [ScalarValue] enum to handle the scalar value types in CSM systems. + +#### Dependencies + +| Package | Previous Version | New Version | +|:----------------------------------------|:----------------:|:---------------:| +| | | | + +## [4.0.0] - 19.05-2026 + +### Removed + +- Removed [GenericUtils] class cause i only had a method for object clonation, replaced and moved to [ObjectUtils] with the method [Clone]. + +- Added new Error concepts and better error handling for some features that weren´t being handled correclty. + +- Added a simplified [ObjectBase] abstraction for non used generic methods. + +- Removed [ObjectBase{TEntity}] cause an utility method was created. + +#### Dependencies + +| Package | Previous Version | New Version | +|:----------------------------------------|:----------------:|:---------------:| +| | | | + +## [3.0.0] - 25.12-2025 + +### Remved + +- Removed from errors model [ErrorInfo] since it was only needed when internal errors are exposed to connected services with critical information. + +#### Dependencies + +| Package | Previous Version | New Version | +|:----------------------------------------|:----------------:|:---------------:| +| | | | + ## [2.1.1] - 24.12-2025 ### Changed diff --git a/CSM Foundation Core/CSM Foundation Core.csproj b/CSM Foundation Core/CSM Foundation Core.csproj index 695fd6f..7cb5de5 100644 --- a/CSM Foundation Core/CSM Foundation Core.csproj +++ b/CSM Foundation Core/CSM Foundation Core.csproj @@ -4,7 +4,7 @@ net8.0 CSM_Foundation_Core CSM.Foundation.Core - 2.1.1 + 4.0.0 Renato Urrea Cosmos (CSM) https://github.com/Cosmos-CSM/csm_foundation_core @@ -12,6 +12,7 @@ enable true CHANGELOG.md + true diff --git a/CSM Foundation Core/Convertion/Abstractions/Bases/ConverterBase.cs b/CSM Foundation Core/Convertion/Abstractions/Bases/ConverterBase.cs index 55ecdbf..98a7ead 100644 --- a/CSM Foundation Core/Convertion/Abstractions/Bases/ConverterBase.cs +++ b/CSM Foundation Core/Convertion/Abstractions/Bases/ConverterBase.cs @@ -2,7 +2,7 @@ using System.Text.Json.Serialization; using CSM_Foundation_Core.Convertion.Abstractions.Interfaces; -using CSM_Foundation_Core.Core.Exceptions; +using CSM_Foundation_Core.Core.Errors; namespace CSM_Foundation_Core.Convertion.Abstractions.Bases; @@ -54,6 +54,7 @@ [.. wrongTypes] } } + /// public override TBase? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { JsonDocument document = JsonDocument.ParseValue(ref reader); JsonElement element = document.RootElement; @@ -83,6 +84,7 @@ [.. wrongTypes] ); } + /// public override void Write(Utf8JsonWriter writer, TBase value, JsonSerializerOptions options) { foreach (Type variation in Variants) { diff --git a/CSM Foundation Core/Convertion/Abstractions/Bases/ConverterVariantBase.cs b/CSM Foundation Core/Convertion/Abstractions/Bases/ConverterVariantBase.cs index e74ae19..5c20db0 100644 --- a/CSM Foundation Core/Convertion/Abstractions/Bases/ConverterVariantBase.cs +++ b/CSM Foundation Core/Convertion/Abstractions/Bases/ConverterVariantBase.cs @@ -6,6 +6,7 @@ namespace CSM_Foundation_Core.Convertion.Abstractions.Bases; public abstract class ConverterVariantBase : IConverterVariant { + /// public string Discriminator { get; init; } /// diff --git a/CSM Foundation Core/Core/Exceptions/ConverterBaseError.cs b/CSM Foundation Core/Core/Errors/ConverterBaseError.cs similarity index 80% rename from CSM Foundation Core/Core/Exceptions/ConverterBaseError.cs rename to CSM Foundation Core/Core/Errors/ConverterBaseError.cs index 6991d1e..747db29 100644 --- a/CSM Foundation Core/Core/Exceptions/ConverterBaseError.cs +++ b/CSM Foundation Core/Core/Errors/ConverterBaseError.cs @@ -2,7 +2,7 @@ using CSM_Foundation_Core.Convertion.Abstractions.Interfaces; using CSM_Foundation_Core.Errors.Abstractions.Bases; -namespace CSM_Foundation_Core.Core.Exceptions; +namespace CSM_Foundation_Core.Core.Errors; /// /// Exception situations enumerator. @@ -34,15 +34,17 @@ public class ConverterBaseError /// /// Creates a new instance. /// - /// - /// Exception situation. + /// + /// Error event. + /// + /// + /// Wrong variations found. /// - /// /// /// Discriminator when the variation convertion fails. /// - public ConverterBaseError(ConverterBaseErrorEvents situation, Type[]? wrongVariations = null, string discriminator = "") - : base("CSM Converter Exception", situation) { + public ConverterBaseError(ConverterBaseErrorEvents @event, Type[]? wrongVariations = null, string discriminator = "") + : base("CSM Converter Exception", @event) { wrongVariations ??= []; Data = new Dictionary { diff --git a/CSM Foundation Core/Core/Exceptions/SystemError.cs b/CSM Foundation Core/Core/Errors/SystemError.cs similarity index 96% rename from CSM Foundation Core/Core/Exceptions/SystemError.cs rename to CSM Foundation Core/Core/Errors/SystemError.cs index 208b925..87ef5d4 100644 --- a/CSM Foundation Core/Core/Exceptions/SystemError.cs +++ b/CSM Foundation Core/Core/Errors/SystemError.cs @@ -1,7 +1,7 @@ using CSM_Foundation_Core.Errors.Abstractions.Bases; using CSM_Foundation_Core.Errors.Models; -namespace CSM_Foundation_Core.Core.Exceptions; +namespace CSM_Foundation_Core.Core.Errors; /// diff --git a/CSM Foundation Core/Core/Extensions/DateTimeExtensions.cs b/CSM Foundation Core/Core/Extensions/DateTimeExtensions.cs index 51c05de..99d72f1 100644 --- a/CSM Foundation Core/Core/Extensions/DateTimeExtensions.cs +++ b/CSM Foundation Core/Core/Extensions/DateTimeExtensions.cs @@ -1,8 +1,22 @@ namespace CSM_Foundation_Core.Core.Extensions; + + +/// +/// Extends class. +/// public static class DateTimeExtensions { - public static DateTime Trim(this DateTime Current) { - return new DateTime(Current.Year, Current.Month, Current.Day, Current.Hour, Current.Minute, Current.Second); + /// + /// Trims the given to seconds. + /// + /// + /// Current object. + /// + /// + /// Trimmed to seconds. + /// + public static DateTime Trim(this DateTime obj) { + return new DateTime(obj.Year, obj.Month, obj.Day, obj.Hour, obj.Minute, obj.Second); } } diff --git a/CSM Foundation Core/Core/Extensions/EnumerableExtension.cs b/CSM Foundation Core/Core/Extensions/EnumerableExtension.cs index 8f7889a..224dc4a 100644 --- a/CSM Foundation Core/Core/Extensions/EnumerableExtension.cs +++ b/CSM Foundation Core/Core/Extensions/EnumerableExtension.cs @@ -1,6 +1,24 @@ namespace CSM_Foundation_Core.Core.Extensions; + + +/// +/// Extends class. +/// public static class EnumerableExtension { - public static bool Empty(this IEnumerable List) { - return !List.Any(); + + /// + /// Checks if the is empty. + /// + /// + /// Type of the content. + /// + /// + /// Current object. + /// + /// + /// Whether the collection is empty or not. + /// + public static bool Empty(this IEnumerable obj) { + return !obj.Any(); } } diff --git a/CSM Foundation Core/Core/Resources/ResourceType.cs b/CSM Foundation Core/Core/Resources/ResourceType.cs new file mode 100644 index 0000000..2eaac44 --- /dev/null +++ b/CSM Foundation Core/Core/Resources/ResourceType.cs @@ -0,0 +1,16 @@ +namespace CSM_Foundation_Core.Core.Resources; + +/// +/// Resource types enumeration. +/// +public enum ResourceType { + /// + /// When the resource has a remote access Link. + /// + HLINK, + + /// + /// When the resource has local or network access data. + /// + FILE, +} diff --git a/CSM Foundation Core/Core/Resources/ScalarValue.cs b/CSM Foundation Core/Core/Resources/ScalarValue.cs new file mode 100644 index 0000000..7f8feb1 --- /dev/null +++ b/CSM Foundation Core/Core/Resources/ScalarValue.cs @@ -0,0 +1,31 @@ +namespace CSM_Foundation_Core.Core.Resources; + +/// +/// Scalar values enumerator. +/// +public enum ScalarValue { + /// + /// Double precision floating point number. + /// + DOUBLE, + + /// + /// Integer number. + /// + INT, + + /// + /// String value. + /// + STRING, + + /// + /// Boolean value. + /// + BOOL, + + /// + /// Enumeration value. + /// + ENUM, +} diff --git a/CSM Foundation Core/Core/Utils/FileUtils.cs b/CSM Foundation Core/Core/Utils/FileUtils.cs index 8c454eb..d1b91e5 100644 --- a/CSM Foundation Core/Core/Utils/FileUtils.cs +++ b/CSM Foundation Core/Core/Utils/FileUtils.cs @@ -1,21 +1,52 @@ using System.Text.Json; namespace CSM_Foundation_Core.Core.Utils; + + +/// +/// Provide system files utility methods. +/// public class FileUtils { - public static string FormatLocation(string Location) { + + /// + /// Formats the given for system sensitive '/' or '\\' usage. + /// + /// + /// System file path. + /// + /// + /// Formatted system file path. + /// + public static string FormatPath(string path) { string wd = Directory.GetCurrentDirectory(); if (wd.Contains('/')) { - Location = Location.Replace("\\", "/"); + path = path.Replace("\\", "/"); } - return Location; + return path; } - public static TModel Deserealize(string Location) { - if (!File.Exists(Location)) { - throw new FileNotFoundException(Location); + + /// + /// Gets the given file content as an instance. + /// + /// + /// Type of the converted data model. + /// + /// + /// File system path. + /// + /// + /// Converted file content into a instance. + /// + /// + /// Thrown when the given does not exist. + /// + public static TModel Deserealize(string path) { + if (!File.Exists(path)) { + throw new FileNotFoundException(path); } - Stream fs = File.OpenRead(Location); + Stream fs = File.OpenRead(path); TModel m = JsonSerializer.Deserialize(fs)!; return m; } diff --git a/CSM Foundation Core/Core/Utils/GenericUtils.cs b/CSM Foundation Core/Core/Utils/GenericUtils.cs deleted file mode 100644 index c1141ee..0000000 --- a/CSM Foundation Core/Core/Utils/GenericUtils.cs +++ /dev/null @@ -1,62 +0,0 @@ - -using System.Collections; -using System.Reflection; - -namespace CSM_Foundation_Core.Core.Utils; -public static class GenericUtils { - public static TObject DeepCopy(this TObject target, bool isParentProperty = true, Dictionary? visited = null) { - visited ??= []; - - if (target == null) - return default!; - - Type ObjectType = isParentProperty ? typeof(TObject) : target.GetType(); - - if (visited.TryGetValue(target, out var existingClone)) - return (TObject)existingClone; - - TObject Cloned = (TObject?)Activator.CreateInstance(ObjectType) - ?? throw new Exception("PENDING WRONG ACTIVATION"); - - visited[target] = Cloned; - - PropertyInfo[] ObjectPropertiesInfo = ObjectType.GetProperties(); - foreach (PropertyInfo PropertyInfo in ObjectPropertiesInfo) { - if (!PropertyInfo.CanRead || !PropertyInfo.CanWrite || PropertyInfo.GetIndexParameters().Length > 0) - continue; - - object? OriginalValue = PropertyInfo.GetValue(target); - if (OriginalValue == null) { - PropertyInfo.SetValue(Cloned, null); - } else if (PropertyInfo.PropertyType.IsValueType || PropertyInfo.PropertyType == typeof(string)) { - PropertyInfo.SetValue(Cloned, OriginalValue); - } else if (typeof(IEnumerable).IsAssignableFrom(PropertyInfo.PropertyType)) { - var clonedCollection = CloneCollection((IEnumerable)OriginalValue, PropertyInfo.PropertyType, visited); - PropertyInfo.SetValue(Cloned, clonedCollection); - } else { - var clonedValue = OriginalValue.DeepCopy(false, visited); - PropertyInfo.SetValue(Cloned, clonedValue); - } - } - - return Cloned.GetHashCode() == target!.GetHashCode() - ? throw new Exception("Clonation Error:\n SAME HASH ON:" + target.GetType()) - : Cloned; - } - - private static object CloneCollection(IEnumerable originalCollection, Type collectionType, Dictionary visited) { - Type elementType = collectionType.IsGenericType - ? collectionType.GetGenericArguments()[0] - : typeof(object); - - var clonedCollection = (IList?)Activator.CreateInstance(typeof(List<>).MakeGenericType(elementType)); - if (clonedCollection == null) return originalCollection; - - foreach (var item in originalCollection) { - var clonedItem = item.DeepCopy(false, visited); - clonedCollection.Add(clonedItem); - } - - return clonedCollection; - } -} diff --git a/CSM Foundation Core/Core/Utils/ObjectUtils.cs b/CSM Foundation Core/Core/Utils/ObjectUtils.cs new file mode 100644 index 0000000..5c75905 --- /dev/null +++ b/CSM Foundation Core/Core/Utils/ObjectUtils.cs @@ -0,0 +1,36 @@ +using System.Reflection; + +namespace CSM_Foundation_Core.Core.Utils; + + +/// +/// Provides utlity methods for objects handling. +/// +public static class ObjectUtils { + + /// + /// Clones the current object into a new instance with the same property values. + /// + /// + /// Object reference to be cloned. + /// + /// + /// Cloned instance. + /// + public static TObject Clone(TObject obj) { + if(obj is null) + return obj; + + Type objType = typeof(TObject); + + TObject? Cloned = (TObject?)Activator.CreateInstance(objType) + ?? throw new Exception("PENDING WRONG ACTIVATION"); + PropertyInfo[] ObjectPropertiesInfo = objType.GetProperties(); + foreach (PropertyInfo PropertyInfo in ObjectPropertiesInfo) { + object? OriginalValue = PropertyInfo.GetValue(obj); + PropertyInfo.SetValue(Cloned, OriginalValue); + } + + return Cloned.GetHashCode() == obj?.GetHashCode() ? throw new Exception("Same hash detected along clonation, fatal error.") : Cloned; + } +} diff --git a/CSM Foundation Core/Core/Utils/RandomUtils.cs b/CSM Foundation Core/Core/Utils/RandomUtils.cs index 5f7279e..27fda8c 100644 --- a/CSM Foundation Core/Core/Utils/RandomUtils.cs +++ b/CSM Foundation Core/Core/Utils/RandomUtils.cs @@ -1,15 +1,27 @@ namespace CSM_Foundation_Core.Core.Utils; + +/// +/// Provide randomization utility methods. +/// public static class RandomUtils { private static readonly Random Randomizer = new(); - public static string String(int Size) { - char[] letters = new char[Size]; - for (int Pointing = 0; Pointing < Size; Pointing++) { - int Code = Randomizer.Next(65, 91); - letters[Pointing] = (char)Code; + /// + /// Gets a randomized string. + /// + /// + /// String size + /// + /// + /// A randomized string. + /// + public static string String(int size) { + char[] letters = new char[size]; + for (int i = 0; i < size; i++) { + int code = Randomizer.Next(65, 91); + letters[i] = (char)code; } - string GeneratedString = new(letters); - return GeneratedString; + return new string(letters); } } diff --git a/CSM Foundation Core/Errors/Abstractions/Bases/ErrorBase.cs b/CSM Foundation Core/Errors/Abstractions/Bases/ErrorBase.cs index 736b8c8..26784c5 100644 --- a/CSM Foundation Core/Errors/Abstractions/Bases/ErrorBase.cs +++ b/CSM Foundation Core/Errors/Abstractions/Bases/ErrorBase.cs @@ -13,21 +13,29 @@ public abstract class ErrorBase : Exception, IException where TEvent : Enum { + /// public string Advise { get; init; } + /// public TEvent Event { get; init; } + /// + public object EventCode { get => Convert.ToInt32(Event); } + + /// public Exception? Exception { get; init; } + /// public ErrorFeedback[] Feedback { get; init; } + /// public new IDictionary Data { get; init; } /// /// Creates a new instance. /// - /// - /// Exception subject. + /// + /// Error message. /// /// /// Exception trigger event. diff --git a/CSM Foundation Core/Errors/Abstractions/Interfaces/IError.cs b/CSM Foundation Core/Errors/Abstractions/Interfaces/IError.cs index 6ea3196..92e3b09 100644 --- a/CSM Foundation Core/Errors/Abstractions/Interfaces/IError.cs +++ b/CSM Foundation Core/Errors/Abstractions/Interfaces/IError.cs @@ -14,6 +14,11 @@ public interface IError { /// string Message { get; } + /// + /// Generic event object value. + /// + public object EventCode { get; } + /// /// Exception stacking trace. /// @@ -51,8 +56,6 @@ public interface IException : IError where TEvent : Enum { - /// - /// Exception trigger event. - /// + /// TEvent Event { get; } } \ No newline at end of file diff --git a/CSM Foundation Core/Errors/Models/ErrorInfo.cs b/CSM Foundation Core/Errors/Models/ErrorInfo.cs deleted file mode 100644 index a0bd8e8..0000000 --- a/CSM Foundation Core/Errors/Models/ErrorInfo.cs +++ /dev/null @@ -1,35 +0,0 @@ -namespace CSM_Foundation_Core.Errors.Models; - -/// -/// {model} record for . -/// -/// -/// Stores public information to expose an Exception details. -/// -/// -public record ErrorInfo { - /// - /// Where the exception got thrown. - /// - public required string Trace { get; init; } - - /// - /// Specific exception management situation code. - /// - public required int Situation { get; init; } - - /// - /// User friendly displayable error message. - /// - public required string Advise { get; init; } - - /// - /// Exception message caught at system level. - /// - public required string System { get; init; } - - /// - /// Additional detailed information about the exception. - /// - public Dictionary Factors { get; init; } = []; -}