Sha256: f7f3bbbe546bf0f6d5e288b8315ffaed88b8cf849e41c5ea0de94b041c0155a9

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

Registering services with a Needle registry is very straightforward. The simplest way to do it is:

<pre>
  registry.register( :foo ) { Bar.new }
</pre>

The above will register a new service with the registry, naming it @:foo@. When @:foo@ is requested from the registry, a new instance of @Bar@ will be instantiated and returned.

You get services from the registry in either of two ways:

<pre>
  # Treating the registry as a Hash
  svc = registry[:foo]

  # Treating the service as a property of the registry
  svc = registry.foo
</pre>

h3. Convenience Methods

Because you will often need to register many services with a registry at once, a convenience method has been provided to make this use case lean and mean. Just call @registry!@, passing a block that accepts no parameters. This block will be evaluated in a new context, with any unrecognized method call being interpreted as a new service registration of that name:

<pre>
  registry.register! do
    foo { Bar.new }
    bar { Foo.new }
    ...
  end
</pre>

The above will register two new services with the registry, @:foo@ and @:bar@.

h3. Default Lifecycle

By default, a service is only instantiated once per registry. This means that (using the above example) if the service @:foo@ were queried twice, the registry would return the same object for both queries:

<pre>
  svc1 = registry.foo
  svc2 = registry.foo

  p svc1.object_id == svc2.object_id #=> true
</pre>

You can change this behavior, with _service models_. See the chapter on Service Models for more information.

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
needle-0.5.0 doc/manual/parts/02_services.txt