Observable state management primitives with state manager and view state base classes.
$ dotnet add package FastPatterns.ObserverA collection of lightweight design pattern implementations for .NET applications.
FastPatterns.Core provides foundational utilities and building blocks for .NET applications. It includes:
using (new ScopedStopwatch(elapsed =>
Console.WriteLine($"Elapsed: {elapsed.TotalMilliseconds} ms")))
{
Task.Delay(1000).Wait(); // simulate work
}
FastPatterns.Extensions offers reusable extension methods and utility types to enhance .NET development, focusing on logging, diagnostics, and resource management:
ILogger to simplify logging timed operations. The TimedScope method creates a disposable scope that logs the duration of an operation when disposed.SimpleMapper, MapWith, and RecordMapper provide lightweight object-to-object mapping helpers.void HookTemporaryEvent(Button btn)
{
btn.Click += OnClick;
using var _ = new DisposableAction(() => btn.Click -= OnClick);
// Scoped logic...
}
public class OrderService
{
private readonly ILogger<OrderService> _logger;
public OrderService(ILogger<OrderService> logger)
{
_logger = logger;
}
public async Task ProcessOrderAsync()
{
using (_logger.TimedScope("ProcessOrder"))
{
await Task.Delay(500); // simulate work
}
}
}
FastPatterns.Extensions provides three small helpers for copying values between types:
SimpleMapper then lets you run additional mapping logic via a delegate.var source = new Source();
var dest = source.MapWith<Source, Dest>((src, dest) =>
{
dest.NameReversed = src.Name.ReverseString();
dest.Nested = SimpleMapper<NestedSource, NestedDest>.Map(src.Nested);
dest.Car = RecordMapper.MapToRecord<Car, CarDto>(source.Car);
});
You can use the IDataProtectionService to encrypt specific EF Core properties like this:
var encryptionConverter = new ValueConverter<string, string>(
v => dataProtectionService.Encrypt(v),
v => dataProtectionService.Decrypt(v)
);
modelBuilder.Entity() .Property(e => e.Name) .HasConversion(encryptionConverter);#### 🔧 Setup in Program.cs builder.Services.AddFastPatternDataProtection(builder.Configuration);"FastPatterns:Extensions:Security": { "EncryptionKey": "your-key-here" }
The FastPatterns.Mediator project provides a simple mediator with request/response and notification support.
IMediator exposes SendAsync for requests and PublishAsync for notifications.IRequestHandler<TRequest, TResponse> while notifications use INotificationHandler<TNotification>.IPipelineBehavior allows cross-cutting code to run before and after a request handler.IServiceCollection.AddValidationBehavior registers the built-in validation pipeline and discovers all IValidator<T> implementations.samples/FastPatterns.Mediator.WebApiSample for a minimal API example.var services = new ServiceCollection();
services.AddMediator()
.AddValidationBehavior();
var mediator = services.BuildServiceProvider()
.GetRequiredService<IMediator>();
await mediator.SendAsync(new CreateUserCommand
{
Name = "John Doe",
Username = "doe87",
Email = "john@doe.com"
});
FastPatterns.Observer implements a basic observable state system.
Observer manages subscriptions using AddStateChangeListeners and BroadcastStateChange.StateManager<TState> extends Observer and exposes a SetState method that only broadcasts when the state changes.StateStoreBase<TState> provides an asynchronous store with EnsureLoadedAsync, ReloadAsync, and Invalidate helpers.SingleFlight<TKey, TResult> implements a single-flight pattern that guarantees only one producer runs per key while other callers await the same task.ViewState records a timestamp and loading flag and can be extended with custom state data.samples/FastPatterns.Observer.BlazorWASM shows how to manage component state with these classes.var sf = new SingleFlight<string, WeatherState>();
var state = await sf.RunAsync("weather", _ => LoadWeatherAsync(), ct);
public class WeatherStore : StateStoreBase<WeatherState>
{
protected override Task<WeatherState?> LoadCoreAsync(CancellationToken ct) => LoadWeatherAsync();
}
await weatherStore.EnsureLoadedAsync();
ObservableComponentBase<TState> in FastPatterns.Blazor integrates the observer pattern with Blazor components. It:
TState.OnInitialized and triggers StateHasChanged when notified.These libraries provide small building blocks that can be composed to add mediator and observer patterns to .NET applications.