---
title: "Remove Circular Dependencies by Using Dependency Injection and the Repository Pattern in Ruby"
description: "Shopify broke a circular dependency between an internal pricing gem and its two consumers (the Shopify Core Rails monolith and the Storefront Renderer) by applying the Repository pattern with constructor-based dependency injection."
url: "https://swe.quest/articles/remove-circular-dependencies-by-using-dependency-injection-and-the-repository-605bdeddd937fd64"
canonical: "https://shopify.engineering/repository-pattern-ruby"
source: "Shopify Engineering"
published: "2021-03-19"
difficulty: "medium"
kind: "How-to"
reading_minutes: 8
tags: ["ruby","refactoring","type-systems","testing"]
---

# Remove Circular Dependencies by Using Dependency Injection and the Repository Pattern in Ruby

A post on [Shopify Engineering](https://swe.quest/sources/shopify.md), published 2021-03-19, 8 min read, How-to. Original: [https://shopify.engineering/repository-pattern-ruby](https://shopify.engineering/repository-pattern-ruby)

## Summary

Shopify broke a circular dependency between an internal pricing gem and its two consumers (the Shopify Core Rails monolith and the Storefront Renderer) by applying the Repository pattern with constructor-based dependency injection. The gem defines a contract, made up of shared Schema domain objects and a PricingRepositoryInterface. Each consumer implements that interface over its own data sources and passes the instance into PricingEngine::Engine, which keeps the gem stateless and unaware of how data is fetched. Sorbet signatures and interfaces enforce the contract at the type level. Testing combines mock repositories inside the gem, unit tests for each consumer's implementation, and shared integration test scripts.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Ruby](https://swe.quest/tags/ruby.md) (Languages)
- [Refactoring & migrations](https://swe.quest/tags/refactoring.md) (Engineering practices)
- [Type systems](https://swe.quest/tags/type-systems.md) (Compilers & runtimes)
- [Testing](https://swe.quest/tags/testing.md) (Engineering practices)

## Prerequisites

- Ruby gems ([easy posts on Ruby](https://swe.quest/tags/ruby.md?difficulty=easy))
- Object-oriented interfaces

## Keywords

Repository pattern, Dependency Injection, Sorbet, Ruby on Rails, Storefront Renderer, Shopify Core

## Related posts

- [How We Fixed the Dependency Confusion Vulnerability in Over 600 Ruby Applications](https://swe.quest/articles/how-we-fixed-the-dependency-confusion-vulnerability-in-over-600-ruby-14f9a0a0836510b2.md): Shopify Engineering · 2022-01-27 · Medium · 9 min read · Supply chain security, Ruby, Refactoring & migrations, CI/CD
  Shopify closed a dependency confusion hole across roughly 600 Ruby applications by migrating each \`Gemfile.lock\` to Bundler's newer format, which pins every gem to an explicit source.
- [Shopify-Made Patterns in Our Rails Apps](https://swe.quest/articles/shopify-made-patterns-in-our-rails-apps-92bda4387ca54f09.md): Shopify Engineering · 2021-07-13 · Easy · 8 min read · Ruby, Developer productivity, Distributed databases, System design, Schema migrations
  Shopify's Rails monolith relies on a set of in-house patterns: the dev CLI for consistent environment setup, a podded architecture that shards the full infrastructure stack by shop, and domain-driven components that share a single database with no database-level foreign keys.
- [From Ruby to Node: Overhauling Shopify’s CLI for a Better Developer Experience](https://swe.quest/articles/from-ruby-to-node-overhauling-shopify-s-cli-for-a-better-developer-experience-c31f4bbd707f942e.md): Shopify Engineering · 2023-01-10 · Medium · 12 min read · Developer tools, TypeScript, JavaScript, Refactoring & migrations, Testing
  Shopify rewrote its CLI from Ruby to TypeScript on Node to reduce system requirements, unify the Theme, App and Hydrogen workflows, and stay easy for internal teams to contribute to.
- [Building a Business System Integration and Automation Platform at Shopify](https://swe.quest/articles/building-a-business-system-integration-and-automation-platform-at-shopify-ca92d2227e71ff9f.md): Shopify Engineering · 2022-04-22 · Medium · 11 min read · Ruby, System design, API design, Testing
  Shopify replaced Mulesoft with an in-house Ruby on Rails platform for integrating internal business systems such as Salesforce, Airtable and Netsuite, because the proprietary JVM/XML tool sat outside Shopify's stack and couldn't easily reach internal services like Kafka.
- [A Packwerk Retrospective](https://swe.quest/articles/a-packwerk-retrospective-b9d1167fd6021459.md): Shopify Engineering · 2024-02-07 · Medium · 14 min read · Ruby, Static analysis, Refactoring & migrations, System design
  Packwerk, Shopify's static analysis gem for enforcing package boundaries in Rails monoliths via constant references, works best as a dependency-graph tool rather than an API-privacy tool, which is why privacy checks were removed in 3.0.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
