intravenous icon indicating copy to clipboard operation
intravenous copied to clipboard

Inconsistent behavior with singleton in factory usage.

Open Meettya opened this issue 9 years ago • 0 comments

I have some strange behavior (maybe its ok, but strange) (all examples in CoffeeScript): first - create class Foo

module.exports = class Foo

  # intravenous injection
  @$inject = ['Helper']

  constructor: (Helper, options) ->

next, register two classes - Helper and Foo

container.register 'Helper', Helper, 'singleton'
container.register 'Foo', Foo

and now use somewhere Foo factory

module.exports = class Bar

  # intravenous injection
  @$inject = ['FooFactory']

  constructor: (FooFactory, options) ->
    @_foo_instance_ = FooFactory.get()

If Helper used in Foo only - it will be re-created on every Bar call (and it may be correct - its singleton in factory context, but how about perRequest?).

Instead, if we are call use Helper in non-factory classes to - it will be created once only and re-created on Bar calls (and its looks more correctly - its singleton as registered).

May be I`m miss something, but its looks strange. And actually I cant decide what kind of behavior will be correctly. Just appoint in docs?

Meettya avatar Nov 10 '14 09:11 Meettya