Sobes.tech
Principal

What is AUS-2?

sobes.tech AI

Answer from AI

AUS-2, or Asynchronous State Management 2nd Edition, is a set of patterns and recommendations for managing state in asynchronous applications in C#. It aims to simplify working with code that contains many asynchronous operations and to ensure its predictability and stability. The main principles of AUS-2 include:

  • Clear separation of responsibilities. The application's state is clearly separated from the logic that changes it.
  • Encapsulation of state changes. State changes occur only through specific, controlled actions (e.g., commands or events).
  • Asynchrony by nature. All operations that change state and handle external influences are considered potentially asynchronous.
  • Use of thread-safe data structures. Data structures resistant to concurrent access from different threads are used.
  • Immutability. Immutable state objects are used whenever possible, simplifying change tracking and preventing unwanted side effects.

Typical components of an architecture following AUS-2 principles include:

  • State: An object(s) representing the current state of the application or its part.
  • Action/Command: An object describing the intention to change the state.
  • Handler/Reducer: A function or object that takes the current state and action and returns a new state. All state change logic occurs in the handler.

Advantages of using AUS-2:

  • Improved testability. Clear separation and encapsulation make state change logic easily testable.
  • Predictability. State changes occur only through specific mechanisms, making application behavior more understandable and predictable.
  • Debugging convenience. Easier to trace how and why the application's state changed.
  • Scalability. The architecture scales well as the application's complexity grows.

Example of a simple implementation of the principle (without using a full framework):

// Immutable state
public record AppState(int Count);

// Action type
public record IncrementAction();

// Handler
public static class StateHandler
{
    public static AppState Handle(AppState currentState, IncrementAction action)
    {
        // Create a new state instead of modifying the existing one
        return new AppState(currentState.Count + 1);
    }
    // Other handlers for other action types...
}

// Usage example (simplified, without considering asynchrony and thread safety in this specific example)
/*
AppState currentState = new AppState(0);
currentState = StateHandler.Handle(currentState, new IncrementAction());
Console.WriteLine(currentState.Count); // Outputs 1
*/

AUS-2 is not a specific library but rather a set of principles that can be implemented using various patterns (e.g., Redux-like) and libraries within the .NET ecosystem.