AliSoftware/Dip

Register with Scope and Tag & Register Generic Classes

Closed this issue · 3 comments

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.

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.

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

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>.