Maestria.Extensions
3.4.1
See the version list below for details.
dotnet add package Maestria.Extensions --version 3.4.1
NuGet\Install-Package Maestria.Extensions -Version 3.4.1
<PackageReference Include="Maestria.Extensions" Version="3.4.1" />
paket add Maestria.Extensions --version 3.4.1
#r "nuget: Maestria.Extensions, 3.4.1"
// Install Maestria.Extensions as a Cake Addin #addin nuget:?package=Maestria.Extensions&version=3.4.1 // Install Maestria.Extensions as a Cake Tool #tool nuget:?package=Maestria.Extensions&version=3.4.1
Maestria.Extensions
What is Maestria.Extensions?
Extension function pack to increase productivity and improve source code writing. By default, all extension methods are safe.
What is Maestria Project?
This library is part of Maestria Project.
Maestria is a project to provide productivity and elegance to your source code writing.
Where can I get it?
First, install NuGet. Then, install Maestria.Extensions from the package manager console:
PM> Install-Package Maestria.Extensions
or install from the dotnet cli command line:
> dotnet add package Maestria.Extensions
How do I get started?
First, import "Maestria.Extensions" reference:
using Maestria.Extensions;
Then in your application code, use fluent syntax
// AggregateExtenions
<Array>.Max();
<Array>.Min();
// Base64Extensions
<byte[]>.ToBase64(<byte[]>)
<string>.ToBase64(<string>, <Encoding>)
var decoded = <byte[]>.FromBase64(<encoded-byte[]>, <Encoding>)
var decoded = <string>.FromBase64(<encoded-string>, <Encoding>)
// CollectionExtensions
<IEnumerable>.IsNullOrEmpty()
<IEnumerable>.HasItems()
<IDictionary>.TryGetValue(<key>, <@default-value>)
<IEnumerable>.Iterate(item => <action>)
await <IEnumerable>.Iterate(async item => await <action>)
<IEnumerable>.Iterate((item, index) => <action>)
await <IEnumerable>.Iterate(async (item, index) => await <action>)
// EnumExtensions
<Enum>.GetDisplayName()
<Enum>.GetDescription()
EnumExtensions.GetValues<TEnum>()
EnumExtensions.GetValues(typeof(<TEnum>))
// ExceptionExtensions
<Exception>.GetAllMessages()
<Exception>.GetMostInner()
// HashExtensions
"value".GetHashMd5()
"value".GetHashSha1()
"value".GetHashSha256()
"value".GetHashSha384()
"value".GetHashSha512()
HashExtensions.ComputeHash(<HashAlgorithm>, "value")
// RoundExtensions
<floating-point>.Round(<digits>)
<floating-point>.Round(<digits>, <MidpointRounding>)
<floating-point>.RoundUp(<digits>)
// TruncateExtensions
<floating-point>.Truncate(<digits>)
// StringExtensions
<string>.TrimStart(<start-comparison>, <ignore-case>)
<string>.TrimEnd(<start-comparison>, <ignore-case>)
<string>.AddToBeginningIfNotStartsWith(<start-comparison>, <ignore-case>)
<string>.AddToBeginningIfHasValue(<start-comparison>, <prefix>)
<string>.AddToEndIfNotEndsWith(<start-comparison>, <ignore-case>)
<string>.AddToEndIfHasValue(<start-comparison>, <prefix>)
<string>.EscapeXml()
<string>.Format(<values-to-format>)
<string>.IsNullOrEmpty()
<string>.IsNullOrWhiteSpace()
<string>.EmptyIf(<string>)
<string>.EmptyIfNull()
<string>.EmptyIfNullOrWhiteSpace()
<string>.HasValue() // Check if text is not null and not white space
<string>.EqualsIgnoreCase(<camparison-value>, <auto-trim>)
<string>.OnlyNumbers()
<string>.RemoveAccents()
<string>.Join(<separator>)
<string>.SubstringBeforeFirstOccurrence("-")
<string>.SubstringBeforeLastOccurrence("-")
<string>.SubstringAfterFirstOccurrence("-")
<string>.SubstringAfterLastOccurrence("-")
<string>.SubstringAtOccurrenceIndex("-", 1)
<string>.SubstringSafe(<start-index>, <length>)
<string>.Truncate(<length>)
<string>.TruncateStart(<length>)
<string>.OnlyLettersOrNumbersOrUnderscoresOrHyphensTest(<string>)
<string>.ToSnakeCase()
// Guid
<Guid>.IsEmpty()
<Guid?>.IsNullOrEmpty()
<string>.IsNullOrWhiteSpace()
<Guid>.IfEmpty(<value>)
<Guid?>.IfNullOrEmpty(<value>)
// SyntaxExtensions
<object>.IsNull()
<object>.HasValue() or <object>.IsNotNull() // Same result
<object>.In(<array-of-values>)
<IComparable>.Between(<starting-value>, <ending-value>)
<IComparable>.LimitMaxAt(<max-value>)
<IComparable>.LimitMinAt(<min-value>)
// Pipelines
var value = <string>
.OnlyNumbers()
.DetachedCall(x => Console.WriteLine(x)) // <<< Call a method with current value and continue execution pipeline
.Format("mask"); // value is only the number of string formatted and only numbers are written on console
<string>
.OnlyNumbers()
.OutVar(out var variableToExternalFromScopeAccess) // <<< Create variable with current value on external scope and continua execution pipeline
.Format("mask"); // value is only the number of string formatted and only numbers are written on console
If fluent expressions
It's possible to execute fluent comparisons expression with the syntax: <value>.IfGreater(<value-to-compare>).Then(<result-if-compare-is-true>)
.
The methods for comparison operations are: IfGreater
, IfGreaterOrEqual
, IfLest
. IfLessOrEqual
, If
and IfNot
.
Rules:
- When condition it's
false
, result the pipeline is<value>
. - When
<value>
or<value-to-compare>
is null:- Result only
true
if both arenull
and comparison is equality operationIf
. - When an only value is
null
, the result istrue
if the operation is not equality comparisonIfNot
. - When
<value>
isNullable<>
,<result-if-compare-is-true>
always ifNullable<>
data type. - But when
<value>
is notNullable<>
,<result-if-compare-is-true>
allow useNullable<>
and notNullable<>
data type. - All other operations comparisons result in
false
.
- Result only
- It's possible return
null
value at<result-if-compare-is-true>
, but then indicated syntax is<value>.NullIf(<value-to-compare>)
.
Examples:
<IComparable>.IfGreater(10).Then(5)
<IComparable>.IfGreaterOrEqual(10).Then(5)
<IComparable>.IfLest(10).Then(5)
<IComparable>.IfLessOrEqual(10).Then(5)
<IComparable>.If(10).Then(5)
<IComparable>.IfNot(10).Then(5)
Delegates expression only executes when the condition it's true.
<IComparable>.IfGreater(10).Then(() => 5)
<IComparable>.IfGreaterOrEqual(10).Then(() => 5)
<IComparable>.IfLest(10).Then(() => 5)
<IComparable>.IfLessOrEqual(10).Then(() => 5)
<IComparable>.If(10).Then(() => 5)
<IComparable>.IfNot(10).Then(() => 5)
Other fluent comparison operations:
// IfNullExtensions
<object>.IfNull(<output-value-when-null>)
<string>.IfNullOrEmpty(<output-value-when-null-or-empty-string>)
<string>.IfNullOrWhiteSpace(<output-value-when-null-or-empty-white-space>)
// NullIfExtensions
<object>.NullIf(<comparison-value>)
<floating-point>.NullIf(<comparison-value>, <tolerance-to-comparasion>)
<string>.NullIf(<comparison-value>, <ignore-case>)
<string>.NullIfEmpty(<comparison-value>)
<string>.NullIfWhiteSpace(<comparison-value>)
<IComparable>.NullIfIn(<comparison-value>)
<IComparable>.NullIfBetween(<comparison-value>)
Data Types
ISimpleResult, SimpleResult and SimpleResult< TValue>
This structure has success and message for simple method result, extensible with generic TValue on "Value" property.
SimpleResult ok = SimpleResult.Ok(<optional-message>);
SimpleResult fail = SimpleResult.Fail("Fail message");
// Implict conversions
SimpleResult ok = true;
SimpleResult fail = "Fail message"
SimpleResult fail = new Exception("Fail message")
// Initializer
var result = new SimpleResult
{
Success = true,
Message = "Successfully processed"
}
To improve then development experience, there are implicit conversions to assign data from:
bool
: To set or verify propertySuccess
.string
: To set fail message andSuccess
tofalse
.Exception
: To set fail message, Exception andSuccess
tofalse
.TValue
: To set value andSuccess
totrue
(Even if null).
And implicit conversions to assingn data to:
bool
:SimpleResult
: Get data from propertySuccess
.SimpleResult<TValue>
: Get data from propertySuccess
Exception
: Get data from propertyException
TValue
: Get data from propertyValue
The property
SuccessAndHasValue
check ifSuccess == true and Value != null
inSimpleResult<TValue>
.
Caution on
SimpleResult<TValue>
: Implicit comparisonif (mySimpleResultVariable)
is equivalent toif (mySimpleResultVariable.Success)
.
Use explicitif (mySimpleResultVariable.SuccessAndHasValue)
when result value can be null with success is true
Use cases:
public SimpleResult Execute(Args args)
{
if (args == null)
return "Argument cannot be null"; // <===== Implicit cast to failure result
try
{
// Execute actions
return true; // <===== Implicit cast success result
}
catch (Exception e)
{
return e; // <===== Implicit cast to failure result
}
}
public SimpleResult<int> Execute2(Args args)
{
if (args == null)
return "Argument cannot be null"; // <===== Implicit cast to failure result
try
{
// Execute actions
return 10; // <===== Implicit cast success result
}
catch (Exception e)
{
return e; // <===== Implicit cast to failure result
}
}
// ...
var result = Execute(...);
var result2 = Execute2(...);
if (result && result2) // <===== Implicit cast to boolean
{
// ...
}
Try<TSuccess, TFailure>
Auxiliary data type to increment the expressibility of method results when success and failure need different structures.
To facilitate development there is support for implicit conversion.
public void Try<PersonCreated, CustomError> Save(Person value)
{
...
if (condition)
return new CustomError() { Code = 999, Message = "Failure message" }
return new PersonCreated { Id = 123 };
}
...
var result = Save(person);
if (result)
Console.WriteLine(result.Value.Id);
else
Console.WriteLine($"Error {result.Failure.Code}: {result.Failure.Message}");
Settings
It's possible set default settings for library:
Extensions.GlobalSettings.Configure(cfg => cfg
.FloatAndDoubleTolerance(default-float-and-double-comparasion-tolerance) // Default is 0.00001f
Development Guidelines
- Create file with extensions method name
this
argument name must bevalue
for single object orvalues
to IEnumerable inheritances- Put new file into folder
/src/Extensions/<extended-data-type>
- Expressions methods like
If
,NullIf
,Is
,In
,EmptyIf
,Between
, must be located insrc/Extensions/Comparable
folder Numbers
extensions methods must bet located insrc/Extensions/Number
- Expressions methods like
- The
I
prefix for interfaces must be omitted from the folder name. src\Settings\MaestriaExtensionSettings
: File to configure global defaults behaviors
Breaking Changes
If my contributions helped you, please help me buy a coffee 😄
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- JetBrains.Annotations (>= 2020.1.0)
- System.ComponentModel.Annotations (>= 4.5.0)
-
net5.0
- JetBrains.Annotations (>= 2020.1.0)
- System.ComponentModel.Annotations (>= 4.5.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
3.5.0 | 1,654 | 1/26/2024 |
3.4.2 | 9,333 | 11/9/2022 |
3.4.1 | 340 | 11/9/2022 |
3.4.0 | 330 | 11/9/2022 |
3.3.2 | 1,086 | 10/7/2022 |
3.3.1 | 798 | 9/12/2022 |
3.3.0 | 412 | 9/12/2022 |
3.2.3 | 729 | 8/27/2022 |
3.2.2 | 411 | 8/26/2022 |
3.2.1 | 404 | 8/26/2022 |
3.2.0 | 2,695 | 5/31/2022 |
3.1.3 | 6,902 | 11/24/2021 |
3.1.2 | 531 | 11/3/2021 |
3.1.0 | 835 | 9/8/2021 |
3.0.2 | 368 | 9/8/2021 |
3.0.1 | 4,505 | 8/4/2021 |
3.0.0 | 428 | 8/4/2021 |
2.1.4 | 1,583 | 7/5/2021 |
2.1.3 | 646 | 6/22/2021 |
2.1.2 | 451 | 6/20/2021 |
2.1.1 | 463 | 6/7/2021 |
2.1.0 | 488 | 6/4/2021 |
2.0.13 | 411 | 6/1/2021 |
2.0.12 | 543 | 5/25/2021 |
2.0.11 | 404 | 5/20/2021 |
2.0.10 | 389 | 5/16/2021 |
2.0.9 | 371 | 5/4/2021 |
2.0.8 | 465 | 4/4/2021 |
2.0.7 | 334 | 4/3/2021 |
2.0.6 | 356 | 4/2/2021 |
2.0.5 | 382 | 3/28/2021 |
2.0.4 | 376 | 3/28/2021 |
2.0.3 | 418 | 3/27/2021 |
2.0.2 | 435 | 3/27/2021 |
2.0.1 | 451 | 3/19/2021 |
2.0.0 | 343 | 3/19/2021 |
1.2.5 | 475 | 3/14/2021 |
1.2.4 | 451 | 1/10/2021 |
1.2.3 | 437 | 1/10/2021 |
1.2.2 | 553 | 9/1/2020 |
1.2.1 | 484 | 8/14/2020 |
1.2.0 | 562 | 8/9/2020 |
1.1.1 | 573 | 7/28/2020 |
1.1.0 | 563 | 7/12/2020 |
1.0.10 | 487 | 4/19/2020 |
1.0.9 | 502 | 4/14/2020 |
1.0.8 | 487 | 4/14/2020 |
1.0.7 | 623 | 4/11/2020 |
1.0.6 | 639 | 12/20/2019 |
1.0.5 | 625 | 12/16/2019 |
1.0.4 | 534 | 12/16/2019 |
1.0.3 | 499 | 11/25/2019 |
1.0.2 | 492 | 11/18/2019 |
1.0.1 | 579 | 10/14/2019 |
1.0.0 | 569 | 10/9/2019 |