I am working on email service which can send emails using IMAP, SMTP or third-party software (like SendGrid).
And I need to determine which service will handle the process of sending at runtime, because the user will specify which provider will be used (in the request there is a property called ProviderType).
So, what is the best approach of implementing this process.
I was trying to implement an interface called IEmailSender depending on the provider type, but I've failed.
I am using .Net core 6
Here is a very crude implementation, but should get you the idea.
Declare your interface for email sender, e.g.
Add implementation classes for each flavour, e.g.
Each of these can have its own dependencies as you wish.
Add a factory class (with an interface too if you want. left that out to keep it simple)
Register each of the implementations, including the factory class.
In your usage, inject
EmailSenderFactoryand resolve anIEmailSenderfrom there.