fluentmvvm 0.2.0-alpha
It is now recommended to use Microsoft's MVVM Community Toolkit as a preferred alternative to this package. Its source generators allow to write boilerplate-free view models without any runtime penalty.
See the version list below for details.
dotnet add package fluentmvvm --version 0.2.0-alpha
NuGet\Install-Package fluentmvvm -Version 0.2.0-alpha
<PackageReference Include="fluentmvvm" Version="0.2.0-alpha" />
paket add fluentmvvm --version 0.2.0-alpha
#r "nuget: fluentmvvm, 0.2.0-alpha"
// Install fluentmvvm as a Cake Addin #addin nuget:?package=fluentmvvm&version=0.2.0-alpha&prerelease // Install fluentmvvm as a Cake Tool #tool nuget:?package=fluentmvvm&version=0.2.0-alpha&prerelease
Property Declaration
Instead of defining backing fields for each property, simply use Get()
for the property getter and Set(value)
for the property setter.
public class PersonViewModel : FluentViewModelBase
{
public string Name
{
get => this.Get<string>();
set => this.Set(value);
}
}
Set
raises a PropertyChanged
event only if the new value is different from the old value.
Remarks
Note that this mechanism only works for public instance properties with a set
accessor.
Dependencies between properties
For cases where a (computed) property's value depends on the values of other properties, Affects
can be used after Set
.
It raises a PropertyChanged
event for the specified property if the new value was different from the old one.
public string FullName => $"{this.FirstName} {this.LastName}";
public string FirstName
{
get => this.Get<string>();
set => this.Set(value)
.Affects(nameof(this.FullName));
}
public string LastName
{
get => this.Get<string>();
set => this.Set(value)
.Affects(nameof(this.FullName));
}
Dependencies between properties and commands
For cases when a commands CanExecute
is based on the value of some property, Affects
can be used with the command to raise the CanExecuteChanged
event for.
public ICommand BuyThingsCommand { get; }
public int Balance
{
get => this.Get<int>();
set => this.Set(value)
.Affects(this.BuyThingsCommand);
}
Remarks
Affects
accepts a parameter of type ICommand
. However, if the command does not contain a
public void RaiseCanExecuteChanged()
method, an exception is thrown. You can also derive from the interface IWpfCommand
provided by fluentmvvm in order to make sure that aforementioned method exists on your command implementations.
Fluent API
FluentViewModelBase
provides a fluent API that can be used in property setters.
this.When(<condition>) // never or once
.Set(value) // exactly once
.Affects(<property name>) // never, once or many times
.Affects(<command>) // never, once or many times
.WasUpdated(); // never or once
Overview
If the condition passed to
When
evaluates tofalse
, the calls toSet
andAffects
do not execute anything.Set
checks whether the new value differs from the current value of the property. If it does differ, it sets the value to the property and raises aPropertyChanged
event. If it does not, nothing happens and all followingAffects
calls do not execute either.When expressing a dependency between one property and another,
Affects
can be used with the name of the property to raise aPropertyChanged
event for. The event is raised only if the new value of the property differed from the old value.If your command implementations derive from
IWpfCommand
, dependencies between properties and commands can also be expressed usingAffects
. That way, when the new value of the property differs from the old value, aCanExecuteChanged
event is raised for the command.WasUpdated
returns abool
value indicating whether the property value was updated (and thus whether aPropertyChanged
event was raised for the property).
Remarks
As code is dynamically emitted at runtime, fluentmvvm can not be used in Ahead of time (AOT) environments such as Xamarin.Android, Xamarin.iOS, Mono or .NET Native.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. 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
- Microsoft.CSharp (>= 4.7.0)
- System.Dynamic.Runtime (>= 4.3.0)
- System.Reflection.Emit (>= 4.7.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 | |
---|---|---|---|
0.6.1-alpha | 273 | 2/2/2021 | |
0.6.0-alpha | 204 | 2/2/2021 | |
0.5.0-alpha | 240 | 1/2/2021 | |
0.3.2-alpha | 369 | 3/19/2020 | |
0.3.1-alpha | 330 | 3/19/2020 | |
0.3.0-alpha | 345 | 3/8/2020 | |
0.2.0-alpha | 345 | 12/11/2019 |
- Added attributes to control backing field generation.
- Removed (unintentional) limit on the number of properties a view model can have