FluentCoding 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package FluentCoding --version 2.0.0
                    
NuGet\Install-Package FluentCoding -Version 2.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="FluentCoding" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FluentCoding" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="FluentCoding" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FluentCoding --version 2.0.0
                    
#r "nuget: FluentCoding, 2.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.
#:package FluentCoding@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FluentCoding&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=FluentCoding&version=2.0.0
                    
Install as a Cake Tool

What's NEW

  • Do now ALWAYS return the subject (even when called with a Func)
  • Removed the Do extension with a single Action/Func, left only the extension with params[] Action/Func
  • Added the DoAsync extension
  • Changed DoForAll into DoForEach, added DoForEachAsync
  • Changed DoForAllMap into MapForEach, added MapForEachAsync
  • Removed EqualsToAny and EqualsTo (with input a comparator) same as EquivalentTo or EquivalentToAny
  • Added EqualsToAnyAsync (without input comparator)
  • Added MapAsync
  • Collapsed Switch and SwitchMap into Switch
  • Added SwitchAsync
  • Added TryAsync and TryToAsync
  • Collapsed Then(onSucces, onFail) and ThenMap(onSucces, onFail) into Then
  • Added WhenAnyAsync, WhenAsync

FluentCoding

Set of functionalities to extend linq with more fluent capabilities This functionalities can be combined together to fluently manipulate an object:

Do,Equals,Is, Map,Or, Switch,Try, When

Almost all the Fluent extension expose the Async version that can be used with a Task<T>. Currently excluded: Is

Do

Do something with/to and object and return the object: Do, DoForEach,DoAsync, DoForEachAsync

Do, DoAsync

When the Do subject is null it just return the subject

Action Apply one or more Actions to the subject and the return the subject

identity.Do(_ => _.Name = "John");
//or
identity.Do(_ => _.Name = "John",
            _ => _.Surname = "Smith");

Func Apply one or more Func to the subject and then return the Subject

private TypeT UpdateIdentity(TypeT identity)
{ 
  [...] //do stuff
  return updatedIdentity
}

var identitiesList = new List<Identity>();
identitiesList.Add(identity.Do(UpdateIdentity));

Async

Task<Identity> identity = GetIdentityFromAPIAsync(...);
identity.DoAsync(_ => _.Name = "John").Result;
//or
identity.DoAsync(_ => _.Name = "John",
            _ => _.Surname = "Smith").Result;

DoForEach, DoForEachAsync

Apply one ore more Actions or a Function to each item in an Enumerable and then return the Enumerable itself

IEnumerable<Identity> itentities = LoadIdentitiesDataBase(...);

//update the LastUpdate field for each item in identities
itentities.DoForEach(_ => _.LastUpdate = DateTime.Now); 

//or

//update the LastUpdate field and the LastUpdateAuthor for each item in identities
itentities.DoForEach(_ => _.LastUpdate = DateTime.Now,
                    _ => _.LastUpdateAuthor = "Admin");

Async

Task<IEnumerable<Identity>> itentities = GetIdentitiesFromAPIAsync(...);

//update the LastUpdate field for each item in identities
itentities.DoForEachAsync(_ => _.LastUpdate = DateTime.Now).Result; 

//or

//update the LastUpdate field and the LastUpdateAuthor for each item in identities
itentities.DoForEachAsync(_ => _.LastUpdate = DateTime.Now,
                          _ => _.LastUpdateAuthor = "Admin").Result;

Equals

Expand the equality functions: EqualsToAny, EquivalentTo, EquivalentToAny

When the subject is null it return false

EqualsToAny

bool EqualityCheck(Identity p1, Identity p2) => p1.Pincode == p1.Pincode;

Identity people1 = ReadFromDataBase(...);
Identity people2 = ReadFromDataBase(...);
Identity people3 = ReadFromDataBase(...);
Identity people4 = ReadFromDataBase(...);

//The framework Equals is used in this case
people1.EqualsToAny(people2, people3, people4);
"XX".EqualsToAny("YY", "TT", "XX", "VV"); 

EquivalentTo - EquivalentToAny

bool EqualityCheck(Identity p1, Identity p2) => p1.Pincode == p1.Pincode;

Tesla tesla = new Tesla() { ... };
Ferrari ferrari = new Ferrari() { ... };
Ferrari ferrari2 = new Ferrari() { ... };
Ferrari ferrari3 = new Ferrari() { ... };

tesla.EquivalentTo(ferrari, (t, f) => t.PlateNumber == f.PlateNumber);

tesla.EquivalentToAny((t, f) => t.PlateNumber == f.PlateNumber, ferrari, ferrari2, ferrari3);

Is

Functionalities: Is, IsNullOrEquivalent

IsNullOrEquivalent

Handy shorthand method to check if something is null or an equivalent state Expose a way to specify more option to check fo null or equivalent state. Via action on a 'IsNullOptions' object, or submitting an 'IsNullOptions' object

string.Empty.IsNullOrEquivalent(); //true
null.IsNullOrEquivalent(); //true
" ".IsNullOrEquivalent(); //false
objectInstance.IsNullOrEquivalent(); //false
"".IsNullOrEquivalent(_ => _.StringIsNullWhen = StringIsNullWhenEnum.Null); //false
"".IsNullOrEquivalent(_ => _.StringIsNullWhen = StringIsNullWhenEnum.NullOrEmpty); //true
"  ".IsNullOrEquivalent(_ => _.StringIsNullWhen = StringIsNullWhenEnum.Null); //false
"  ".IsNullOrEquivalent(_ => _.StringIsNullWhen = StringIsNullWhenEnum.NullOrEmptyOrWhiteSpaces); //true

var options = IsNullOptions.StringIsNullWhenNull;
"".IsNullOrEquivalent(options); //false
" ".IsNullOrEquivalent(options); //false

options = IsNullOptions.StringIsNullWhenNullOrEmpty;
"".IsNullOrEquivalent(options); //true
" ".IsNullOrEquivalent(options); //false
public enum TestEnum { Enum1, Enum2 }
TestEnum.Enum1.IsNullOrEquivalent(); //False
TestEnum.Enum2.IsNullOrEquivalent(); //False
public static Func<bool> NullFunc = null;
public static Func<bool> NotNullFunc = () => true;
NullFunc.IsNullOrEquivalent(); //true
NotNullFunc.IsNullOrEquivalent(); //false

Is

Apply a boolean predicate to an object and obtain the preticate result along with the subject. Can be combined with other functions from this library in a fluent way

Address address = new Address() { ... }; 
(var isSatisfied, var checkSubject) = address.Is(_ => _.Country == "ITA");

var result = address.Is(_ => _.LastUpdate > DateTime.Now.AddYears(-5));
if(result.IsSatisfied) 
   result.Subject; /*do something*/

Map

Convert a Type into another one: Map, MapForEach,MapAsync, MapForEachAsync

Map

Identity people = ReadFromDataBase(...);
var address = people.Map(p => new Address() {Street = p.Domicile, Country = p.BornCountry, ...});
Tesla ConvertToTesla(Ferrari f) 
{
    //[...] do something
    return teslaConversion;
}

TeslaSoftware ExtractSoftware(Tesla t) 
{
    //[...] do something
    return softwareFromTesla;
}

Ferrari ferrari = new Ferrari() { ... };
var sw = ferrari.Map(ConvertToTesla)
                .Map(ExtractSoftware);

MapForEach

Return an enumerable with the result of all the call to Map(item)

IEnumerable<Car> cars = ReadCarsFromDataBase(...);
var carsSoftware = cars.MapForEach(ExtractSoftware);
//do soemthing...
carsSoftware.Where(_ => _.Version >= 1.4).[...];

Or

Choose when pick the right object based on a predicate. By default Left when not null

Or

var currentAddress = object.AddressDomicile.Or(object.AddressResidence);
var validData = object1.Or(object2, (subject)=> !subject.IsStillValid);
var mostRecentData = dataSource1.Or(dataSource2, (subject, orValue)=> orValue.LastUpdateTime > subject.LastUpdateTime);

OrIsEmpty

(for strings)

string newAddress = null; //left string is null
string oldAddress = "address-value";
newAddress.Or(oldAddress); // return oldAddress
newAddress.OrIsEmpty(oldAddress); // return oldAddress

string newAddress = ""; //left string is empty
string oldAddress = "address-value";
newAddress.Or(oldAddress); // return newAddress !!
newAddress.Or(oldAddress, newAddr => string.IsNullOrEmpty(newAddr)); // return oldAddress
newAddress.OrIsEmpty(oldAddress); // return oldAddress

Switch

Fluent version of the switch case: Switch

Switch

Keep the same type on the output T → T

Identity people = new Identity() { ... };
Identity ApiGetPeople(string pincode) { ... return people; }
Identity ApiGetPeopleAddress(people) { ... return peopleAddress; }

var updatedPeople =
people.Switch
(
    p => p,
    (p => p.LastUpdate < DateTime.Node.AddDays(-30) , p => ApiGetPeople(p.Pincode)),
    (p => p.LastUpdate < DateTime.Node.AddDays(-10) , p => p.Do(_ => _.Address = ApiGetPeopleAddress(p)))
)

Switch

Change the output type from the subject type to the type from the function output: T → K

Identity people = new Identity() { ... };

var ageType = 
people.Switch
(
    p => Enum.Old,
    (p => p.YearsOld < 18 , p => Enum.Child),
    (p => p.YearsOld > 18  &&  p.YearsOld < 60 , p => Enum.Adult)
);

TryCatch

Inline wrap methods for the Try{}Catch{}: Try, TryTo, TryAsync, TryToAsync

Try (base class)

Try to do something and return a context with all the information

Car LoadCarData(string licensPlate)
{
 //[...] do something
 return car; 
}

var tryResult = "xxxxx".Try(LoadCarData);
tryResult.IsSuccesful; //true or false
tryResult.Subject; //the input string licensePlace
tryResult.Result; //the Car object loader
tryResult.Error; //the Exception raised when loading the car data
Car LoadCarData(string licensePlate)
{
 //[...] do something
 return car; 
}

CustomError ManageException(String licensePlate, Exception e) => new CustomError(e.Messge, licensePlate);

var tryResult = "carLicensePlate".Try(LoadCarData, ManageException);
tryResult.IsSuccesful; //true or false
tryResult.Subject; //the input string licensePlace
tryResult.Result; //the Car object loader
tryResult.Error; //the CustomError returned by ManageException

Try.OnSuccess or Try.OnFail

Try to do something and when ok do something else

Car LoadCarData(string licensPlate)
{
 //[...] do something
 return car; 
}
List<CarComponent> DisassembleCar(Car car)
{
 //[...] do something
 return carComponents; 
}

(var Components, var tryCatchContext) = "xxxxx".Try(LoadCarData)
                                               .OnSuccess(DisassembleCar);                       
Components; //the disassembled car components ONLY WHEN no exception occurred, default of List<CarComponent> otherwise
tryCatchContext; //the TryCatch class from the previous example, ALWAYS returned
Car LoadCarData(string licensPlate)
{
 //[...] do something
 return car; 
}
CustomError ManageException(String licensePlate, Exception e) => new CustomError(e.Messge, licensePlate);

List<Car> availableCar = new List<Car>();

(var error, var tryCatchContext) = "xxxxx".Try(plate => availableCar.Add(LoadCarData(plate))
                                          .OnFail(ManageException);
                       
error; //the CustomError ONLY WHEN and exception occurred, default of CustomError otherwise
tryCatchContext; //the TryCatch class from the previous example, ALWAYS returned

Try.Then

Try to do something and then manage the success or the fail result

Car LoadCarData(string licensPlate)
{
 //[...] do something
 return car; 
}
bool AddCarToStock(Car car) 
{
 //[...] do something
 return true; 
}
bool TraceCarError(string plate, Exception e) 
{
 //[...] do something
 return true; 
}

CustomError ManageException(String licensePlate, Exception e) => new CustomError(e.Messge, licensePlate);

"xxxxx".Try(LoadCarData)
       .Then(AddCarToStock, TraceCarError);                       

TryTo

Try to do something or manage the exception, the output type can differ from the subject input type

var date = "2022-12-29".TryTo(DateTime.Parse, (c, ex) => DateTime.MinValue);

When

Apply one or more checks on the subjects and then apply and Action or a Function only when all the checks are satisfied

When (base class)

var car = LoadCarData(...);

When<Car> result = car.When(c => c.Type == "Ferrari");

result.IsSuccess; // the predicate result
result.Subject; //the predicate subject

When.Then

Keep the same input type T → T

var car = LoadCarData(...);
c.Insurance = InsuranceType.LowBudget;

car.When(c => c.Type == "Ferrari")   
   .Then(c => c.Insurance = InsuranceType.Luxury);

Change the input type T → K

var car = LoadCarData(...);
Ferrari ConvertToFerrari(Car car) 
{
 //[...] do something
 return ferrari; 
}

Ferrari CreateNewFerrari(Car car)
{
 //[...] do something
 return newFerrari; 
}

//when
var ferrari = car.When(c => c.Type == "Ferrari")      
                 .ThenMap(ConvertToFerrari, CreateNewFerrari);

When.ThenAnd

Concatenate more Then on the subject. The ThenAnd output is the When context, to close a chain of ThenAnd the latest should be a Then

var car = LoadCarData(...);
c.Insurance = InsuranceType.LowBudget;

car.When(c => c.Type == "Ferrari")   
   .ThenAnd(c => c.Insurance = InsuranceType.Luxury)
   .ThenAnd(c => c.Color = "Red")
   .Then(c => c.Available = true)

When.OrWhen.Then

Apply one or more checks conditions on the subject, in Or condition with the current succesful state

var car = LoadCarData(...);
c.Insurance = InsuranceType.LowBudget;

car.When(c => c.Type == "Ferrari")
   .OrWhen(c => c.Type == "Lamborghini")
   .Then(c => c.Insurance = InsuranceType.Luxury);

When.OrWhen.AndWhen.Then

Apply one or more checks conditions on the subject, in Or condition with the with the current succesful state Then Apply one or more checks conditions on the subject, in And condition with the current succesful state When Once AndWhen is used the OrWhen is not available anymore

var car = LoadCarData(...);
c.Insurance = InsuranceType.LowBudget;

car.When(c => c.Type == "Ferrari")
   .OrWhen(c => c.Type == "Lamborghini")
   .AndWhen(c => c.MarketPrice >= 180000)
   .Then(c => c.Insurance = InsuranceType.Luxury);

When.ThenMap only when True

Map the subject when condition is satisfied, if not return the subject

var car = LoadCarData(...);
Ferrari ConvertToFerrari(Car car) 
{
 //[...] do something
 return ferrari; 
}

//when
(Ferrari ferrari, Car subject) = car.When(c => c.Type == "Ferrari")      
                                    .ThenMap(ConvertToFerrari);
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.8

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net6.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on FluentCoding:

Package Downloads
FluentCoding.String

Updated to match last signed version of FluentCoding

FluentCoding.Enum

Updated to match last signed version of FluentCoding

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.3 1,041 4/3/2023
2.1.2 307 3/29/2023
2.1.1 578 3/16/2023
2.1.0 617 2/20/2023
2.0.1 375 2/10/2023
2.0.0 767 1/11/2023
1.2.1 767 1/5/2023
1.2.0 765 1/5/2023
1.1.0 756 1/3/2023
1.0.4 952 1/2/2023
1.0.2 770 1/2/2023
1.0.1 425 1/5/2023

# What's NEW
- Do now ALWAYS return the subject (even when called with a Func)
- Removed the Do extension with a single Action/Func, left only the extension with params[] Action/Func
- Added the DoAsync extension
- Changed DoForAll into DoForEach, added DoForEachAsync
- Changed DoForAllMap into MapForEach, added MapForEachAsync
- Removed EqualsToAny and EqualsTo (with input a comparator) same as EquivalentTo or EquivalentToAny
- Added EqualsToAnyAsync (without input comparator)
- Added MapAsync
- Collapsed Switch and SwitchMap into Switch
- Added SwitchAsync
- Added TryAsync and TryToAsync
- Collapsed Then(onSucces, onFail) and ThenMap(onSucces, onFail) into Then
- Added WhenAnyAsync, WhenAsync