Climbs on graph of objects (.net)
Master | Provider |
---|---|
Mono CI Provided by travis-ci | |
TeamCity CI Provided by CodeBetter | |
Windows CI Provided by AppVeyor |
For example, If you want to check the size of a object and his descendents in your .net heap - You can do it easily with graph climber.
Other examples :
- Writing serializers / deserializers without having to use Expressions or runtime code generation
- Doing something with all the objects that implements some interface within a graph of an object
You write a Processor
, a Processor
is an object that has a lot of ProcessMethod
s, they have a signature like this one :
[ProcessorMethod(Precedence = 102)]
public void ProcessReferenceType<T>(IWriteOnlyExactValueDescriptor<T> descriptor)
where T : class
There are many Descriptors
to choose from, and you can really go crazy with generic arguments. You can even implement IGenericParameterFilter
in an Attribute and decorate your generic parameter with it, Like this method here which accepts only primitive values :
[ProcessorMethod(Precedence = 99)]
public void ProcessPrimitives<[Primitive]T>(IReadOnlyValueDescriptor<T> descriptor)
Then you need to choose between the simple state member providers (or to create one of your own). The objective of the IStateMemberProvider
is to identify all the "State Members" that lies inside a given type. The graph climber climbs only on state members, Those can be fields, properties and even pair of get/set methods.
Note : A state member can be "read"/"write" only, but that's good as long as you want only read/write only access, When you'll try to read/write to those state members, no exception will be thrown from the default implementations of the StateMemberProviders
. You may throw it from the StateMember
(if you created it).
var stateMemberProvider = new CachingStateMemberProvider(new PropertiesStateMemberProvider());
var gc = new GraphClimber<MyProcessor>(stateMemberProvider);
After you've done those, you ready to climb on objects :
var myProcessorInstance = new MyProcessor();
var myObject = GetComplexObject();
gc.Climb(myObject, myProcessorInstance);
What's the graph climber is going to do now?
- Look for the climbed object type
- Tell the state member provider to give all the state members that the given type has
- Look for the most appropriate method (If exists) inside "MyProcessor" to call for every state member and call it.
Actually, It's not going to do steps 2 and 3 all the times, only in the first time it encounters a new type of object, Because it generates an implementation of a method that climbs on that type runtime and than it caches it. Basicly that means "One time reflection", It's fast!
Q : How can I help? A : You can help us by starting issues, writing code, documenting code, writing tests, and so. Pull Requests are awesome.
Q : Is this going to be better documented? A : Yeah, I Promise.
Q : How can I climb recursively? A : Call climb() on the given descriptors, they will continue to climb on the current object that found on the state member, And remember, don't climb on nulls!
Q : Can I climb on arrays? A : Sure you can, In that case the name of the statemember is going to be formatted with the array indices
Q : Can I visit Enums?
A : Hell yeah, Get IReadOnlyEnumExactValueDescriptor
as an argument in your processor method
- Writing full API
- Implementing SlowGraphClimber with Examples (Current State)
- Implementing GraphClimber with full tests