Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snowflake - Add Producer Constructor #33

Open
Foxtrek64 opened this issue Nov 13, 2024 · 1 comment
Open

Snowflake - Add Producer Constructor #33

Foxtrek64 opened this issue Nov 13, 2024 · 1 comment

Comments

@Foxtrek64
Copy link
Contributor

Foxtrek64 commented Nov 13, 2024

Description
This change should add a constructor to the Snowflake type intended for use by servers, or more accurately, producers of new Snowflake values.

API Proposal

public Snowflake(DateTimeOffset timeStamp, byte internalWorkerId, byte internalProcessId, ushort increment, ulong epoch = 0);
public Snowflake(DateTimeOffset timeStamp, byte internalWorkerId, byte internalProcessId, ushort increment, DateTimeOffset? epoch = null);

These methods likely would not be able to forward to the existing Snowflake(ulong, ulong) ctor due to the need for a method body to perform multiple mathematical operations.

Why This is Needed
Currently, the Snowflake type is intended for use solely by consumers, e.g. those who are sent Snowflake values over an API or who store them in a database. This is not suitable for producers of this type, who would currently need to produce the Snowflake value themselves as a ulong, then pass that to the Snowflake(ulong, ulong) ctor.

@Foxtrek64 Foxtrek64 changed the title Snowflake - Add Server Constructor Snowflake - Add Producer Constructor Nov 13, 2024
@Foxtrek64
Copy link
Contributor Author

Foxtrek64 commented Nov 13, 2024

Example Use:

ushort increment = 0;
public User CreateUser(string emailAddress, string firstName, string lastName)
{
    var userId = new Snowflake(DateTimeOffset.UtcNow(), _context.WorkerId; System.Environment.CurrentManagedThreadId, increment++, Constants.MyServiceEpoch);
    
    var user = new User(userId, emailAddress, firstName, lastName);
    
    // Todo: write user to database
    return user;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant