C
C#2y ago
Hulkstance

❔ Lazy initialization question

Lazy initialization is usually used whereby you only load or initialize an object when you first need it. The question is how do I know if I need it here: _jetStreamFactory = new Lazy<IJetStream>(() => _connection.CreateJetStreamContext());?
public sealed class NatsPublisher : IPublisher
{
private readonly ProducerConfiguration _configuration;
private readonly IJetStream _jetStream;

public NatsPublisher(IOptions<ProducerConfiguration> options, IConnection connection)
{
_configuration = options.Value;

JetStreamUtils.CreateStreamOrUpdateSubjects(connection, _configuration.Stream, _configuration.Subject);
_jetStream = connection.CreateJetStreamContext();
}

public async ValueTask<PublishAck> Publish<T>(T payload)
where T : class
{
var data = JsonSerializer.SerializeToUtf8Bytes(payload);

var msg = new Msg(_configuration.Subject, null, null, data);

return await _jetStream.PublishAsync(msg);
}

public async ValueTask<PublishAck> PublishAsync(byte[] payload)
{
var msg = new Msg(_configuration.Subject, null, null, payload);
return await _jetStream.PublishAsync(msg);
}

public async ValueTask<PublishAck> PublishAsync(byte[] payload, IEnumerable<(string, string)> headers)
{
var msg = new Msg(_configuration.Subject, null, null, payload);

foreach (var (header, val) in headers)
{
msg.Header[header] = val;
}

return await _jetStream.PublishAsync(msg);
}

public async ValueTask<PublishAck> PublishWithDeduplicationIdAsync(byte[] payload, string id)
{
var msg = new Msg(_configuration.Subject, null, null, payload)
{
Header = { ["Nats-Msg-Id"] = id }
};

return await _jetStream.PublishAsync(msg);
}
}
public sealed class NatsPublisher : IPublisher
{
private readonly ProducerConfiguration _configuration;
private readonly IJetStream _jetStream;

public NatsPublisher(IOptions<ProducerConfiguration> options, IConnection connection)
{
_configuration = options.Value;

JetStreamUtils.CreateStreamOrUpdateSubjects(connection, _configuration.Stream, _configuration.Subject);
_jetStream = connection.CreateJetStreamContext();
}

public async ValueTask<PublishAck> Publish<T>(T payload)
where T : class
{
var data = JsonSerializer.SerializeToUtf8Bytes(payload);

var msg = new Msg(_configuration.Subject, null, null, data);

return await _jetStream.PublishAsync(msg);
}

public async ValueTask<PublishAck> PublishAsync(byte[] payload)
{
var msg = new Msg(_configuration.Subject, null, null, payload);
return await _jetStream.PublishAsync(msg);
}

public async ValueTask<PublishAck> PublishAsync(byte[] payload, IEnumerable<(string, string)> headers)
{
var msg = new Msg(_configuration.Subject, null, null, payload);

foreach (var (header, val) in headers)
{
msg.Header[header] = val;
}

return await _jetStream.PublishAsync(msg);
}

public async ValueTask<PublishAck> PublishWithDeduplicationIdAsync(byte[] payload, string id)
{
var msg = new Msg(_configuration.Subject, null, null, payload)
{
Header = { ["Nats-Msg-Id"] = id }
};

return await _jetStream.PublishAsync(msg);
}
}
1 Reply
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server