Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to implement Inherited interfaces in C#

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 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?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading