Skip to content

Conversation

@moeen
Copy link

@moeen moeen commented Jul 7, 2025

This pull request introduces new composite lock methods for various lock types via code generation.

Auto Generated Codes

  • src/DistributedLock.Core/DistributedLockProviderExtensions.cs
  • src/DistributedLock.Core/DistributedReaderWriterLockProviderExtensions.cs
  • src/DistributedLock.Core/DistributedSemaphoreProviderExtensions.cs
  • src/DistributedLock.Core/DistributedUpgradeableReaderWriterLockProviderExtensions.cs

Copy link
Owner

@madelson madelson left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution @moeen ! This is off to a great start!

/// </summary>
{{interfaceName}} {{createMethodName}}(string name{{(interfaceName.Contains("Semaphore") ? ", int maxCount" : string.Empty)}});
}
""";
Copy link
Owner

Choose a reason for hiding this comment

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

I love the use of """ strings here, but let's indent less:

var providerInterfaceCode = $$"""
    ...
    """;

Please address throughout.

Copy link
Author

Choose a reason for hiding this comment

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

The generated code looks well indented. The raw string literals use aligned indentation for readability in the generator, the compiler strips the common leading whitespace anyway.
Happy to adjust if you prefer less indentation!

@wuzxc1230123
Copy link

Is there a better alternative to this PR?

@moeen
Copy link
Author

moeen commented Dec 1, 2025

@madelson

Hi! Sorry for the delay - I was busy and totally forgot about this. I've now resolved all the comments and added the tests as discussed. The PR should be ready for another review. Thanks for your patience!

Sample use with the Redis Provider:

var connection = await ConnectionMultiplexer.ConnectAsync("localhost:6379");
var provider = new RedisDistributedSynchronizationProvider(connection.GetDatabase());

const string FromAccount = "account:alice";
const string ToAccount = "account:bob";
const decimal Amount = 100m;

var accounts = new[] { FromAccount, ToAccount };

await using (var handle = await provider.TryAcquireAllLocksAsync(accounts, TimeSpan.FromSeconds(30)))
{
    if (handle != null)
    {
        Console.WriteLine($"Transferring ${Amount} from {FromAccount} to {ToAccount}");
        await Task.Delay(1000);
        Console.WriteLine("Transfer completed successfully");
    }
    else
    {
        Console.WriteLine("Failed to acquire locks - transfer aborted");
    }
}

await connection.CloseAsync();

@wuzxc1230123
Copy link

@moeen Dear Boss, could you please merge the code and provide the nget feature? I really need this functionality—thank you very much!

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.

3 participants