asalom / swifttalking.com

https://www.swifttalking.com
MIT License
0 stars 0 forks source link

Dependency Injection with Service Locator | Swift Talking #10

Open utterances-bot opened 4 years ago

utterances-bot commented 4 years ago

Dependency Injection with Service Locator | Swift Talking

With the Service Locator pattern we can make a registry of dependencies for a given object. There is a very simple way to create one with minimal code, learn how in this post.

https://www.swifttalking.com/dependency-injection-with-service-locator/

jpinsonault commented 4 years ago

I appreciate the minimalism. Did you ever run into any issues with it, or think of ways to improve it?

asalom commented 4 years ago

Hello @jpinsonault, thanks for taking the time to read the article and to make a comment.

We made use of this pattern in a rather large project and the main issue we encountered was the amount of extra code we needed to add as a result of defining new objects. For every object we defined we had to create a service locator protocol and the extension. This made adding new objects cumbersome. On the other hand it brought structure and a defined way to add dependencies to an object which left no imagination to the developer. Whenever you needed a dependency, you knew exactly where to look for it.

We didn't develop it further but I am sure there is room for improvement. If I had to start somewhere, I would look into generics and a way to avoid having to define so many protocols

WorldsGreatestDetective commented 3 years ago

I read this article when attempting to understand dependency injection/service location patterns as a result of reading that the DIP framework can be used as a service locator and is essential in iOS development;

locatorhttps://cocoapods.org/pods/Dip

I suppose this container's entire purpose is to help simplify managing and adding new objects. Thoughts?