DEV Community

Discussion on: A different approach to frontend architecture

Collapse
 
joruch profile image
Joris

There are some good ways to do DI in the frontend nowadays, with BottleJS for example. Angular also heavily uses DI. Another reason why I don't get why people don't seem to talk about that more.

Collapse
 
cfuehrmann profile image
Carsten Führmann

I just glanced over the docu of BottleJS. It's interesting to see how the DI container API compensates for the language's lack of static types: When you register a service whose "constructor" has arguments, you have to list, during registration, the names of the registrations that should be passed for those arguments. For example, assume that Beer has three arguments:

var Beer = function(barley, hops, water) { /* A beer service, :yum: */ };
Enter fullscreen mode Exit fullscreen mode

Then, when we register Beer, we have to pass the names of the registrations for 'Barley', 'Hops', and 'Water' during registration:

bottle.service('Beer', Beer, 'Barley', 'Hops', 'Water');
Enter fullscreen mode Exit fullscreen mode

In a statically typed language, we wouldn't have to list 'Barley', 'Hops', and 'Water' during registration since they would be discovered via reflection from the typed arguments of the Beer's constructor. This is what I meant when I wrote DI containers are less convenient in languages without runtime types.

Be that as it may, BottleJS looks nice.