hmlongco / Resolver

Swift Ultralight Dependency Injection / Service Locator framework
MIT License
2.14k stars 188 forks source link

Expose `rawValue` to allow `Hashable` and `Equatable` external conformances #129

Closed minuscorp closed 2 years ago

minuscorp commented 2 years ago

Right now, there's no way to build objects like [Resolver.Name: SomeObject] in order to register collections of named dependencies because Resolver.Name does not conform to Hashable. As it is a very concrete need, instead of adding the conformance inside the library I see more convenient to just make public the rawValue so it is possible to debug names and make external new conformances.

minuscorp commented 2 years ago

Bump? @hmlongco

hmlongco commented 2 years ago

I feel that if we needed Hashable and Equatable we should just add it rather than force everyone to roll their own.

That said... I don't get the "concrete need" you're attempting to solve. Why would you want to create collections of named dependencies for registration in the first place?

minuscorp commented 2 years ago

We have different configurations that register different services and we test with collections which name to register with which namespace. So we use names as keys and values as services, collections of services and such.

hmlongco commented 2 years ago

Resolver.Name now conforms to Hashable and Equatable.

    func testResolverNameHashableConformance() {

        let registrations: [Resolver.Name:XYZNameService] = [
            .fred : XYZNameService("Fred"),
            .barney : XYZNameService("Barney"),
        ]

        registrations.forEach { (name, service) in
            resolver.register(name: name) { service }
        }

        let fred: XYZNameService? = resolver.optional(name: .fred)
        let barney: XYZNameService? = resolver.optional(name: .barney)

        // Check all services resolved
        XCTAssertNotNil(fred)
        XCTAssertNotNil(barney)

        // Check correct service factories called
        XCTAssert(fred?.name == "Fred")
        XCTAssert(barney?.name == "Barney")
    }