Code Monkey home page Code Monkey logo

autoinc's Introduction

AutoInc

Multi-provider distributed unique Id generator for .NET. Its purpose is to generate sequence numbers for common business use-cases. For example: Product, Invoice or Registration numbers.

AutoInc.Neo4j

AutoInc.Neo4j is a Neo4j provider, allowing for unique Ids to be generated by the Neo4j graph database.

Getting Started

Using Nuget package manager to install the AutoInc.Neo4j package to your .NET project, or type the following from the package manager console.

Install-Package AutoInc.Neo4j

We'll need to connect to the Neo4j database. See Neo4j.Driver documentation for detailed connection instructions.

var driver = GraphDatabase.Driver("neo4j://localhost:7687");

AutoInc.Neo4j maintains sequence numbers inside dedicated nodes. One node is created for each sequence number (scope). By default, AutoInc.Neo4j uses the UniqueId label. This can be changed by setting the static Neo4jOptions.LabelName property.

Neo4jOptions.LabelName = "MySequenceNumber";

Initialising constraints

Before creating sequence numbers, we should create unique constraints on our sequence nodes to avoid duplicates. Due to the differences between Neo4j Community and Neo4j Enterprise, we need to perform an extra step if the latter is used.

Neo4jOptions.UseNeo4jEnterprise = true; // Only if Neo4j Enterprise is used

await driver.InitialiseUniqueIdsAsync();

Generating sequence numbers

A sequence number is generated by calling the NextUniqueIdAsync method and specifying the scope name.

var invoiceNumber = await driver.NextUniqueIdAsync("invoice");

Updating the next sequence number

Sometimes it is useful to set the next (or start) number for a sequence range. We can do this by calling the UpdateUniqueIdAsync method and specifying the scope name. When calling this method, it effectively sets the last sequence number. What this means is that when calling NextUniqueIdAsync it will return the last sequence number + 1.

await driver.UpdateUniqueIdAsync("invoice", 10);

Console.WriteLine(await driver.NextUniqueIdAsync("invoice"));
// Prints 11

Extension methods

AutoInc.Neo4j provides extension methods for NextUniqueIdAsync and UpdateUniqueIdAsync which hang off IDriver, IAsyncSession and IAsyncTransaction interfaces which all work the same way.

Transactions

Ideally, we would combine the sequence number generation along with the query which consumes the sequence number (the create query). To do this we wrap both operations inside a transaction. The advantage here is that if the transaction is rolled-back, the sequence number is also reset, so the number is not wasted.

var session = driver.AsyncSession();

await session.WriteTransactionAsync(async tx =>
{
    var id = await tx.NextUniqueIdAsync("invoice");
    
    var parameters = new { id };
    var query = "CREATE (invoice:Invoice {Id: $id})";
    await tx.RunAsync(query, parameters);
});

await session.CloseAsync();

Non-async methods

Neo4j.Driver supports non-async operations through its Neo4j.Driver.Simple package. This package exposes alternative interfaces ISession and ITransaction. AutoInc.Neo4j provides UpdateUniqueId and NextUniqueId extension methods which hang off these interfaces. These are identical to the async versions.

driver.UpdateUniqueId("invoice", 20);

Console.WriteLine(driver.NextUniqueId("invoice"));
// Prints 21

autoinc's People

Contributors

nad-au avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.