Codescovery.Library.Commons 9.0.0

dotnet add package Codescovery.Library.Commons --version 9.0.0                
NuGet\Install-Package Codescovery.Library.Commons -Version 9.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Codescovery.Library.Commons" Version="9.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Codescovery.Library.Commons --version 9.0.0                
#r "nuget: Codescovery.Library.Commons, 9.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Codescovery.Library.Commons as a Cake Addin
#addin nuget:?package=Codescovery.Library.Commons&version=9.0.0

// Install Codescovery.Library.Commons as a Cake Tool
#tool nuget:?package=Codescovery.Library.Commons&version=9.0.0                

library-commons

.Net 9 package that provides some common:

Extensions

Deep Clone Extension

The deep clone extension uses Activator.CreateInstace to clone the entity.

This approach considered some studies made by cinorid in this answer.

Benchmark
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.18363.1734 (1909/November2019Update/19H2)
Intel Core i5-6200U CPU 2.30GHz (Skylake), 1 CPU, 4 logical and 2 physical cores
[Host]     : .NET Framework 4.8 (4.8.4400.0), X86 LegacyJIT
DefaultJob : .NET Framework 4.8 (4.8.4400.0), X86 LegacyJIT
Method Mean Error StdDev Gen 0 Allocated
BinarySerializer 220.69 us 4.374 us 9.963 us 49.8047 77 KB
XmlSerializer 182.72 us 3.619 us 9.405 us 21.9727 34 KB
Activator.CreateInstance 49.99 us 0.992 us 2.861 us 1.9531 3 KB

Object Extensions

Provides six Methods:

IsNullOrDefault

bool IsNullOrDefault(this object obj)

Returns whether object has a null or a default value

As

T As<T>(this object sourceObject, T defaultValue = default, bool throwExceptionOnError = false)

Cast an object to a Generic Type.

It throws an CastingException case throwExceptionOnError argument is true, else it will return a default value;

And

IFluentPropertySelectorService<T> And<T>(this T obj)

Returns an IFluentPropertySelectorService used in Fluent property Setter(#Fluent property Setter).

With

 IFluentPropertySelectorService<T> With<T>(this T obj)

Returns an IFluentPropertySelectorService used in Fluent property Setter(#Fluent property Setter).

FillWith

void FillWith<T>(this T obj, T source) where T : class

Fill obj with the values from source object using [DeepCloneExtensions](#Deep Clone Extension)

DeepCloneWith

IFluentPropertySelectorService<T> DeepCloneWith<T>(this T obj) where T : new()

Creates a new cloned instance of generic type T using [DeepCloneExtensions](#Deep Clone Extension)

Base64

There's an extension that will handle base64 encode and decode from a raw string

Usage

Import extensions

using Codescovery.Library.Commons.Extensions;

public void Encode()
{
    var encodedString = rawString.ToBase64Encoded();
}
public void Decode()
{
    var decodedString = rawString.ToBase64Decoded();
}
public void EncodeNull()
{
    string? nullRawString = null;
    var encodedString = nullRawString.ToBase64Encoded();
}
public void DecodeNull()
{
    string? nullEncodedString = null;
    var decodedString = nullEncodedString.ToBase64Decoded();
}

Enum Extension

The enum extensions provide two methods:

  • ToNullableInt
  • ToInt

Usage

Import extensions

using Codescovery.Library.Commons.Extensions;

For this example, we are going to use the enum bellow

ExampleEnum example

public enum ExampleEnum
{
    Value1 = 1,
    Value2 = 5,
}
var exampleEnum = ExampleEnum.Value1;
var exampleEnumCastedInt = exampleEnum.ToInt();
var exampleEnumCastedNullableInt = exampleEnum.ToNullableInt();

Fluent property Setter

We are providing a fluent property setter, thanks to Adem Catamak answer.

Which allow us to modify and clone an object using like fluent api.

Usage

Firts you need to import the extensions:

using Codescovery.Library.Commons.Extensions;

For this example, we are going to use the class bellow

ClonableExampleClass example
    public class ClonableExampleClass
    {


        public string? ExampleString { get; set; } = default;
        public int ExampleInt { get; set; } = default;
        public ExampleEnum ExampleEnum { get; set; } = default;
        public ClonableExampleClass? ExampleNestedClass { get; set; }
        public List<ClonableExampleClass>? ExampleList { get; set; }
    }

How to use:

Modify the values of the current class using fluent api

var exampleClass = new ClonableExampleClass
{
ExampleInt = 1
}
.With()
.Property(@class => @class.ExampleInt)
.Set(intValue;
Deep clone class and modify using fluent api
 var exampleClass = new ClonableExampleClass
        {
            ExampleInt = 1
        };
var cloned= exampleClass.DeepCloneWith()
.Property(@class => @class.ExampleInt)
.Set(1234)
.And()
.Property(@class => "Example string")
.Set(stringValue);

ExceptionsExtensions

Create a full formated exception message.

Usage

Firts you need to import the extensions:

using Codescovery.Library.Commons.Extensions;

Or you can inherint our BaseException class that will handle it for you.

using Codescovery.Library.Commons.Abstractions;

ListExtensions

Converts any objects to a list of type

ValueIn

Checks if a string value is in a csharp IEnumerable<string> of string

Helpers

ConfiguratioBuilder Helper

Provides two Methods:

GetCurrentDirectoryBasePath

FolderPath GetCurrentDirectoryBasePath(FolderPath basePath =null, params string[] paths)

Based on a nullable folder path it will return the application current directory base path concatenated with aditional path parameters passed.

GetBasePath

 FolderPath GetBasePath(FolderPath basePath = null)

Will return the basePath if <> null, else will return the DefaultBasePath

Constants

public const string DefaultConfigurationFileName = "appsettings.json";
public const string DefaultConfigurationFileExtension = ".json";
public const string DefaultConfigurationFileExtensionSeparator = ".";
public const string DefaultConfigurationsProjectSubFolderName = "Configurations";
public static readonly FolderPath DefaultBasePath = Directory.GetCurrentDirectory();

ExceptionMessageHelper

Helper to override Exception default messages:

Provides 3 Methods

OverrideAditionalInfoHeaderText

void OverrideAditionalInfoHeaderText(this string aditionalInfoHeaderText)

Will Override the AditionalInfoHeaderText text

Default Value : Aditional Info:

OverrideStackTraceHeaderText

void OverrideStackTraceHeaderText(this string stackTraceHeaderText)

Will Override the StackTraceHeaderText text

Default Value: Stack Trace:

OverrideMessageHeaderText

 void OverrideMessageHeaderText(this string messageHeaderText)

Will Override the MessageHeaderText text

Default Value : Message:

Constants

    public const string DefaultErrorMesage = "Error: Application reports that an error occurred:";
    public const string DefaultMessageHeaderText = "Message:";
    public const string DefaultAditionalInfoHeaderText = "Aditional Info:";
    public const string DefaultStackTraceHeaderText = "Stack Trace:";
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Codescovery.Library.Commons:

Package Downloads
Codescovery.Library.DependencyInjection

A pack with some common dependency injection methods

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
9.0.0 102 11/22/2024
1.0.0-beta8 276 9/26/2023
1.0.0-beta7 112 9/20/2023
1.0.0-beta6 99 9/19/2023
1.0.0-beta5 151 5/4/2023
1.0.0-beta4 98 5/4/2023
1.0.0-beta3 234 4/9/2023
1.0.0-beta2 122 3/17/2023
1.0.0-beta1 115 2/28/2023