π SnapshotIt is a lightweight, extensible C# library for .NET that allows you to easily snapshot (capture), restore, and copy the state of your objects at any point in your application. Perfect for undo/redo functionality, state recovery, and seamless state management!
- Snapshotting: Instantly capture the state of any object and restore it when needed.
- Copying: Deep-copy objects to avoid unwanted side effects.
- Unbounded Channel (
BufferBlock<T>): Use as an unbounded channel for posting, reading, and closing items. - Dependency Injection Helpers: Easy integration with ASP.NET and .NET DI containers.
- Extensible: Add new features and extensions easily.
- FluentValidation Integration: Validate objects when saving or restoring snapshots (see
SnapshotIt.FluentValidation).
- .NET 6 or .NET 7
Add the package to your project (coming soon via NuGet):
dotnet add package SnapshotItOr clone this repo and reference directly.
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
// Copy class
var product = new Product { Id = 1, Name = "Product" }; // reference type, mutable
var product2 = Snapshot.Out.Copy(product); // deep copy
// Create a snapshot collection
Snapshot.Out.Create<Product>(10); // initialize collection with room for 10 snapshots
// Capture the state
Snapshot.Out.Post(product);
// Mutate the object
product.Id = 2;
product.Name = "Gucci";
// Restore previous state
var capturedProduct = Snapshot.Out.Get<Product>(0); // get first snapshot
var capturedByPredicate = Snapshot.Out.Get<Product>(p => p.Id == 1);Logging Example:
log.Information($"The product's name is {product.Name}"); // Logs: Gucci
log.Information($"The previous product's name was {capturedProduct.Name}"); // Logs: Nikeusing SnapshotIt.DependencyInjection;
// Standard DI via constructor
public class UserController : BaseController
{
public ILogger logger;
public UserController(ILogger logger)
{
this.logger = logger;
}
}
// Or get directly from DI container
public class UserController : BaseController
{
public ILogger logger = Connector.GetService<ILogger>();
}using SnapshotIt.DependencyInjection;
[RuntimeDependencyInjectionOption(Lifetime = ServiceLifetime.Scoped)]
public class ProductRepository : IProductRepository {}
[RuntimeDependencyInjectionOption(Lifetime = ServiceLifetime.Transient)]
public class ColorRepository : IColorRepository {}
var runtimeServices = new RuntimeRegisterServices(Assembly.GetExecutingAssembly(), services);
runtimeServices.ConfigureAllServices();See SnapshotIt.FluentValidation/README.md for details.
- Validate objects before posting or retrieving snapshots.
- Synchronous and asynchronous validation support.
- Comprehensive error handling.
Example:
var validator = new ProductValidator();
Snapshot.Out.Post(product, validator);
var validatedProduct = Snapshot.Out.Get<Product>(0, validator);NOT ALLOWED !
Happy coding! π