π Snapshot It 1.0.0. Before changing the state of component, just snapshot the state of object to make it easy to recovery.
- .NET 8
π Snapshot It
is extensionable lightweight-library, provides bunch of extensions.
- π Snapshotting - simply snapshot any instance in any context of application, and use it in runtime of application.
- ποΈ Copying - copy any instance.
- π Channel `BufferBlock` - use it as unbounded channel, post, read, close.
ποΈ We have a class Product
(?)
public class Product
{
public int Id {get;set}
public string Name{get;set;}
}
// Copy class
var product = new Product() { Id = 1,Name = "Product" } // note, it is reference type and mutable
var product2 = Snapshot.Out.Copy(product); // copies `product`
πΉ Let's do following steps....
using SnapshotIt;
var product = new Product()
{
Id = 1
Name = "Nike"
}
Snapshot.Out.Create<Product>(10); // initialized collection of `Product` with size `10`
Snapshot.Out.Post<Product>(product); // captures state
product.Id = 2;
product.Name = "Gucci";
const int index = 0;
// Gets the first snapshot from capture collection
// By default index = 0, using index easily can find the correct captured instance
var Capturedproduct = Snapshot.Out.Get<Product>(index);
// or use expressions
var Capturedproduct = Snapshot.Out.Get<Product>(o => o.Id == n);
// Logs: The product's name is Gucci
log.Information($"The product's name is {product.Name}");
// Logs: The previous product's name was Nike
log.Information($"The previous product's name was {Capturedproduct.Name}");
π ASP.NET and Dependency Injections
using SnapshotIt.DependencyInjection;
// Actually for injecting stuff into class, you are injecting from $constructor
public class UserController : BaseController
{
public ILogger logger;
public UserController(ILogger logger)
{
this.logger = logger;
}
}
// You can communicate with DI container very easy </>
// Use following steps ...
public class UserController : BaseController
{
public ILogger logger = Connector.GetService<ILogger>(); // you got it :)
}
β¨π¨ Registration of services to dependency injection container.
using SnapshotIt.DependencyInjection;
// Interface should be named same as class but with prefix `I` [!]
[RuntimeDependencyInjectionOption(Lifetime = ServiceLifetime.Scoped)]
public class ProductRepository : IProductRepository{}
[RuntimeDependencyInjectionOption(Lifetime = ServiceLifetime.Transient)]
public class ColorRepository : IColorRepository{}
var RuntimeServicesRegisterExecutor = new RuntimeRegisterServices(Assembly.GetExecutingAssembly(), services);
RuntimeServicesRegisterExecutor.ConfigureAllServices();