Developers

.NET connector

A generated NuGet package with typed interfaces and a LINQ provider for your schema.

The database can generate a NuGet package for its schema: an interface for each perspective (ICustomer), an OmniContext, and a LINQ provider that turns your queries into OmniCore's query language. The package contains only what the person who downloads it can read.

Getting the package

powershell
Invoke-WebRequest "https://omnicore.example.com/api/connector/package?packageId=Contoso.Crm&version=1.0.0&namespace=Contoso.Crm" `
  -Headers @{ Authorization = "Bearer $token" } -OutFile Contoso.Crm.1.0.0.nupkg
ParameterDefault
packageIdOmniCore.Client
version1.0.0
namespace
contextName

The same schema and options give identical bytes. Bump version whenever the schema changes. GET /api/connector/source returns the generated C# for review, and OmniCore Admin's Connector package tab builds and saves the package for you.

The runtime part, OmniCore.Database.Connector, is also on nuget.org for shared code and hand-written interfaces.

Connecting

csharp
using Contoso.Crm;
using OmniCore.Connector;

var http = new HttpClient { BaseAddress = new Uri("https://omnicore.example.com/") };
var db = new OmniContext(http, new OmniClientOptions
{
    AccessTokenProvider = async ct => await myTokenSource.GetAccessTokenAsync(ct),
});

AccessTokenProvider is called for every request, so you can return a cached or refreshed token.

Creating

csharp
var ann = db.Create<ICustomer>();
ann.AccountNumber = "C-1001";
ann.FirstName = "Ann";
ann.CreditLimit = 5000m;
await db.InsertAsync(ann);          // ann.Id and ann.Version are now set

Querying with LINQ

csharp
var bigCustomers = await db.Query<ICustomer>()
    .Where(c => c.CreditLimit > 1000 && c.LastName != null)
    .OrderByDescending(c => c.CreditLimit).ThenBy(c => c.LastName)
    .Skip(20).Take(20)
    .ToListAsync();

That sends:

GET /api/entities?$impl=Customer&$where=creditLimit > 1000 & lastName != null&$order=creditLimit desc,lastName asc&$skip=20&$take=20

Query<T>() always adds $impl for T. ToQueryString() shows exactly what will be sent.

Several perspectives on one entity

csharp
// Customers who are also staff
var staffCustomers = await db.Query<ICustomer>().Where(c => c.Implements<IStaff>()).ToListAsync();

// The same object, as another perspective. Shared fields are the same value.
IContact contact = ann.As<IContact>();
contact.Email = "ann@example.com";

// Add a perspective (saved with the next SaveAsync), or remove one
IStaff staff = ann.Implement<IStaff>();
staff.EmployeeId = "E-042";
ann.RemoveInterface<ISupplier>();

Saving, reading and deleting

csharp
ann.CreditLimit = 6000m;
await db.SaveAsync(ann);       // PATCH with only the changed fields, If-Match: "<version>"; nothing sent if unchanged

ICustomer? again = await db.GetAsync<ICustomer>(ann.Id);   // null if missing, hidden or not a Customer
await db.DeleteAsync(ann);     // guarded by its version

Entities track their own changes, so SaveAsync sends only what changed, with the version read. If someone else saved first, it fails rather than overwriting their change.