1

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?

5
  • 2
    UsingDbMail() is returning a bool, and a boolean value does not have the property Send. Please edit your question to include a detailed description of what you are trying to do. Commented Dec 29, 2021 at 2:19
  • 1
    Presumably you meant you want engine.UsingDbMail("hello").Send(); (a method named Send on an object returned by the UsingSomething methods); have an interface with the Send member exposed, and have your UsingXyz methods return an object that implements this interface. Commented Dec 29, 2021 at 2:22
  • 1
    Like, public IMailerService UsingDbMail(string body), and then have a class DbMailerService : IMailerService and then you'll have a SmtpMailerService : IMailerService, and so on. Commented Dec 29, 2021 at 2:23
  • 1
    @MathieuGuindon you are a super genius :) thank you so much, that does it. Not sure why I did not think of that - Post as answer please so I can mark it as answered. Commented Dec 29, 2021 at 2:27
  • 1
    Posted from phone, took a little while but, done! Commented Dec 29, 2021 at 2:38

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

That's brilliant, thanks so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.