C
C#13mo ago
SWEETPONY

How increment ulong correctly?

I'm sorry for this title I will describe everything rn. I had following DTO:
public class NotificationDto
{
[JsonProperty( PropertyName = "id" )]
[JsonRequired]
public Guid Id { get; set; }

[JsonProperty( PropertyName = "message" )]
[JsonRequired]
public MessageDto Message { get; set; }
}
public class NotificationDto
{
[JsonProperty( PropertyName = "id" )]
[JsonRequired]
public Guid Id { get; set; }

[JsonProperty( PropertyName = "message" )]
[JsonRequired]
public MessageDto Message { get; set; }
}
Then I use this dto:
private async Task OnMessagesSend(
IDeliveryHandlerContext context,
MessagesSendArguments arguments )
{
foreach (var message in arguments.Items)
{
switch (message.Behavior)
{
case BehaviorType.Simple:
foreach (var operatorName in message.To)
{
var endpoint = await _operatorEndpointCache.EndpointGet(operatorName);
await endpoint.EventEmit(new NotificationsReceivedEvent(
new NotificationDto
{
Id = Guid.NewGuid(),
Message = message
}));
}
break;
}
private async Task OnMessagesSend(
IDeliveryHandlerContext context,
MessagesSendArguments arguments )
{
foreach (var message in arguments.Items)
{
switch (message.Behavior)
{
case BehaviorType.Simple:
foreach (var operatorName in message.To)
{
var endpoint = await _operatorEndpointCache.EndpointGet(operatorName);
await endpoint.EventEmit(new NotificationsReceivedEvent(
new NotificationDto
{
Id = Guid.NewGuid(),
Message = message
}));
}
break;
}
but today we decided to change Guid to ulong. So now I can't just do Guid.NewGuid(). Maybe I should have smth like this:
public class NotificationDto
{
private static ulong _nextId = 1;

[JsonProperty(PropertyName = "id")]
[JsonRequired]
public ulong Id { get; set; }

[JsonProperty(PropertyName = "message")]
[JsonRequired]
public MessageDto Message { get; set; }

public NotificationDto()
{
Id = _nextId++;
}
}
public class NotificationDto
{
private static ulong _nextId = 1;

[JsonProperty(PropertyName = "id")]
[JsonRequired]
public ulong Id { get; set; }

[JsonProperty(PropertyName = "message")]
[JsonRequired]
public MessageDto Message { get; set; }

public NotificationDto()
{
Id = _nextId++;
}
}
or I should just smth like Interlocked.Increment? What is the best way to solve my problem?
2 Replies
wasabi
wasabi13mo ago
To use an external data store. Variables don't survive process restart.
Angius
Angius13mo ago
Leave the primary key generation to the database
Want results from more Discord servers?
Add your server