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 packaging, distribution and documentation for the C# driver #623

Closed farost closed 2 months ago

farost commented 2 months ago

Usage and product changes

We introduce packaging, distribution and documentation C# driver for TypeDB (the original driver PR). It is built using the cross-platform .NET 6 framework.

Usage:

The driver is distributed as a series of Nuget packages. To use the driver, import the latest versions of the driver (TypeDB.Driver) and its Pinvoke runtime (TypeDB.Driver.Pinvoke) suitable for your platform.

CS project: Here is an example from a .csproj for MacOS x86-64:

<PackageReference Include="TypeDB.Driver" Version={VERSION} />
<PackageReference Include="TypeDB.Driver.Pinvoke.osx-x64" Version={VERSION} />

If you aim to build a platform-independent package, reference all the needed runtimes (it will affect the size of your application by downloading a respective set of platform-specific dynamic libraries):

<PackageReference Include="TypeDB.Driver.Pinvoke.osx-x64" Version={VERSION} />
<PackageReference Include="TypeDB.Driver.Pinvoke.linux-x64" Version={VERSION} />
<PackageReference Include="TypeDB.Driver.Pinvoke.win-x64" Version={VERSION} />
<PackageReference Include="TypeDB.Driver.Pinvoke.osx-arm64" Version={VERSION} />
...

Bazel:

  1. Import both the TypeDB.Driver and TypeDB.Driver.Pinvoke nuget packages as dependencies to your build rule.
  2. For development, you can also use a csharp bazel rule, passing targets //csharp:driver-csharp (the driver itself), //csharp/Api:api (exposed Api namespace), //csharp/Common:common (exposed Common namespace) as dependencies.

A simple usage example (see csharp/Test/Integration/Examples for more):

using TypeDB.Driver.Api;
using TypeDB.Driver.Common;

public class TypeDBExample
{
        public void SetupTypeDB()
        {
            string dbName = "access-management-db";
            string serverAddr = "127.0.0.1:1729";

            try
            {
                using (ITypeDBDriver driver = Drivers.CoreDriver(serverAddr))
                {
                    driver.Databases.Create(dbName);
                    IDatabase database = driver.Databases.Get(dbName);

                    using (ITypeDBSession session = driver.Session(dbName, SessionType.Schema))
                    {
                        using (ITypeDBTransaction transaction = session.Transaction(TransactionType.Write))
                        {
                            transaction.Query.Define("define person sub entity;").Resolve();

                            string longQuery = "define name sub attribute, value string; person owns name;";
                            transaction.Query.Define(longQuery).Resolve();

                            transaction.Commit();
                        }
                    }

                    database.Delete();
                }
            }
            catch (TypeDBDriverException e)
            {
                Console.WriteLine($"Caught TypeDB Driver Exception: {e}");
            }
        }
}

Implementation

Documentation: Documentation for C# is generated in a similar way as the C/C++ ones: doxygen -> adoc. This way, the C# parser has some extra logic of parsing the intermediate doxygen files but follows a similar (copypasted) structure. Additionally, some small bugs for both old parsers have been fixed.

Distribution: We decided to distribute the C# driver as a series of packages to allow the end user to choose between multi-platform and compactness. This way, we have: TypeDB.Driver.{X-X-X-version}.nupkg - the main package with the driver, which fails in runtime if no runtime is additionally provided. TypeDB.Driver.Pinvoke.{X-X-X-version}.{platform}.nupkg - platform-specific runtimes (osx-x64, osx-arm64, linux-x64, linux-arm64, win-x64).

The distribution is implemented via several bazel rules: nuget_pack (with a wrapper for native packages in this PR) and nuget_push and their respective calls for both runtime and main packages.

Tests: Integration tests use NUnit framework as a built-in framework for C# bazel test rules. This PR presents both test-core and test-cloud as small usage examples within the integration tests. These tests are also triggered by Factory.

There is also a new Deployment test, which creates a .csproj project and tries to fetch all the needed (and all the available runtime) packages to use them for another small C# program. This test is triggered by CircleCI.

vaticle-bot commented 2 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 2 months ago

There is a series of connected PRs:

  1. Bazel-distribution with fresh Bazel rules
  2. Dependencies with updated refs and todo cleanings
  3. TypeDB Driver with new Bazel rules usage, updated documentation, etc