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

Shift tenant to aggregateId part of streamId #10

Merged

Conversation

bartelink
Copy link
Contributor

While the exact layout of a streamId is pretty arbitrary, in general it works better to have the tenant to the right of the '-' in the streamId as the default category projection treats anything to the left of the first '-' as the category (while this is configurable, this is the default, so Pit Of Success principles apply)

@bartelink bartelink force-pushed the bartelink/fix-streamname-ordering branch from 5876410 to fabcf77 Compare October 14, 2021 15:34
@bartelink
Copy link
Contributor Author

bartelink commented Oct 14, 2021

(Not adding the following goldplated validation logic for now, (which I have implemented and used with success elsewhere) in the interest of not drowning the reader in trivia)

// Generates a stream id in the canonical `{category}-{aggregateId}` format
public static string ToStreamId(Type streamType, object aggregateId, object? tenantId = null)
{
    var tenantPrefix = tenantId == null ? $"{tenantId}_"  : "";

    if (tenantId != null)
        CheckNoEmbeddedDashes(nameof(tenantId), tenantId.ToString()!);
    var category = ToStreamPrefix(streamType);
    CheckNoEmbeddedDashes(nameof(streamType), category);

    // (Out-of-the box, the category projection treats anything before a `-` separator as the category name)
    // For this reason, we place the "{tenantId}_" bit (if present) on the right hand side of the '-'
    return $"{category}-{tenantPrefix}{aggregateId}";

    static void CheckNoEmbeddedDashes(string argName, string value)
    {
        if (value.IndexOf('-') != -1)
            throw new ArgumentException("Stream name components may not include embedded '-' characters.", argName);
    }
}

Copy link
Contributor

@oskardudycz oskardudycz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, sorry for taking that long to approve it. Thank you for contribution!

Regarding validation, let's keep it simple for now :)

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

Successfully merging this pull request may close these issues.

2 participants