2

I'm working in c# trying to implement 2 classes that use the same interface. Both classes only require one method each. However the two methods need to be of different types, taking different parameters. I would really like to be able to inherit one single method from the interface and overwrite it in each class as this would be nice and neat (the interface is necessary for the program functionality (working with dependency injection)) Unfortunately both classes are also necessary as the email sender class will be used repeatedly in other programs.

I have found a solution which ALMOST fits my problem, it is: Implementing methods from an interface but with different parameters

The difference that I have is that each of my two classes should only call one method.

Here is my code as it stands (having tried to implement the generics in the previous solution):

interface IEmailSender<T>
{
     T DoEmail();
}

public class EmailGenerator : IEmailSender<MailMessage>
{
     public override MailMessage DoEmail(string emailAddress)
     {
         MailMessage message = new MailMessage();
         //code to generate email message
         return message;
     }
}

public class EmailSender : IEmailSender<bool>
{
    public override bool DoEmail(MailMessage message)
    {
        //code to send email
        return true;
    }
}

Ideally I'd like my email sender method to be void but I don't mind it returning a boolean if it has to have a return value.

Hope that makes sense, I'm literally just trying to be neat here because currently in order to make it work, I would need to have two methods in my interface and have each class call an empty method.

Thanks in advance,

Community
  • 1
  • 1
Anya Hope
  • 1,301
  • 1
  • 17
  • 33
  • 6
    Could you explain why these two class should "share" the same interface ? They don't seem to do similar things, do they ? – Raphaël Althaus Aug 29 '14 at 09:57
  • I had originally wanted one 'email' interface to handle both the classes that dealt with emails. If there is no way of doing this then i'll have to separate them out. – Anya Hope Aug 29 '14 at 10:03

1 Answers1

1

It looks like you are struggling because of a poor architecture. EmailGenerator is not an IEmailSender. There's no case when you'd use it as an IEmailSender.

I would recommend this:

interface IEmailGenerator{
    MailMessage BuildMailMessage(string message)
}

interface IEmailSender
{
    void SendEmail(string message);
}

public class EmailGenerator : IEmailGenerator{
     public MailMessage BuildMailMessage(string message)
     {
          return new MailMessage();
     }
}

public class EmailSender : IEmailSender
{
     private readonly IEmailGenerator _emailGenerator;

     public EmailSender (IEmailGenerator emailGenerator){
        _emailGenerator = emailGenerator;
     }

     public void SendEmail(string message)
     {
          var message = _emailGenerator.BuildMailMessage(message);

          //send email
     }
}

This way you have clearly separated out the difference (and concerns) between Sending an Email and Generating an Email. You can use DI to wire everything together:

class Consumer{
    private IEmailSender _emailSender;

    Consumer(IEmailSender emailSender){ _emailSender = emailSender;}

    void Method()
    {
        string msg = "Hello Wolrd!"
        _emailSender.SendEmail(msg);
    }
}
Philip Pittle
  • 11,821
  • 8
  • 59
  • 123
  • Ok, thanks a lot. I had thought it might be impossible to get both into one interface! I'll see how this goes. – Anya Hope Aug 29 '14 at 10:08