AliSoftware / Dip

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

Register with Scope and Tag & Register Generic Classes #162

Closed vander2675 closed 7 years ago

vander2675 commented 7 years ago

I've got a Problem with registering a Class with a Tag and a Scope. I can't find the right registerMethod for it.

What I want to do:

            container.register(.uniqe, tag: EntityType.User) {
                PutImpl<User>(backgroundContext: try container.resolve())
            }

Wich is not possible. Is there a correct registerMethod for this kind of Registration?

And while I'm at it: Is this (using Tags) how you would resolve a particular Instance of an Generic Class with DI? Thank you very much for your help. Still getting into DI.

ilyapuchka commented 7 years ago

I'm not sure I understand why you can not find a method that registers with scope and tag at the same time - all register methods accept scope and tag parameters. Does your tag type conform to DependencyTagConvertible? And you should resolve your component with container.resolve() as PutImpl<User>

Different containers approach resolving generic types in different ways, sometimes it's dictated by language features. i.e. in C# di containers you can skip providing generic parameters when registering generic type because C# support "open generics". This is not something that is possible in Swift.

vander2675 commented 7 years ago

Okay Thank you very much. Is it possible to register a generic Class once and resolve it with multiple Instances?

ilyapuchka commented 7 years ago

You register it with unique scope just like other types. For container the generic nature of the type does not matter, you register it as PutImpl<User> and you resolve it as PutImpl<User>.