The State Management Protocol is a protocol used for state management using events. This protocol was presented at the talk below and ported to WPF by leveraging routed events. This implementation was mainly built to facilitate the use of the Flux pattern in WPF instead of the MVVM pattern. This implementation only provides the mechanisms to write to the store and should be used in conjunction with the Wpf.DependencyResolution protocol to expose the store to descendant elements.
This implementation provides extensions that can be used with elements to allow elements to store state and decouple side effects. When using this protocol, an implementation of the store must be provide through the IStore
interface. This allows for extremely basic store implementation, as well as more complex implementation using observables, such as ReactiveX.
The use of this protocol assumes knowledge of Flux and/or any other implementations such as Elm and Redux.
- Implement a store using the
IStore
interface. - Create routed events by extending the
FluxAction
class to request updates to the store. - Create reducers using the
IReducer
interface to update the store. - Call
ProvideStore
using theStoreProvider
extension on the element to act as the orchestrator. - (Optional) Using Wpf.DependencyResolution, expose the store to descendants using the
ProvideInstance
method from theInstanceProvider
extension. - (Optional) Call
Enhance
using theEnhancer
extension on any descendant element to intercept, modify, or make side effecting calls. Enhancers are applied starting from the Enhancer farthest away from the StoreProvider in the element tree. As well, create any other routed events by extendingFluxAction
that may be required by the Enhancer. - Call
Dispatch
with routed events extendingFluxAction
.
This getter provides the current state of the store.
This method should trigger the generation of new state. It should consequently trigger a property change on the State property.
This method is the implementation of the Reducer to provide the new state of the store.
This class is a wrapper for RoutedEventArgs and is mainly used to identify routed events meant for State Management.
By calling ProvideStore
with a IStore
within an element, that element becomes a listener for any FluxActions
and will dispatch those actions to the IStore
. This is our bridge between the WPF event system and the store.
By calling Enhance
with a DispatchHandler
within an element, that element becomes a listener for any FluxActions
and will execute the DispatchHandler
. This is useful to executing side effecting code. In an ideal setup, all side effecting code should be isolated within services that collaborate with the Enhancer
. This allows the Enhancer
to be tested easily as a series of dispatches. Once again, Wpf.DependencyResolution can be used to inject services into the Enhancer
.
By calling Dispatch
with any FluxAction
within an element will trigger a store update at the StoreProvider
. Any Enhancer
in between the element and the StoreProvider
can react to the dispatched FluxAction
. Dispatch
is basically a wrapper for the native RaiseEvent
, but can be used to replace the event bus within the protocol if required.