vaticle / typedb-driver

TypeDB Drivers for Rust, Python, Java, Node.js, C, C++, and C#.
https://typedb.com
Apache License 2.0
30 stars 32 forks source link

Introduce C# driver without documentation and deployment #616

Closed farost closed 3 months ago

farost commented 3 months ago

Usage and product changes

We introduce the C# driver for TypeDB. It is built using the cross-platform .NET 6 framework.

Usage: Deployment and usage examples will be provided in a separate pull request. Current state of the code lets you compiling the driver + writing and running behaviour and integration tests for it. The driver is expected to be a Nuget package, which could be added as a dependency to a project and referenced via "using" statements inside the users' code for all platforms.

Architecture: The C# driver is a thin wrapper around the TypeDB Rust driver, introducing classes for a more intuitive interface. Mostly each C# object holds a reference to the corresponding native Rust object, using an FFI (SWIG for C#) for the native object wrappers generation and resource management.

Any error encountered will throw a TypeDBDriverException. Note that methods which return an IEnumerable or a Promise and encounter a server-side error will only throw when the return objects are evaluated (e.g. iterate over or call a Linq method for an IEnumerable and call Resolve() for a Promise).

A simple usage example:

// Inside a try-catch block
using (ITypeDBDriver driver = TypeDB.CoreDriver(TypeDB.DEFAULT_ADDRESS))
{
    string dbName = "mydb";
    driver.Databases.Create(dbName);
    IDatabase mydb = driver.Databases.Get(dbName);
    System.Console.WriteLine(mydb.Name);

    using (ITypeDBSession schemaSession = driver.Session(dbName, SessionType.SCHEMA))
    {
        using (ITypeDBTransaction writeTransaction = schemaSession.Transaction(TransactionType.WRITE))
        {
            string defineQuery = "...some define query...";
            writeTransaction.Query.Define(defineQuery).Resolve();
            writeTransaction.Commit();
        }
    }

    mydb.Delete();
}

Implementation

Driver: Introduces the C# driver, wrapping the C-native classes produced by Rust driver using SWIG. The driver aims to provide a familiar interface for C# developers, hiding data transformations and native calls under the hood. The code's architecture and build flow follow the one from Java with small C#-specific alterations.

Tests: Integration tests use NUnit framework as a built-in framework for C# bazel test rules (more to come in another PR). Behaviour tests use Xunit.Gherkin.Quick framework and are implemented using partial classes for flexible construction of tests based on different sets of step declaration files.

Build: To fetch nuget packages, we use Paket and reference this dependencies in a regular Bazel way (example from the official Bazel repo for C# rules).

vaticle-bot commented 3 months ago

PR Review Checklist

Do not edit the content of this comment. The PR reviewer should simply update this comment by ticking each review item below, as they get completed.


Trivial Change

Code

Architecture

farost commented 3 months ago

The original PR is here. The current PR tries to remerge the changes by squashing.

flyingsilverfin commented 3 months ago

Re-merged after previous merge didn't do a squash-merge :)