Ticket #778 (new enhancement)

Opened 15 months ago

Last modified 9 months ago

Inversion of Control and Dependency Injection

Reported by: laurentj Owned by:
Priority: highest Milestone: Jelix 1.2
Component: jelix:utils Version: 1.0RC1
Severity: normal Keywords:
Cc: Php version:
Review: Hosting Provider:
Blocked By: Documentation needed: no
Blocking:

Description

Christophe's patch #523 brings a kind of dependency injection / Inversion of Control, but it is not enough. We should provide a real framework for IOC/DI.

First, we should have DI on constructors. We could have too DI throw setters.

I think we could remove methods added by Christophe on jClasses, and then get/create methods could do IOC/DI transparently. For example, this methods could check if there is a mapping for the given selector. If yes, it use jClassBinding etc, and if not, it tries to load the class as usual.

To indicate the dependencies, I don't know yet if we should use an xml file or a simple ini file. We'll see.

Change History

Changed 15 months ago by doubleface

Yes, I added these method to not cause any perf problem. But since this is now just a conditional, this is not a problem any more. I'm looking forward for your remarks. I'm glad to get another point of view of it!

Changed 15 months ago by laurentj

I don't know yet if we would have some perf problem. But the idea is that the "user" of a class won't know if this is a real class or a binding. So a developer could bind *every* class using in a module without modifying the code of this module, without changing create() call by specific method for binding.

Changed 15 months ago by doubleface

This is exactly the point, I think, to allow the user to modify the behavior of a class without modifying the source code of it. This is especially usefull for unit test when you want to test a class with dependencies and only test this class and not its dependencies. And the creation of things like plugins is then very easy.

And about the use of it or not in the classes of Jelix, we should think about it, maybe for form builders (for example), since the overhead is not this bad : indeed, the current implementation does not use reflection (not the case of Fabien Potencier's implementation)... But I know this should be thinked very carefully, just a proposition.

For the understanding of the current implementation, I am working on an article to put on the wiki.

Changed 15 months ago by doubleface

Here is the article (in french)

Changed 15 months ago by laurentj

  • type changed from bug to enhancement

Changed 9 months ago by ametaireau

Here is my implementation of a DI container, that could be included in jelix. http://www.notmyidea.org/?p=189&preview=true&preview_id=189&public=1&nonce=dbae6e73f3

Note: See TracTickets for help on using tickets.