Skip to content

Commit

Permalink
Introduce C# driver without deployment (#609)
Browse files Browse the repository at this point in the history
## Usage and product changes

We introduce the C# driver for TypeDB. It is built using the
cross-platform [.NET 6
framework](https://dotnet.microsoft.com/en-us/download/dotnet/6.0).

**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#](https://www.swig.org/Doc4.2/SWIGDocumentation.html#CSharp)) 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 (more examples to copy and paste could be find in our docs and integration tests):
```
// 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, right now these tests are
messy).
Behaviour tests use Xunit.Gherkin.Quick framework and are implemented
using [partial
classes](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods)
for flexible construction of tests based on different sets of step
declaration files.

**Build:**
To fetch nuget packages, we use
[Paket](https://www.nuget.org/packages/Paket) and reference this
dependencies in a regular Bazel way ([example from the official Bazel
repo for C#
rules](https://github.com/bazelbuild/rules_dotnet/blob/master/paket.dependencies)).
  • Loading branch information
farost authored Mar 22, 2024
2 parents 52f1f68 + 02f2d0e commit a3041f1
Show file tree
Hide file tree
Showing 233 changed files with 21,637 additions and 263 deletions.
178 changes: 86 additions & 92 deletions .factory/automation.yml

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,7 @@ venv
# Cargo files
Cargo.lock
Cargo.toml

# Temporary paket files for C#
paket-files/
paket.lock
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ release_validate_deps(
"@vaticle_typeql",
],
tags = ["manual"], # in order for bazel test //... to not fail
version_file = ":VERSION",
)

# Force tools to be built during `build //...`
Expand Down
17 changes: 17 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ rules_antlr_dependencies(antlr_version, JAVA)
load("@vaticle_dependencies//builder/cpp:deps.bzl", cpp_deps = "deps")
cpp_deps()

# Load //builder/csharp
load("@vaticle_dependencies//builder/csharp:deps.bzl", dotnet_deps = "deps")
dotnet_deps()
load(
"@rules_dotnet//dotnet:repositories.bzl",
"dotnet_register_toolchains",
"rules_dotnet_dependencies",
)
rules_dotnet_dependencies()
dotnet_register_toolchains("dotnet", "6.0.413")
load("@rules_dotnet//dotnet:paket.rules_dotnet_nuget_packages.bzl", "rules_dotnet_nuget_packages")
rules_dotnet_nuget_packages()
load("@rules_dotnet//dotnet:paket.paket2bazel_dependencies.bzl", "paket2bazel_dependencies")
paket2bazel_dependencies()
load("//csharp/nuget:paket.csharp_deps.bzl", csharp_deps = "csharp_deps")
csharp_deps()

# Load //builder/proto_grpc
load("@vaticle_dependencies//builder/proto_grpc:deps.bzl", grpc_deps = "deps")
grpc_deps()
Expand Down
Loading

0 comments on commit a3041f1

Please sign in to comment.