Does somebody tried to create annotated di container?
- erani
- Member | 6
Let me give a small example.
suppose we have next class:
class MyClassA {
/** @Autowired('serviceName') */
private $myVariable
public function setMyVariable(MyClassB $cls) {
$this->myVariable = $cls;
}
No when DI try to build the object it analyze all annotation and inject required services based on annotations.
Also it is possible to create the loader to analyze class annotations and create configuration so you will not require to configure service in neon file, something like:
/**
* @Service('myService')
*/
class MyService {
...
}
So to do something as in Spring Framework
Last edited by erani (2012-10-31 20:28)
- HosipLan
- Moderator | 4668
Oh. Okay.
We had a really long discussion over this topic, and we've agreed that you're breaking inversion of control principle by this. Basically, your class is defining it's service name, and some service is defining that it dependends on specific service. Your code should't have a slightest idea, that there is DI Container or something like services. You should depend on interfaces and abstraction. Not service names.
Next thing is, we do not plan to support injecting to properties. It is kinda agains Dependency Injection. By not using the constructor for mandatory services, you're creating dependency on DIC, which is again wrong. You rather should teach your IDE to help you generate constructor. Something like this – it will save you typing and preserve the functionality.
On the other hand, Nette Framework has really advanced autowiring technology. You can have complex class graph and write so little configuration. You're gonna love Neon ;)
You should use as much constructor injection in bussines layer services as possible. It's the best kind of injection ;)
On presenters, we have inject*()
methods. Unfortunatelly, there
is no documentation in english. You can read the code here and the text translated
here.
- HosipLan
- Moderator | 4668
You don't have to agree. You can even write it as an extension by yourself. But it will (fortunately) never be in Nette. Because it is a bad practise.
Again. You don't have to write huge configuration files. Neon in combination
with autowiring is really expressive and inteligent. The most common scenario is
that all you have to do is list classes in services
section in
config. They will get passed as needed, if you'll hold on to DI.
It is much more expresive than annotations.
- Nox
- Member | 378
You can also split the config files so you don't end up with a single huge one.
for those who do not like write huge configuration files and prefer annotations
Well, so far we know about you only :) although I guess they'd still stick with the “pure” approach
I was reluctant to start with neon config at first, but now I really like it
- redhead
- Member | 1313
There is the thing with Java – all configurations (before annotations) were in XML and it was really a lot of writing and searching of how to write something and it was all very verbose. Neon and the magic behind – autowiring – is quite nice, easy and clear. Also, you have all the configuration at one place and you don't have to jump all over the files to change services.
I used JEE before Nette got DIC support and I was too a bit deterred of another config file (we finally got annotations, right?!) but it turned out that annotations are not needed in this case and all in all they don't help in having clear source code (classes don't need to know, they just get).
Try to look at it from above and clear your mind from annotations hell in Java, give it a go for some time and then decide if it fits you or not.