Mile
Mile
CC#
Created by Mile on 10/2/2024 in #help
Managing Scoped Service Lifecycles with a Singleton Factory in .NET: Best Practices?
public class ServiceResolver
{
private readonly IServiceProvider _serviceProvider;

public ServiceResolver(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

public IPaymentService ResolvePaymentService(IntegrationName integrationName)
{
return integrationName switch
{
IntegrationName.PayPal => _serviceProvider.GetRequiredService<PayPalService>(),
IntegrationName.Stripe => _serviceProvider.GetRequiredService<StripeService>(),
_ => throw new ArgumentException("Invalid payment service type.")
};
}
}
public class ServiceResolver
{
private readonly IServiceProvider _serviceProvider;

public ServiceResolver(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

public IPaymentService ResolvePaymentService(IntegrationName integrationName)
{
return integrationName switch
{
IntegrationName.PayPal => _serviceProvider.GetRequiredService<PayPalService>(),
IntegrationName.Stripe => _serviceProvider.GetRequiredService<StripeService>(),
_ => throw new ArgumentException("Invalid payment service type.")
};
}
}
66 replies
CC#
Created by Mile on 10/2/2024 in #help
Managing Scoped Service Lifecycles with a Singleton Factory in .NET: Best Practices?
and return reference to them using ServiceResolver
66 replies
CC#
Created by Mile on 10/2/2024 in #help
Managing Scoped Service Lifecycles with a Singleton Factory in .NET: Best Practices?
I'll register each payment provider service as singleton
66 replies
CC#
Created by Mile on 10/2/2024 in #help
Managing Scoped Service Lifecycles with a Singleton Factory in .NET: Best Practices?
parameters that I've passed within Initialize will be fetched from database on each request
66 replies
CC#
Created by Mile on 10/2/2024 in #help
Managing Scoped Service Lifecycles with a Singleton Factory in .NET: Best Practices?
yes
66 replies
CC#
Created by Mile on 10/2/2024 in #help
Managing Scoped Service Lifecycles with a Singleton Factory in .NET: Best Practices?
these parameters don't change, so once per application start, but payment service lifecycle is per request (scoped)
66 replies
CC#
Created by Mile on 10/2/2024 in #help
Managing Scoped Service Lifecycles with a Singleton Factory in .NET: Best Practices?
hm, I need to do it once, these parameters are store id, store key (for client)
66 replies
CC#
Created by Mile on 10/2/2024 in #help
Managing Scoped Service Lifecycles with a Singleton Factory in .NET: Best Practices?
I just need to pass initialization parameters within Initialize:
paymentService.Initialize(providerDataDto.CompanyMarketIntegrationKeyValues);
paymentService.Initialize(providerDataDto.CompanyMarketIntegrationKeyValues);
66 replies
CC#
Created by Mile on 10/2/2024 in #help
Managing Scoped Service Lifecycles with a Singleton Factory in .NET: Best Practices?
closer to 5-10
66 replies
CC#
Created by Mile on 10/2/2024 in #help
Managing Scoped Service Lifecycles with a Singleton Factory in .NET: Best Practices?
2 for now, but in the future it is going to be more
66 replies
CC#
Created by Mile on 10/2/2024 in #help
Managing Scoped Service Lifecycles with a Singleton Factory in .NET: Best Practices?
will try this
66 replies
CC#
Created by Mile on 10/2/2024 in #help
Managing Scoped Service Lifecycles with a Singleton Factory in .NET: Best Practices?
I tried registering Add<IPaymentService> and that didn't work
66 replies
CC#
Created by Mile on 10/2/2024 in #help
Managing Scoped Service Lifecycles with a Singleton Factory in .NET: Best Practices?
Yeah, that's the issue, I don't know which PaymentProvider Service I need to instantiate each call of method "Handle"
66 replies
CC#
Created by Mile on 10/2/2024 in #help
Managing Scoped Service Lifecycles with a Singleton Factory in .NET: Best Practices?
My question is: How can I ensure that the scope is properly disposed of after using the service in the Handle method? Is there a better way to implement this pattern that would better respect the Scoped lifecycle of the services? Any insights or best practices would be greatly appreciated.
66 replies
CC#
Created by Mile on 8/23/2024 in #help
Payments - architectural dilemma
do you have an idea on the best approach @canton7 ?
7 replies
CC#
Created by Mile on 8/23/2024 in #help
Payments - architectural dilemma
However I'm still not sure should I use strategy pattern, dependency injection or factory pattern for this, any additional suggestions? Additionaly, where do I store sensitive configuration information for each payment provider?
7 replies
CC#
Created by Mile on 8/23/2024 in #help
Payments - architectural dilemma
Encapsulating domain logic for each payment provider within seperate namespace seems as better approach
7 replies
CC#
Created by Mile on 8/23/2024 in #help
Payments - architectural dilemma
yeah, after giving it some more thought, separate solutions are definitely overkill at this point
7 replies