# Using dry-system auto-registration with singletons

**URL:** <https://discourse.dry-rb.org/t/using-dry-system-auto-registration-with-singletons/656>\
**Category:** Uncategorized\
**Created:** [November 26, 2018, 4:27pm UTC](https://discourse.dry-rb.org/t/using-dry-system-auto-registration-with-singletons/656 "2018-11-26T16:27:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmaze](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@dmaze](https://discourse.dry-rb.org/u/dmaze)\
**Post date:** [November 26, 2018, 4:27pm UTC](https://discourse.dry-rb.org/t/using-dry-system-auto-registration-with-singletons/656/1 "2018-11-26T16:27:45Z")

</div>

I’m trying to use the dry-system auto-registration to create some objects that “should” be singletons, though I don’t want to force them to be at a code level. For example I might have some sort of domain-aware cache that I want to have injected, and every time it gets injected it should have the same underlying object.

If I have a trivial object that exposes `#h` as a Hash and make that auto-registered, I can readily see that the data isn’t getting shared, like

```ruby
Application[:component].h[:k] = :v
v = Application[:component].h[:k]
print v ? v : "[nil]"

```

(Complete example at [https://gist.github.com/dmaze/1cb828a47045b2214a1491fc34cb6144](https://gist.github.com/dmaze/1cb828a47045b2214a1491fc34cb6144))

Is there a convenient way to make the auto-registrar provide a single instance of the object to all consumers, instead of having it get recreated on every call?

---

<div class="post-metadata">

**Author:** ![timriley](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/timriley/32/25_2.png) [@timriley](https://discourse.dry-rb.org/u/timriley)\
**Post date:** [November 27, 2018, 12:08am UTC](https://discourse.dry-rb.org/t/using-dry-system-auto-registration-with-singletons/656/2 "2018-11-27T00:08:15Z")

</div>

The simplest way right now would be to register the object in a bootable component, e.g. `boot/my_cache.rb`:

```ruby
MyApp::Container.boot :my_cache do
  start do
    require "my_cache"

    register "my_cache", MyCache.new
  end
end

```
