title: How to implement generic base classes that conform to protocols having type constraints and implement a factory for those generic types?
body: <p>Im trying to implement the repository pattern in my project. I'd like the design to be flexible enough so that I can swap out the underlying implementation should I need to in the future. For example, I'd like to be able to support Realm but be able to swap it out with Core Data if need be. I'd also like to implement fakes for testing.</p>
  <p>I'm starting the implementation with fakes and what I'd like to do is implement a FakeRepository base class with generic constraints that conforms to the Repository protocol.</p>
  <p>Note that I've omitted some of the implementation details to keep the post as short as possible.</p>
  <pre><code>protocol Repository {
      associatedtype Item
  
      var count: Int { get }
  
      func item(at index: Int) -&gt; Item
  }
  
  class FakeRepository&lt;Model&gt;: Repository where Model: Identifiable {
      private var items: [Model] = []
  
      var count: Int { items.count }
  
      func item(at index: Int) -&gt; Model {
          return items[index]
      }
  }
  </code></pre>
  <p>I'd also like to define a protocol for each of the models that I intend on supporting. For example, a UserRepository.</p>
  <pre><code>struct User: Identifiable {
      let id: Int
      let name: String
  }
  
  protocol UserRepository: Repository where Item == User { }
  </code></pre>
  <p>Now all I have to do is define a concrete FakeUserRepository and I don't need to implement anything because all the work is already done.</p>
  <pre><code>class FakeUserRepository: FakeRepository&lt;User&gt;, UserRepository { }
  </code></pre>
  <p>The trouble I have is when I get to the factory pattern implementation. What I'd like to do is something like this, but the factory protocol doesn't compile because UserRepository has associated type requirements.</p>
  <pre><code>protocol UserRepositoryFactory {
      func makeRepository() -&gt; UserRepository // DOES NOT COMPILE
  }
  
  struct FakeUserRepositoryFactory: UserRepositoryFactory {
      func makeRepository() -&gt; UserRepository {
          return FakeUserRepository()
      }
  }
  
  struct CoreDataUserRepositoryFactory: UserRepositoryFactory {
      func makeRepository() -&gt; UserRepository {
          return CoreDataUserRepository()
      }
  }
  </code></pre>
  <p>And then I'd like to pass around a dependency container with all of my factories.</p>
  <pre><code>struct DependencyContainer {
      let userRepositoryFactory: UserRepositoryFactory
  }
  </code></pre>
  <p>Another solution I tried was like this:</p>
  <pre><code>protocol Repository2 {
      associatedtype Item
      func item(at index: Int) -&gt; Item
  }
  
  class Repository2Base&lt;Model&gt;: Repository2 {
      func item(at index: Int) -&gt; Model {
          fatalError()
      }
  }
  
  class FakeRepository2&lt;Model&gt;: Repository2Base&lt;Model&gt; {
      var items: [Model] = []
      override func item(at index: Int) -&gt; Model {
          return items[index]
      }
  }
  
  protocol UserRepositoryFactory2 {
      func makeRepository() -&gt; Repository2Base&lt;User&gt;
  }
  
  class FakeUserRepositoryFactory2: UserRepositoryFactory2 {
      func makeRepository() -&gt; Repository2Base&lt;User&gt; {
          return FakeRepository2&lt;User&gt;()
      }
  }
  </code></pre>
  <p>Now this compiles and works well but I don't like how I have to invoke <code>fatalError()</code> just to compile. It seems like a hack. Is there an elegant way to achieve my goal?</p>