AliSoftware/Dip

Something like resolve by id

v-silin opened this issue · 3 comments

Hi.

Is it possible to add ability to resolve entities by id?
Something like shared+unique scope:
let a = container.resolve(id: "SomeId") as Service
let b = container.resolve(id: "SomeId") as Service

a === b // true

Best regards,
Vasili Silin.

Nice. Thank you.


Just wanted to double check:
// raw code with syntax mistakes but same sense.
container.register(tag: "AnyTag") { |instance 1| } // with tag
container.register(tag: nil) { |instance 2| } // without tag

var a = container.resolve(tag: "AnyTag") // instance 1
var b = container.resolve(tag: "NewTag") // instance 2
var c = container.resolve(tag: nil) // instance 2

So, the following is correct:
a != b
a != c
b == c


Are everything above true?

Not exactly, first you should use singleton scope, otherwise each resolve will create new object, shared reuses objects only within single objects graph.
And then b will not be the same as c because it is associated with tag even though this tag was not registered. This is stated in the docs.