I am not sure what this is called so I most likely calling it wrong when I say Inherited interface
. Here is what I am trying to achieve.
I have an interface like this
public interface INotificationEngine
{
bool UsingDbMail();
bool UsingSMTP();
bool UsingSMS();
}
My class looks like this
public class NotificationEngine
{
public class Send : INotificationEngine
{
public bool UsingDbMail(string para)
{
throw new NotImplementedException();
}
public bool UsingSMTP()
{
throw new NotImplementedException();
}
public bool UsingSMS()
{
throw new NotImplementedException();
}
}
}
That allows me to do something like the following
NotificationEngine.Send sendRequest = new NotificationEngine.Send();
sendRequest.UsingDbMail("hello");
What I want to achieve instead is the following
NotificationEngine engine = new NotificationEngine();
engine.UsingDbMail("hello").Send;
Any idea how can I do that with interfaces or any other way?
CodePudding user response:
Start with your common interface, it probably needs to look something like this:
interface IMailerService
{
bool Send ():
}
You'll want an implementation that uses dbmail, another for SMTP, and another for SMS. Construct an instance of these classes in each of your "UsingXyz" methods (note: no need for a nested class here):
public IMailerService UsingDbMail(...)
{
return new DbMailerService(...);
}
public IMailerService UsingSmtp(...)
{
return new SmtpMailerService(...);
}
public IMailerService UsingSms(...)
{
return new SmsMailerService(...)
}
Now when you call a UsingXyz method, you get an object that exposes a Send
method that it implements as needed:
engine.UsingSms(...).Send(); // sends a SMS message