PocoTracking 1.0.0

dotnet add package PocoTracking --version 1.0.0
                    
NuGet\Install-Package PocoTracking -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PocoTracking" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PocoTracking" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="PocoTracking" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PocoTracking --version 1.0.0
                    
#r "nuget: PocoTracking, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package PocoTracking@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PocoTracking&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=PocoTracking&version=1.0.0
                    
Install as a Cake Tool

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:

  1. The object to create a proxy for
  2. A callback that will be called whenever a property of the proxy object is changed

The callback takes two parameters:

  1. The proxied object
  2. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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