Provide Structurizer with an object graph and it efficiently provides key-values for it.
Clone or download
danielwertheim Changes build script, test logger
Trying to get tests to be reported in TeamCity again.
Latest commit 4ed4862 Nov 27, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tools .Net Standard 2.0 Nov 16, 2018
.gitignore
LICENSE.txt
README.md For v3.1.0 Jun 3, 2017
ReleaseNotes.md
build.cake
build.ps1
buildconfig.cake

README.md

Structurizer

Structurizer is extracted from one of my other projects PineCone, which was used for much of the underlying indexing stuff for another project of mine, SisoDB. Structurizer is reduced to only manage a key-value representation of an object-graph.

Release notes

Release notes are kept here.

Usage

Define a model

You need some model to create key-values for. It will only extract public properties.

public class MyRoot
{
    public string Name { get; set; }
    public int Score { get; set; }
    public MyChild OneChild { get; set; }
    public List<MyChild> ManyChildren { get; set; }
}

public class MyChild
{
    public string SomeString { get; set; }
}

Install

install-package structurizer

Builder construction

The easiest builder to use is the FlexibleStructureBuilder (introduced in v3.0.0).

var builder = new FlexibleStructureBuilder();

You can also use the more static configured StructureBuilder:

var typeConfigs = new StructureTypeConfigurations();
typeConfigs.Register<MyRoot>();

var builder = StructureBuilder.Create(typeConfigs);

Create key-values

Now you can create a structure which will hold key-value StructureIndex items for the graph.

var item = new MyRoot
{
    Name = "Foo Bar",
    Score = 2345,
    OneChild = new MyChild
    {
        SomeString = "One child"
    },
    ManyChildren = new List<MyChild>
    {
        new MyChild {SomeString = "List Child1"},
        new MyChild {SomeString = "List Child2"}
    }
};

var structure = builder.CreateStructure(item);
foreach (var index in structure.Indexes)
{
    Console.WriteLine($"{index.Path}={index.Value}");
}

will generate:

Name=Foo Bar
Score=2345
OneChild.SomeString=One child
ManyChildren[0].SomeString=List Child1
ManyChildren[1].SomeString=List Child2

Control what's being indexed

Using the FlexibleStructureBuilder

At any point (last in wins), just use the Configure methods, e.g.

builder.Configure(i => cfg
    .UseIndexMode(IndexMode.Inclusive)
    .Members(e => e.Name, e => e.Score));

Using the Static StructureBuilder

This is controlled using the StructureTypeConfigurations.Register member.

By default it's going to index everything as the default is to have IndexMode.Exclusive with no exclusions. This can be changed.

var typeConfigs = new StructureTypeConfigurations();
typeConfigs.Register<MyRoot>(cfg => cfg
    .UseIndexMode(IndexMode.Inclusive)
    .Members(t => t.Name, t => t.Score)
    //Use array access to define path of childrens in enumerable
    .Members(t => t.ManyChildren[0].SomeString))
    //Can also be done using strings if no index property exists
    .Members("ManyChildren.SomeString");

Use cases?

Anywhere where you efficiently need to extract key-values from an object-graph. Could e.g. be for:

  • Logging
  • Selectively extracting some property values for generating a checksum for an object-graph
  • Comparing values
  • Storing key-values for searching
  • ...