ColumbiaJS / js-course

Columbia Computer Science Course in Advanced Javascript as Taught By Lev Brie
23 stars 4 forks source link

hw3 dependencies question #30

Open levbrie opened 9 years ago

levbrie commented 9 years ago

@cz2246 asked:

I am trying to implement the dependencies on other modules and cannot seem to figure out how to get the functions registered in modules a module is dependent on? Where should we store them? Should we store them inside the modules dependent as well?

My response: @cz2246 Very good question. This is one of the most important issues you have to solve when creating a dependency injection system. There are a few different ways you can handle this, but there are 3 things you have to consider when deciding how to get the functions registered in dependent modules:

  1. Each module specifies its dependent modules at the time it is created.
  2. At the time a module is created, it is not necessarily the case (in fact, it will rarely be the case) that all of the modules it requires as dependents have already registered all of their functions. This means that you can't simply add in all of another modules registered functions at the time you create your module.
  3. Module dependencies may be circular. You need to make sure you can handle circular dependencies (and not loop forever).

In other words, each module needs to have a way to hold on to a reference to all of the modules that it depends on and, if it can't find a function in its registry, it needs to have a way to loop through its dependents until it finds that registered function (or fails to find it).

Make sense?

njt2116 commented 9 years ago

If anyone else is new to this dependency injection concept, there is a Pluralsight course (albeit slowly paced...) that explains what dependency injection is and what it is doing. For our purposes I think you can watch skip ahead and watch the DI module. Thought I'd share -- here's the link: http://www.pluralsight.com/courses/inversion-of-control.