Neo4jClient.ReturnPoly 1.0.6

.NET Standard 2.0
dotnet add package Neo4jClient.ReturnPoly --version 1.0.6
NuGet\Install-Package Neo4jClient.ReturnPoly -Version 1.0.6
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Neo4jClient.ReturnPoly" Version="1.0.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Neo4jClient.ReturnPoly --version 1.0.6
#r "nuget: Neo4jClient.ReturnPoly, 1.0.6"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Neo4jClient.ReturnPoly as a Cake Addin
#addin nuget:?package=Neo4jClient.ReturnPoly&version=1.0.6

// Install Neo4jClient.ReturnPoly as a Cake Tool
#tool nuget:?package=Neo4jClient.ReturnPoly&version=1.0.6

Neo4jClient.ReturnPoly

A polymorphic return function for Neo4jClient

This library adds two extension methods, and some JsonConverters that allow you to return polymorphic entities from a Neo4j database.

Available on NuGet! NuGet version (Neo4jClient.ReturnPoly)

Install-Package Neo4jClient.ReturnPoly

Getting Values

There are three primary snippets you can use to retrieve the polymorphic values from the graph

// Your standard way of doing things:
await graph.Cypher
            .Match("(n)")
            .ReturnPolymorphic<BaseType>("n")
            .ResultsAsync;

// Your standard way of doing things, but distinctly:
await graph.Cypher
            .Match("(n)")
            .ReturnDistinctPolymorphic<BaseType>("n")
            .ResultsAsync;

// The anonymous object/built return object. This method is a little more clunky, but
// there's not much to be done since the core Neo4jClient expression parser handles
// all of this
await graph.Cypher
            .Match("(n)")
            .Return(() => new {
                MyVariable = Return.As<BaseType>(PolyUtils.Poly("n"))
            })
            .ResultsAsync;

Setting up the converters

Label Converter

/* Database

	(:TypeA { PropertyA: "PropA" })
	(:TypeA:TypeB { PropertyA: "PropA", PropertyB: "PropB" })
	(:TypeB { PropertyA: "PropA", PropertyB: "PropB" })

*/

public class TypeA
{
    public string PropertyA { get; set; }
}

public class TypeB : TypeA
{
    public string PropertyB { get; set; }
}

class Program
{
    static async Task Main(string[] args)
    {
        var client = new BoltGraphClient("bolt://localhost:7687");
        await client.ConnectAsync();

        client.JsonConverters.Add(new PolymorphicJsonLabelConverter<TypeA>());

        var results = await client.Cypher
            .Match("(n)")
            .ReturnPolymorphic<TypeA>("n")
            .ResultsAsync;

        foreach (var result in results)
            Print(result);
    }
    
    static void Print(TypeA type)
    {
        Console.WriteLine(type.GetType().Name);
        Console.WriteLine("  A: " + type.PropertyA);
        if (type is TypeB b)
            Console.WriteLine("  B: " + b.PropertyB);
    }
}
Available Functions

In order to use labels to determine the proper type, you must use one of these two return functions.

ICypherFluentQuery<T> ReturnPolymorphic<T>(this ICypherFluentQuery, string)
ICypherFluentQuery<T> ReturnDistinctPolymorphic<T>(this ICypherFluentQuery, string)

Or if using a built return object, the PolyUtils.Poly(string) method can be used to create the return text. It must be used in conjunction with Return.As<T> like so:

Return.As<T>(PolyUtils.Poly(string))

If you use it a log, add using static Neo4jClient.ReturnPoly.PolyUtils; to your imports, and then its just

Return.As<T>(Poly(string))

And doesn't that just read a little better? (working with what I've got here, as I can't hook into the core expression parsing (yet))

Lambda Converter

/* Database

	Labels dont matter here
	({ PropertyA: "PropA" })
	({ PropertyA: "PropA", PropertyB: "PropB" })
	({ PropertyA: "PropA", PropertyB: "PropB" })

*/

public class TypeA
{
    public string PropertyA { get; set; }
}

public class TypeB : TypeA
{
    public string PropertyB { get; set; }
}

class Program
{
    static async Task Main(string[] args)
    {
        var client = new BoltGraphClient("bolt://localhost:7687");
        await client.ConnectAsync();

        // Since this converter doesn't use labels, you can use the regular .Return<T> functions
        // from Neo4jClient
        client.JsonConverters.Add(new PolymorphicJsonLambdaConverter<TypeA>(jo => {
        	if (jo.ContainsKey("PropertyB"))
                return typeof(TypeB);
            return typeof(TypeA);
        }));

        var results = await client.Cypher
            .Match("(n)")
            .Return<TypeA>("n")
            .ResultsAsync;
        
        foreach (var result in results)
            Print(result);
    }
    
    static void Print(TypeA type)
    {
        Console.WriteLine(type.GetType().Name);
        Console.WriteLine("  A: " + type.PropertyA);
        if (type is TypeB b)
            Console.WriteLine("  B: " + b.PropertyB);
    }
}

There are no extension methods with this style. Just use Return like you normally would

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.6 283 7/30/2021
1.0.5 252 5/28/2021
1.0.4 368 5/22/2021
1.0.3 223 4/6/2021
1.0.2 313 11/10/2020
1.0.1 305 11/10/2020