Service Container

1 year ago by giacholari

Right, so what is a service container? Now, if you are a seasoned developer, go ahead and skip this snippet( I presume that you are familiar with that). If you are a newcomer stay still. It sounds complicated but I promise it is just a term describing a tool. Think of it as a black-box where you put things for later use. Say for example you use Asp.Net Core or Laravel or Django for those who are into python. Now instead of newing up instances of classes manually within your codebase, you can leverage the service container and it will do it for you automatically through reflection. What does reflection mean? It means that the program will scan your file and if it finds a key (which you have specified in the container) it will try to resolve it (new it up) and on top of that it will try to resolve any other dependencies on that returned value from the key before and it will try until there are no more dependencies to be resolved or if it fails for any reason in between. Let it sink. You can bind anything to the container - data structures, interfaces to implementations, and much more. Combine it with the repository pattern(helps to abstract layers of your application) and your code will become that much more decoupled. Next time you create a service for your app, think through this and maybe leverage the service container (I would recommend that you code into an interface and not to an implementation).