Git Product home page Git Product logo

structurizer's Introduction

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

structurizer's People

Contributors

andycmaj avatar apetrut avatar danielwertheim avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.