Liftric / DIKit

Dependency Injection Framework for Swift, inspired by KOIN.
MIT License
102 stars 17 forks source link

Consider Scope.prototype renaming #5

Closed guidoschmidt closed 6 years ago

guidoschmidt commented 6 years ago

I stumbled upon the Scope enum and found the prototype case a bit misleading:

/// Defines the `Component` scope.
public enum Scope {
    /// The same instance of the class is returned each time it is injected.
    case singleton

    /// A new instance of the class is created each time it is injected.
    case prototype // Consider renaming this to `clone` or `instance`?
}
// I'd consider prototype as a term for some form of an abstract class
// the attached documentation above even describes the behavior as creating an instance
container.register(as: .prototype) { ComponentX() }

// I'd found one of these more meaningful
container.register(as: .clone) { ComponentX() }
container.register(as: .instance) { ComponentX() }
benjohnde commented 6 years ago

Interesting. I mainly got inspired by the Spring documentation regarding different types of beans.

For sure we can choose a different name. I am also not 100% happy with prototype. But we need to be very clear and concise with the wording. Instance is returned in both cases. A clone is also not correct, because the factory of the Component is invoked every time the Component is requested.

KOIN uses the terms bean (singleton) and factory (no singleton).

What I also found is that some DIF use the term transient and singleton (as well as scoped for session-based contexts, which I also had in mind to implement).

So maybe we choose as of now transient and singleton as our scopes (and later on scoped, if we actually need this). I would also suggest to rename Scope to Lifetime. As far as my understanding goes, scope would be more a specific area in an application than the whole lifetime of a component.

guidoschmidt commented 6 years ago

Yeah renaming Scope would be a good idea. I'd be fine with using singleton and instance as for my understanding a singleton is a specific instance that only exists once 🤔. transient would be ok, too but seems a bit more cryptic. Maybe use singleton and new/object?

benjohnde commented 6 years ago

That is correct in the sense that a singleton does return the same instance over and over again. But wouldn't registering a component as instance mean the same? Because you are registering an instance of an object, that would imply, that you always get the same instance in return.

Here I also found the terms transient and singleton.

The term factory would also work, because we are not registering the instance of an object but the closure (i.e. factory) of the object, which is for resolving invoked every time the object is requested.

benjohnde commented 6 years ago

Ah that's tricky, because we are basically only registering factories and neither singletons nor instances but the scope or lifetime is important when it comes to resolving.

So the term factory would also be displaced.

benjohnde commented 6 years ago

Settled for the first release even if the choice is maybe not the best the best. We can change that later on.