I am doing a similar service like “AggregateRepository” on this site (but I use gRPC instead of TCP):
The service use EventStoreClient, load events from EventStore and save events to EventStore.
I wonder which Microsoft dependency injection lifetime I should use. EventStoreClient is registered as Singleton so I thought the service should also be registered as Singleton.
But on the website above “AggregateRepository” is registered as Transient. So now I don’t know.
Here are Microsoft dependency injection lifetimes:
Transient lifetime services are created each time they are requested. This lifetime works best for lightweight, stateless services.
Scoped lifetime services are created once per request.
Singleton lifetime services are created the first time they are requested (or when ConfigureServices is run if you specify an instance there) and then every subsequent request will use the same instance.
Which should I choose?