Epiforge.Extensions.Expressions
1.4.0
dotnet add package Epiforge.Extensions.Expressions --version 1.4.0
NuGet\Install-Package Epiforge.Extensions.Expressions -Version 1.4.0
<PackageReference Include="Epiforge.Extensions.Expressions" Version="1.4.0" />
paket add Epiforge.Extensions.Expressions --version 1.4.0
#r "nuget: Epiforge.Extensions.Expressions, 1.4.0"
// Install Epiforge.Extensions.Expressions as a Cake Addin #addin nuget:?package=Epiforge.Extensions.Expressions&version=1.4.0 // Install Epiforge.Extensions.Expressions as a Cake Tool #tool nuget:?package=Epiforge.Extensions.Expressions&version=1.4.0
This library has useful tools for dealing with expressions:
ExpressionEqualityComparer
- Defines methods to support the comparison of expression trees for equalityExpressionExtensions
, providing:Duplicate
- Duplicates the specified expression treeSubstituteMethods
- Recursively scans an expression tree to replace invocations of specific methods with replacement methods
Observable
This library accepts a LambdaExpression
and arguments to pass to it, dissects the LambdaExpression
's body, and hooks into change notification events for properties (INotifyPropertyChanged
), collections (INotifyCollectionChanged
), and dictionaries (Epiforge.Extensions.Collections.INotifyDictionaryChanged
).
// Employee implements INotifyPropertyChanged
var elizabeth = Employee.GetByName("Elizabeth");
var observer = new ExpressionObserver();
var expr = observer.Observe(e => e.Name.Length, elizabeth);
// expr subscribed to elizabeth's PropertyChanged
Then, as changes involving any elements of the expression occur, a chain of automatic re-evaluation will get kicked off, possibly causing the observable expression's Evaluation
property to change.
var elizabeth = Employee.GetByName("Elizabeth");
var observer = new ExpressionObserver();
var expr = observer.Observe(e => e.Name.Length, elizabeth);
// expr.Evaluation.Result == 9
elizabeth.Name = "Lizzy";
// expr.Evaluation.Result == 5
Also, since exceptions may be encountered after an observable expression was created due to subsequent element changes, observable expressions include a Fault
property in their evaluations, which will be set to the exception that was encountered during evaluation.
var elizabeth = Employee.GetByName("Elizabeth");
var observer = new ExpressionObserver();
var expr = observer.Observe(e => e.Name.Length, elizabeth);
// expr.Evaluation.Fault is null
elizabeth.Name = null;
// expr.Evaluation.Fault is NullReferenceException
Observable expressions raise property change events of their own, so listen for those (kinda the whole point)!
var elizabeth = Employee.GetByName("Elizabeth");
var observer = new ExpressionObserver();
var expr = observer.Observe(e => e.Name.Length, elizabeth);
expr.PropertyChanged += (sender, e) =>
{
if (e.PropertyName == "Evaluation")
{
var (fault, result) = expr.Evaluation;
if (fault is not null)
{
// Whoops
}
else
{
// Do something with result
}
}
};
When you dispose of your observable expression, it will disconnect from all the events.
var elizabeth = Employee.GetByName("Elizabeth");
var observer = new ExpressionObserver();
using (var expr = observer.Observe(e => e.Name.Length, elizabeth))
{
// expr subscribed to elizabeth's PropertyChanged
}
// expr unsubcribed from elizabeth's PropertyChanged
Observable expressions will also try to automatically dispose of disposable objects they create in the course of their evaluation when and where it makes sense. Use the ExpressionObserverOptions
class for more direct control over this behavior.
You can use the Optimizer
property to specify an optimization method to invoke automatically during the observable expression creation process.
We recommend Tuomas Hietanen's Linq.Expression.Optimizer, the utilization of which would like like so:
var options = new ExpressionObserverOptions { Optimizer = ExpressionOptimizer.tryVisit };
var a = Expression.Parameter(typeof(bool));
var b = Expression.Parameter(typeof(bool));
var lambda = Expression.Lambda<Func<bool, bool, bool>>
(
Expression.AndAlso
(
Expression.Not(a),
Expression.Not(b)
),
a,
b
); // lambda explicitly defined as (a, b) => !a && !b
var observer = new ExpressionObserver(options);
var expr = observer.Observe<bool>(lambda, false, false);
// optimizer has intervened and defined expr as (a, b) => !(a || b)
// (because Augustus De Morgan said they're essentially the same thing, but this involves less steps)
Observable Queries
This library provides re-implementations of LINQ operations, but instead of returning Enumerable<T>
s and simple values, these return IObservableCollectionQuery<T>
s, IObservableDictionaryQuery<TKey, TValue>
s, and IObservableScalarQuery<T>
s.
This is because, unlike traditional LINQ operations, these implementations continuously update their results until those results are disposed.
But... what could cause those updates?
- the source is enumerable, implements
INotifyCollectionChanged
, and raises aCollectionChanged
event - the source is a dictionary, implements
Epiforge.Extensions.Collections.INotifyDictionaryChanged<TKey, TValue>
, and raises aDictionaryChanged
event - the elements in the enumerable (or the values in the dictionary) implement
INotifyPropertyChanged
and raise aPropertyChanged
event - a reference enclosed by a selector or a predicate passed to the method implements
INotifyCollectionChanged
,Cogs.Collections.INotifyDictionaryChanged<TKey, TValue>
, orINotifyPropertyChanged
and raises one of their events
That last one might be a little surprising, but this is because all selectors and predicates passed to Observable Query methods become Observable Expressions (see above).
This means that you will not be able to pass one that an ExpressionObserver
cannot observe (e.g. a lambda expression that can't be converted to an expression tree or that contains nodes that are unsupported).
But, in exchange for this, you get all kinds of notification plumbing that's just handled for you behind the scenes.
Suppose, for example, you're working on an app that displays a list of notes and you want the notes to be shown in descending order of when they were last edited.
var notes = new ObservableCollection<Note>();
var collectionObserver = new CollectionObserver();
var observedNotes = collectionObserver.ObserveReadOnlyList(notes);
var orderedNotes = observedNotes.ObserveOrderBy(note => note.LastEdited, isDescending: true);
notesViewControl.ItemsSource = orderedNotes;
From then on, as you add Note
s to the notes
observable collection, the IObservableCollectionQuery<Note>
named orderedNotes
will be kept ordered so that notesViewControl
displays them in the preferred order.
Since IObservableCollectionQuery<T>
's are automatically subscribing to events for you, you do need to call Dispose
on them when you don't need them any more.
void Page_Unload(object? sender, EventArgs e)
{
orderedNotes.Dispose();
observedNotes.Dispose();
}
Ahh, but what about exceptions?
Well, Observable Expressions contain a Fault
element in their Evaluation
properties, but... you don't really see those Observable Expressions as an Observable Query caller, do ya?
For that reason, Observable Queries all have OperationFault
properties.
You may subscribe to their PropertyChanging
and PropertyChanged
events to be notified when an Observable Expression or the overall Observable Query runs into a problem.
If there is more than one fault in play, the value of OperationFault
will be an AggregateException
.
Since the ExpressionObserver
has a number of options governing its behavior, you may optionally pass one you've made to the constructor of CollectionObserver
to ensure those options are obeyed when Observable Expressions are created to enable your Observable Queries.
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 is compatible. 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 | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
.NET Framework | net462 is compatible. 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 | 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.6.2
- Epiforge.Extensions.Collections (>= 1.5.0)
- Epiforge.Extensions.Components (>= 1.5.0)
- System.Collections.Immutable (>= 7.0.0)
-
.NETStandard 2.1
- Epiforge.Extensions.Collections (>= 1.5.0)
- Epiforge.Extensions.Components (>= 1.5.0)
- System.Collections.Immutable (>= 7.0.0)
-
net6.0
- Epiforge.Extensions.Collections (>= 1.5.0)
- Epiforge.Extensions.Components (>= 1.5.0)
- System.Collections.Immutable (>= 7.0.0)
-
net7.0
- Epiforge.Extensions.Collections (>= 1.5.0)
- Epiforge.Extensions.Components (>= 1.5.0)
- System.Collections.Immutable (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
We made compatibility adjustments for breaking changes in other libraries.