AliSoftware / Dip

Simple Swift Dependency container. Use protocols to resolve your dependencies and avoid singletons / sharedInstances!
MIT License
978 stars 75 forks source link

Something like resolve by id #164

Closed v-silin closed 7 years ago

v-silin commented 7 years ago

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.

ilyapuchka commented 7 years ago

You can use tags for that - https://github.com/AliSoftware/Dip/wiki/named-definitions

v-silin commented 7 years ago

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?

ilyapuchka commented 7 years ago

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.