https://github.com/dario-l/compositekeygen
Allows to create composite key encoded as Int64 (long).
https://github.com/dario-l/compositekeygen
asp-net-core composite generator identifier multitenancy multitenant netcore snowflake snowflake-net-core
Last synced: 10 months ago
JSON representation
Allows to create composite key encoded as Int64 (long).
- Host: GitHub
- URL: https://github.com/dario-l/compositekeygen
- Owner: dario-l
- License: mit
- Created: 2019-12-05T15:32:58.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-11-09T09:30:10.000Z (over 3 years ago)
- Last Synced: 2025-08-19T06:39:45.549Z (10 months ago)
- Topics: asp-net-core, composite, generator, identifier, multitenancy, multitenant, netcore, snowflake, snowflake-net-core
- Language: C#
- Size: 22.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CompositeKeyGen

Allows to create composite key encoded as Int64 (long).
*Usually you should use these identifiers in domain objects (aggregates).*
Each key is composed from 3 elements:
- instanceId - could be server identifier
- tenantId - could be tenant identifier
- sequenceId - incrementing identifier from other source
## Configuration
This should be done once at application startup:
``` c#
// Define configuration
var config = new MaskConfig(instanceBits: 5, tenantBits: 14);
// Create key generator
var serverId = _application.Configuration.GetServerId(); // this is your code :)
var generator = new KeyGenerator(serverId, config);
```
Configuration defined above will give you the following maximum values:
```
Max InstanceId: 31
Max TenantId: 16383
Max SequenceId: 17592186044415
```
For given configuration max sequenceId will be 8192 times bigger than int.MaxValue.
## Usage
When you need to create key:
``` c#
// Create key providing tenantId and sequenceId
var sequenceId = _hiloGenerator.GetNext(); // this is your code :)
var id = generator.Create(tenantId, sequenceId);
```
*Instead of HiLo you can use [LinearKeyAllocator](https://github.com/dario-l/LinearKeyAllocator).*
Output for example data:
```
For:
instanceId = 1;
tenantId = 10;
sequenceId = 100;
Created id will be:
long id = 288406298012156004
```
When you need to verify key for given tenantId you can reconstruct provided identifier:
``` c#
CompositeKey reconstructed = generator.Reconstruct(id);
if (reconstructed.TenantId != _userContext.TenantId) throw...
```