dotnet add package Bloc.NET --version 2.0.0
NuGet\Install-Package Bloc.NET -Version 2.0.0
<PackageReference Include="Bloc.NET" Version="2.0.0" />
paket add Bloc.NET --version 2.0.0
#r "nuget: Bloc.NET, 2.0.0"
// Install Bloc.NET as a Cake Addin #addin nuget:?package=Bloc.NET&version=2.0.0 // Install Bloc.NET as a Cake Tool #tool nuget:?package=Bloc.NET&version=2.0.0
A predictable state management library for C# that helps implement the BLoC design pattern.
<p align="center"> <img alt="Bloc.NET" src="Bloc.NET/icon.png" width="200"> </p>
You may know this library from it's more famous counterpart in the Dart/Flutter universe — but there's no reason we can't have one for .NET, too! Thanks to Felix (the creator of the original Bloc library) for consulting on this C# port.
In some ways, a bloc is similar to a state machine, but does not require the developer to define the entire table of state transitions ahead of time. Instead of being told to transition to a specific state, blocs listen to a stream of events and yield out states.
The increase in flexibility that comes from using blocs reduces the pain of implementing complex or nested/hierarchical state machines (and state charts, for that matter). The reduction of code allows the developer to focus more clearly on constructing the correct state in response to events, but does lack the rigidity of a state machine. Feel free to use whatever patterns are appropriate for your application.
There are a few things to be aware of regarding this implementations:
No docs! If you're interested in the pattern, I suggest reading the documentation for the Dart/Flutter version of Bloc. Dart and C# are both object-oriented, managed languages with garbage collection, so the concepts are similar enough.
Not actually equivalent to modern Dart/Flutter blocs. The bloc implementation in this library cannot support concurrent events — all events are processed sequentially and in-order. If events take a while to process, they simply queue up. This is equivalent to how the original Bloc library worked prior to Bloc v8.
Both asynchronous and synchronous implementations are provided for C#. The Dart/Flutter implementation does not have a specific synchronous implementation. Since C# is commonly used in game development (and I ported this package to use it with game development), I am providing a synchronous implementation as I believe it will be useful in many situations.
The synchronous implementation differs slightly from the asynchronous one: if an error occurs in the asynchronous implementation, it does not throw and stop execution (since the event is processed asynchronously, errors are not thrown as they would not even be in the same context of the event that triggered the error). Instead, the error is added to the bloc and the bloc can choose how to handle it.
The synchronous implementation, however, will throw an exception when an event is added that triggers an invalid state. I believe this is desirable as it greatly facilitates debugging in simpler situations where forcing everything to be asynchronous is overly complex or too much of a performance hit.
|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.|
|.NET Core||netcoreapp3.0 was computed. netcoreapp3.1 was computed.|
|.NET Standard||netstandard2.1 is compatible.|
|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.|
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.