Class StructurePatchingHelper
Iterate over a complex data structure. Unroll arrays and dictionaries; recurse class members. Only look at public Properties that can read/write. Used for Carrier data patching in WebApi
Namespace: SuperOffice.Util
Assembly: SoCore.dll
Syntax
public static class StructurePatchingHelper : Object
Remarks
This class will visit every element in the data structure it is given. Such a structure can be a simple value type, a string, a class, an array or a
dictionary of elements. Every element will be evaluated to see if its type matches one of those that are to be patched; if so, the supplied
action method is called and the value returned from that method is substituted into the original element.
This is a somewhat-specialized case of the Visitor pattern, with this class implementing the iterator over a data structure and
the dispatching based on element type.
If required, it can be extended to deal with other kinds of sub-elements - but note that the overwriteValue action
argument needs to be tailored to the replacement semantics of each kind of element. Replacing an element of an array is very
different from setting a property of a class.
Methods
ApplyActions(Object, Attribute[], String, Dictionary<Type, StructurePatchingHelper.TypeAction>, Action<Object>)
Iterate over a complex data structure. Unroll arrays and dictionaries; recurse class members. Only look at public Properties that can read/write. Used for Carrier data patching in WebApi
Declaration
public static void ApplyActions(object element, Attribute[] decorations, string name, Dictionary<Type, StructurePatchingHelper.TypeAction> actions, Action<object> overwriteValue)
Parameters
Type | Name | Description |
---|---|---|
Object | element | The root element, typically a Carrier object; but can be an array, a simple type, or anything |
Attribute[] | decorations | Custom attributes belonging to the element |
String | name | |
Dictionary<Type, StructurePatchingHelper.TypeAction> | actions | Types that should trigger actions, mapped to a function that takes a value object + decorations and returns an amended value. The trigger types should be simple types - not sequences or classes as these are the things that are traversed |
Action<Object> | overwriteValue | Client-supplied method that performs the actual overwrite of the original value, with a patched value |
Remarks
See unit tests and the SoCarrierDataPatching class for illustrations of usage
ApplyActions(Object, Dictionary<Type, StructurePatchingHelper.TypeAction>, Action<Object>)
Iterate over a complex data structure. Unroll arrays and dictionaries; recurse class members. Only look at public Properties that can read/write. Used for Carrier data patching in WebApi
Declaration
public static void ApplyActions(object root, Dictionary<Type, StructurePatchingHelper.TypeAction> actions, Action<object> overwriteValue)
Parameters
Type | Name | Description |
---|---|---|
Object | root | The root element, typically a Carrier object; but can be an array, a simple type, or anything |
Dictionary<Type, StructurePatchingHelper.TypeAction> | actions | Types that should trigger actions, mapped to a function that takes a value object + decorations and returns an amended value. The trigger types should be simple types - not sequences or classes as these are the things that are traversed |
Action<Object> | overwriteValue | Client-supplied method that performs the actual overwrite of the original value, with a patched value |
Remarks
See unit tests and the SoCarrierDataPatching class for illustrations of usage