DI container for Google Closure - The next big thing (at least for me).

489 views
Skip to first unread message

Daniel Steigerwald

unread,
Dec 18, 2013, 2:08:39 AM12/18/13
to closure-lib...@googlegroups.com
I have long thought about the DI container and IoC stuff. I wrote several pretty big apps with Google Closure Library.
I always tried to avoid big bull of mud code, but it wasn't easy. I tried to make everything pluggable, replaceable, so that's how github.com/steida/este-library was born.
Library is somehow clean now, but app code still sucks. The golden rule I learned was: Do not use global object, inject everything, service locator is evil, dependency injection ftw.

With such approach my apps become much more cleaner etc., but I had to write a lot of boring boilerplate wiring code. Factories, plugin configuration etc.
I was doing "bastard injection" and "poor man's injection', and despite pattern naming, it was a huge win for code quality.

Today I released https://github.com/steida/grunt-closure-dicontainer. I suppose it will be next huge boost for code quality and frictionless development too.
When designing, I tried to avoid several common mistakes of DI container design or usage.

1) This container can't be used as service locator. There is a detection and nice warning message.
2) This container does auto-registration, so you don't have to register anything. Just create class and consume it where need. Done.
3) This container is not "stringly typed". It parses type annotations and use parsed typed for container generation.
4) This container is developed with strict TDD in mind.
5) This container does not breaks advanced compilation and does not add any overhead.
6) There is no run time magic, generated code can be easily viewed and debugged, if needed.

Of course there are still lot of work to be done, for example: decorators, as and by resolvers, full type expression parsing (huge possibilities), managing lifestyle and more.
But I pushed it to npm anyway, because I am using it already in real project, so I consider it as ready for production.

Question?







Nathan Wright

unread,
Dec 18, 2013, 2:01:47 PM12/18/13
to closure-lib...@googlegroups.com
The example shows how to initialize the DiContainer, could you also show how App might look?



--
 
---
You received this message because you are subscribed to the Google Groups "Closure Library Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to closure-library-d...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Lukáš Vlček

unread,
Dec 18, 2013, 5:10:28 PM12/18/13
to closure-lib...@googlegroups.com

Nice work Daniel, thanks for this!

My questions: if I do not use grunt, can I use your DI module? What is the function of grunt in DI flow? Can you imagine using your module without grunt?

Regards,
Lukáš

Dne 18.12.2013 8:08 "Daniel Steigerwald" <dan...@steigerwald.cz> napsal(a):

Daniel Steigerwald

unread,
Dec 18, 2013, 8:27:51 PM12/18/13
to closure-lib...@googlegroups.com
Nathan, I will prepare full example very soon. For now just some snippets for you:

Note how routes are created in app.Routes and used for wiring with Controllers.

Note, how we prefer route object over hardcoded url strings. It allows us to do some nice things.

este.labs.App is not entirely finish, but usable already.

Feel free to ask any question.



Daniel Steigerwald

unread,
Dec 18, 2013, 8:44:28 PM12/18/13
to closure-lib...@googlegroups.com
Hi Lukáš, 

so how it works. grunt-closuredicontainer is just a code generator. It needs class or list of classes to be resolved and path to deps.js file. I am using Grunt with watchers, so DI container is regenerated every time when I save any type. It parse classes annotations and write DI container for you.

Btw, I really recommend Grunt. Not only for dicontainer, but also for deps.js generating, test running, style check etc. like I have in github.com/steida/este
I am using CoffeeScript but it works with Javascript too because CoffeeScript is just a better syntax for plain old JS.


Vel Bala

unread,
Dec 19, 2013, 9:04:31 AM12/19/13
to closure-lib...@googlegroups.com
thanks to all friends

Daniel Steigerwald

unread,
Mar 28, 2014, 6:47:21 PM3/28/14
to closure-lib...@googlegroups.com
Closure DI container is now plain Node.jsmodule https://github.com/steida/closure-dicontainer
Gulp version will be release very soon.

thanpolas

unread,
Apr 4, 2014, 5:58:15 AM4/4/14
to closure-lib...@googlegroups.com
Arrrrrrr please please please use vanilla JS for the examples!

And it would really help if there was an overview of what's going on with the DI task... (Google Apps Drawing is awesome for this)

Just create a gist with the Dependent Class, the class that gets the DI from the Dependent Class and how you would setup your gruntfile to build those two (and optionally what the outcome would be)...

I've been too busy lately to lay my hands on this but i really want to see what's going on here...

Daniel Steigerwald

unread,
Apr 4, 2014, 6:53:51 AM4/4/14
to closure-lib...@googlegroups.com
You know, learning CoffeeScript is faster then writing wtfcoffee? ten times :-)
Done: https://github.com/steida/closure-dicontainer

Btw, you need Grunt or Gulp or something, or wait for new Este, which will be released pretty soon and you will see a lot of examples there.


--

---
You received this message because you are subscribed to the Google Groups "Closure Library Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to closure-library-d...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages