FluentCoding 1.2.0
See the version list below for details.
dotnet add package FluentCoding --version 1.2.0
NuGet\Install-Package FluentCoding -Version 1.2.0
<PackageReference Include="FluentCoding" Version="1.2.0" />
<PackageVersion Include="FluentCoding" Version="1.2.0" />
<PackageReference Include="FluentCoding" />
paket add FluentCoding --version 1.2.0
#r "nuget: FluentCoding, 1.2.0"
#:package FluentCoding@1.2.0
#addin nuget:?package=FluentCoding&version=1.2.0
#tool nuget:?package=FluentCoding&version=1.2.0
FluentCoding
Set of functionalities to extend linq with more fluent capabilities This functionalities can be combined together to fluently manipulate an object:
ForEach, When, Or, Is, Try, Do, Equals, Map, Switch
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
Do
When the Do subject is null it just return the subject
Action Apply the Action to the subject and the return the subject
identity.Do(_ => _.Name = "John");
//or
identity.Do(_ => _.Name = "John",
_ => _.Surname = "Smith");
Func Apply the Func to the subject and the return the Func result
private TypeT UpdateIdentity(TypeT identity)
{
[...] //do stuff
return updatedIdentity
}
var identitiesList = new List<Identity>();
identitiesList.Add(identity.Do(UpdateIdentity));
Equals
Expand the equality functions: EqualsTo, EqualsToAny, EquivalentTo, EquivalentToAny
When the subject is null it return false
EqualsTo - EqualsToAny
bool EqualityCheck(Identity p1, Identity p2) => p1.Pincode == p1.Pincode;
Identity people1 = ReadFromDataBase(...);
Identity people2 = ReadFromDataBase(...);
Identity people3 = ReadFromDataBase(...);
Identity people4 = ReadFromDataBase(...);
people1.EqualsTo(people2, EqualityCheck);
people1.EqualsToAny(EqualityCheck, people2, people3, people4);
"XX".EqualsToAny("YY", "TT", "XX", "VV"); //when no specified an equality check, the framework Equals is used
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);
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
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);
Switch
Fluent version of the switch case: Switch, SwitchMap
Switch
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)))
)
SwitchMap
Change the output type from the subject type to the type from the function output
Identity people = new Identity() { ... };
var ageType =
people.SwitchMap
(
p => Enum.Old,
(p => p.YearsOld < 18 , p => Enum.Child),
(p => p.YearsOld > 18 && p.YearsOld < 60 , p => Enum.Adult)
);
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
var car = LoadCarData(...);
c.Insurance = InsuranceType.LowBudget;
car.When(c => c.Type == "Ferrari")
.Then(c => c.Insurance = InsuranceType.Luxury);
When.ThenAnd
Concatenate more Then on the subject, the ThenAnd outpuyt 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.Or.Then
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.Or.And.Then
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
var car = LoadCarData(...);
Ferrari ConvertToFerrari(Car car)
{
//[...] do something
return ferrari;
}
//when
(Ferrari ferrari, Car subject) = car.When(c => c.Type == "Ferrari")
.ThenMap(ConvertToFerrari);
When.ThenMap when True or When False
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);
TryCatch
Inline wrap methods for the Try{}Catch{}
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);
ForEach
Expand the ForEach function to all the IEnumerable types
Action
int[] original = { 1, 2, 3 };
var reworked = new List<string>();
original.ForEach(_ => reworked.Add(_.ToString()));
Function:
Return a new IEnumerable with the result of the function applied to each item
int[] original = { 1, 2, 3 };
IEnumerable<string> reworked = original.ForEach(_ => _.ToString());
| Product | Versions 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. |
-
.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.
Reworked the base extensions hidden null management, to not use IsNullOrEquivalent() this to avoid unwanted or unexpected results
Reworked IsNullOptions to have only an enum to manage how to process a string check
Added some IsNullOptions default Setups as static fields
Reworked base extensions to avoid to use other base extensions to reduce overhead and unwanted side effects
Or extensions by default choose right only when subject is null, added OrIsEmpty to manage empty strings as null by default
Renamed ForEach with the input Func as ForEachMap
Added new extensions