hmlongco / Resolver

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

Reset on .application scope #124

Closed tomisacat closed 3 years ago

tomisacat commented 3 years ago

The .application scope is simply an instance of ResolverScopeCache, you can't prevent users from calling reset method within Xcode build system.

Maybe we should establish a distinct scope for .application(called ResolverScopeApplication) and prevent the reset method with fileprivate modifier? e.g.,

public class ResolverScopeApplication: ResolverScope {

    public override init() {}

    public final override func resolve<Service>(resolver: Resolver, registration: ResolverRegistration<Service>, args: Any?) -> Service? {
        if let service = cachedServices[registration.cacheKey] as? Service {
            return service
        }
        let service = registration.resolve(resolver: resolver, args: args)
        if let service = service {
            cachedServices[registration.cacheKey] = service
        }
        return service
    }

    /// The only distinction is replacing public with fileprivate
    fileprivate final func reset() {
        cachedServices.removeAll()
    }

    fileprivate var cachedServices = [String : Any](minimumCapacity: 32)
}
nashfive commented 3 years ago

It's been discussed here : https://github.com/hmlongco/Resolver/issues/86

hmlongco commented 3 years ago

As discussed earlier, it was needed in order to accomplish a system-wide reset for testing, etc. Prior to that change the inheritance went the other way, application first, then cache added the reset.