RCommon
HomeGitHub
  • Introduction
  • Common Scenarios
  • Getting Started
    • Running Examples
    • Roadmap
    • Releases
      • 1.0.1.75
      • 1.0.2.0
      • 2.0.0
      • 2.1.0
  • Topics
    • Fundamentals
      • Configuration
      • Logging
      • GUID Generation
      • Time and Date
      • Emailing
        • SMTP Email
        • SendGrid Email API
      • Data Transfer Objects
        • Pagination
      • Security
        • Current User
        • Claims
      • Events
        • Transactional Events
        • Synchronous Events
        • Asynchronous Events
        • Producers
        • Subscribers
      • Validation
        • Fluent Validation
      • Caching
        • Dynamically Compiled Expressions
        • Persistence Caching
        • Caching Services
        • Redis & Valkey
        • Memory Cache
      • Serialization
        • JSON.NET
        • System.Text.Json
    • Patterns
      • Specification
      • Mediator
        • MediatR
          • Validator Behavior
          • Unit of Work Behavior
          • Logging Behavior
      • CQRS
        • Commands
        • Queries
      • Persistence
        • Repository
          • Entity Framework Core
          • Dapper
          • Linq2Db
        • Transactions
          • Unit of Work
      • Event Bus
        • In Memory
        • MediatR
        • Wolverine
      • Message Bus
        • MassTransit
        • Wolverine
    • Architecture
      • Overview
      • Microservices
      • Clean Architecture
      • Event Driven Architecture
  • Examples
    • Clean Architecture
    • CQRS
    • Mediator: MediatR
    • Event Handling: In Memory
    • Event Handling: MediatR
    • Event Handling: MassTransit
    • Event Handling: Wolverine
    • Validation: Fluent Validation
Powered by GitBook
On this page
  • Sequential Guid Generator
  • Simple Guid Generator
  1. Topics
  2. Fundamentals

GUID Generation

Guid generators in RCommon

The use of Guids in application is ubiquitous in most modern applications. In persistence, Guids are neccesary for building enterprise applications at scale to more easily allow for sharding. In general, there are a more globally unique identifier that can be used in a large variety of circumstances.

Sequential Guid Generator

Sequential Guid's offer the implementor a means of tracking where a guid may have been generated from. In clustered applications, your objects may get persisted into a common data store where you can then infer which node may be persisting that information based on the initial sequence in your Guid. Not that they are a good replacement for logging, but using sequential Guid's helps engineers with identifying patterns based on where the Guid's may be generated from.

Configuration

// Configuration
builder.Services.AddRCommon()
    .WithSequentialGuidGenerator(guid => 
        guid.DefaultSequentialGuidType = SequentialGuidType.SequentialAsString);

Implementation

 public class CreateLighthouseProfileCommandHandler : IAppRequestHandler<CreateLighthouseProfileCommand, LighthouseProfileResponse>
 {
     private readonly IGuidGenerator _guidGenerator;

     public CreateLighthouseProfileCommandHandler(IGuidGenerator guidGenerator)
     {
         _guidGenerator = guidGenerator;
     }
     public async Task<LighthouseProfileResponse> HandleAsync(CreateLighthouseProfileCommand request, CancellationToken cancellationToken)
     {
         LighthouseProfile profile = new LighthouseProfile(_guidGenerator.Create(), request.WebsiteUrl, request.CreatedByEmail);

Simple Guid Generator

A simple means of generating a Guid synonomous with running Guid.NewGuid()

Configuration

// Configuration
builder.Services.AddRCommon()
    .WithSimpleGuidGenerator();

Implementation

public class CreateLighthouseProfileCommandHandler : IAppRequestHandler<CreateLighthouseProfileCommand, LighthouseProfileResponse>
 {
     private readonly IGuidGenerator _guidGenerator;

     public CreateLighthouseProfileCommandHandler(IGuidGenerator guidGenerator)
     {
         _guidGenerator = guidGenerator;
     }
     public async Task<LighthouseProfileResponse> HandleAsync(CreateLighthouseProfileCommand request, CancellationToken cancellationToken)
     {
         LighthouseProfile profile = new LighthouseProfile(_guidGenerator.Create(), request.WebsiteUrl, request.CreatedByEmail);
PreviousLoggingNextTime and Date

Last updated 1 year ago