title: C# interface inheritance with explicit typecast
body: <p>I have a service for creating, saving and sending different types of orders where some types of them will be able to carry attachements.
  The service will send orders to another external service by using IExternalService which is used by several other services with different external endpoints. </p>
  
  <p>IExternalService contains a getter for a external IRepository which is used to send orders to external services.</p>
  
  <p>I've created a new interface for those repositories which will be adding attachements IRepositoryWithAttachement.</p>
  
  <p>I'm providing some sample code below where i left out unimportant stuff:</p>
  
  <pre><code>interface IRepository //Standard repo used by different external services
  {
  string Create(Order order);
  void Update(Order order);
  }
  </code></pre>
  
  <p>Orders with attachements use</p>
  
  <pre><code>interface IRepositoryWithAttachement : IRepository //attachable repo
  {
  string AddFile(Attachement file);
  void UpdateFile(Attachement file);
  }
  </code></pre>
  
  <p>Repo that must send attachements aswell as orders</p>
  
  <pre><code>public class Repository : IRepositoryWithAttachement {...} 
  </code></pre>
  
  <p>Service used by many implementations of external services</p>
  
  <pre><code>interface IExternalService 
  {
   string Name { get; }
   ....
  IRepository Repository { get; }
  }
  </code></pre>
  
  <p>Main service class for creating, saving and sending orders</p>
  
  <pre><code>public class OrderService
  {
  public string Create(Order order)
  {
  ...
  IExternalService eService = _externalServices.GetExternalService(id);
  
  try
          {                
           eService.Repository.Create(order);
          }
          catch (Exception e)
          {
              ....
          }
   ...
  } 
  </code></pre>
  
  <p>Now this particular ordertype will be adding attachments and when it gets the repository with IExternalService it will get an IRepository back and trying to call eService.Repository.AddFile(file) but the AddFile method doesn't exist because the return type is IRepository which i want. But my IRepositoryWithAttachement is extending IRepository so i got confused how i would reach it and i managed to do this:</p>
  
  <pre><code> public string AddFile(Attachement file) {
  
   IExternalService eService = _externalServices.GetExternalService(id);
  
          try
          {                
                  ((IRepositoryWithAttachement ) eService .Repository).AddFile(file); 
          }
          catch (Exception e)
          {               
             ...
          }
   }
  }
  </code></pre>
  
  <p><strong>Question</strong>
  Am i doing this wrong or is it an ugly solution to my problem of getting hold of the addfile method by typecasting?</p>