Skip to content

Provide Structurizer with an object graph and it efficiently provides key-values for it.

License

Notifications You must be signed in to change notification settings

danielwertheim/structurizer

Repository files navigation

Structurizer

NuGet License MIT Build Status

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
  • ...

About

Provide Structurizer with an object graph and it efficiently provides key-values for it.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages