PocoTracking 1.0.0
dotnet add package PocoTracking --version 1.0.0
NuGet\Install-Package PocoTracking -Version 1.0.0
<PackageReference Include="PocoTracking" Version="1.0.0" />
<PackageVersion Include="PocoTracking" Version="1.0.0" />
<PackageReference Include="PocoTracking" />
paket add PocoTracking --version 1.0.0
#r "nuget: PocoTracking, 1.0.0"
#:package PocoTracking@1.0.0
#addin nuget:?package=PocoTracking&version=1.0.0
#tool nuget:?package=PocoTracking&version=1.0.0
PocoTrackingProxyFactory
Lets say you have a class like this:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
You can create a proxy instance of this class like this:
var person = new Person
{
Name = "John",
Age = 30,
};
var proxy = PocoTrackingProxyFactory.CreateProxyInstance(person, (p, propertyName) => {
// This code will be called whenever a property of the proxy object is changed
Console.WriteLine($"Property {propertyName} changed to {p.GetType().GetProperty(propertyName).GetValue(p)}");
});
proxy.Name = "Jane"; // This will print "Property Name changed to Jane"
proxy.Age = 31; // This will print "Property Age changed to 31"
Console.WriteLine(person.Name); // This will print "Jane"
The CreateProxyInstance method takes two parameters:
- The object to create a proxy for
- A callback that will be called whenever a property of the proxy object is changed
The callback takes two parameters:
- The proxied object
- The name of the property that was changed
The proxy object is a wrapper around the given object that allows tracking of the object's properties. When a property of the proxy object is changed, the callback is called with the proxied object and the name of the property that was changed.
The proxy object can be used just like the original object, and changes to the proxy object will be reflected in the original object.
Remarks
This is a simple implementation of a tracking proxy factory for POCO objects. It can be useful for tracking changes to objects in a non-intrusive way.
CreateProxyInstance is implemented as an extension method for the object class, so it can be called on any object instance.
...
var proxy = person.CreateProxyInstance((p, propertyName) => {
Console.WriteLine($"Property {propertyName} changed to {p.GetType().GetProperty(propertyName).GetValue(p)}");
});
...
If the properties of the poco object are not virtual, the proxy object will not be able to track changes to the properties. One way to work around this is to use dynamix instead of var. This way the returned object will not be casted to the original object type and will ise its own properties.
dynamic proxy = person.CreateProxyInstance((p, propertyName) => {
Console.WriteLine($"Property {propertyName} changed to {p.GetType().GetProperty(propertyName).GetValue(p)}");
});
Once a Type as been proxied, it will be cached and reused for future calls to CreateProxyInstance. This means that the same proxy Type will be used for the same object type. Nevertheless, the callback will be different for each proxy instance.
PocoTrackingProxyFactory.CreateProxyType<T>
You can cache the proxy type and create instances of it later. This can be useful if you want to create multiple instances of the same proxy type.
var _ = PocoTrackingProxyFactory.CreateProxyType<Person>();
IGetProxied<T>
The proxy object implements the IGetProxied<T> interface, which allows you to get the proxied object from the proxy object.
var person = new Person
{
Name = "John",
Age = 30,
};
var proxy = person.CreateProxyInstance((p, propertyName) => {
Console.WriteLine($"Property {propertyName} changed to {p.GetType().GetProperty(propertyName).GetValue(p)}");
});
var proxiedPerson = (proxy as IGetProxied<Person>).GetProxied();
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 147 | 8/19/2024 |