title: is it an anti-pattern to cast between interfaces that are related but not identical regarding the signatures?
body: <p>First I'm new to the concept of programming against abstractions.</p>
  
  <p>suppose i have the following:</p>
  
  <pre><code>public interface IEmail {}
  
  public interface IEmailWithAttachments : IEmail
  {
      List&lt;Attachment&gt; Attachments {get; set;}
  }
  
  public interface IService
  {
      void Send(IEmail email);
  }
  </code></pre>
  
  <p>EmailService class sends notifications of type IEmail and to do this it uses an SmtpClient which can only send objects of type MailMessage, that's why I used a factory to return a MailMessage instance that simply contains the properties values of the passed IEmail object.</p>
  
  <pre><code>public class EmailService : IService
  {
      private IMailMessageFactory _mailMessageFactory;
      private ISmtpClient _smtpClient;
  
      public EmailService(ISmtpClient smtpClient, IMailMessageFactory mailMessageFactory)
      {
          _mailMessageFactory = mailMessageFactory;              
          _smtpClient = smtpClient;
      }
      public void Send(IEmail email)
      {
          using(MailMessage mailMessage = _mailMessageFactory.CreateMailMessageFromNotification(email))
          {
               _smtpClient.Send(mailMessage);
          }
      }
  
  }
  
  public interface IMailMessageFactory
  {
      MailMessage CreateMailMessageFromNotification(IEmail email);
  }
  
  public class MailMessageFactory : IMailMessageFactory
  {
      public MailMessage CreateMailMessageFromNotification(IEmail email)
      {
          MailMessage mailMessage = new MailMessage();
  
          //copies the IEmail properties to the new created MailMessage instance
  
          return mailMessage;
      }
  }
  </code></pre>
  
  <p><strong>the question:</strong></p>
  
  <p>as far as i know, IEmailWithAttachment is a different type than IEmail, should the factory method check if the passed IEmail object of type IEmailWithAttachment so I can access the attachment list property? like follows: </p>
  
  <pre><code>public class MailMessageFactory : IMailMessageFactory
  {
      public MailMessage CreateMailMessageFromNotification(IEmail email)
      {
          MailMessage mailMessage = new MailMessage();
  
          //copies the IEmail properties to the new created MailMessage instance
  
          IEmailWithAttachment emailWithAttachment = email as IEmailWithAttachment;
          if (emailWithAttachment != null)
              {
                  foreach (var attachment in emailWithAttachment.Attachments)
                  {
                      mailMessage.Attachments.Add(attachment);
                  }
              }
          return mailMessage;
      }
  }
  </code></pre>
  
  <p><strong>OR</strong> should I create another factory method that takes care of IEmailWithAttachments type? </p>
  
  <p>Thanks in advance</p>