yii2-cookbook icon indicating copy to clipboard operation
yii2-cookbook copied to clipboard

How to use DI Container (suggestion)

Open cherifGsoul opened this issue 9 years ago • 13 comments

I will be nice if the book will have a section that show how to use yii\di\Container and how it can help to decouple the business logic from the framework.

cherifGsoul avatar Apr 26 '15 14:04 cherifGsoul

and how it can help to decouple the business logic from the framework.

if you use a framework class for this you are already coupled to the framework. Can you explain a bit more what you mean?

cebe avatar Apr 26 '15 14:04 cebe

Let's see take an example of application that has entities and services that holds and business rules, I dont talk about the database (AR/Queries) or UI/Web (Controller/View) part here, those classes are decoupled, independentely tested and they dont use any class from the framework, I want to see how I can inject those classes in the different parts of application (Controllers, Components, "maybe" Behaviors etc) with the help of the di\Container if it is possible

cherifGsoul avatar Apr 26 '15 14:04 cherifGsoul

I'm not sure that it is right use case, but I can provide example how u can use aws-sdk inside yii2 app using DI. If that is what use case I will be glad to write example and also get confirmation from Yii core dev team that it is right way to do that :). I'm relatively new to Yii2 so it can be that there is better way to do this.

zmilan avatar Apr 26 '15 15:04 zmilan

@zmilan Yes please

cherifGsoul avatar Apr 26 '15 15:04 cherifGsoul

@cherifGsoul https://github.com/zmilan/yii2-di-aws . When u include it in project u will be able to use it inside any function by adding parameter like this:

function someFunc (AwsFactory $aws) { ... }

AwsFactory is defined like singleton inside Yii2 DI container. This mean that each time it will inject same instance of AwsFactory. You can still create new instance if u need it. I hope this will help :).

@cebe I will like if u can review this code and give me some feedback about it.

zmilan avatar Apr 26 '15 16:04 zmilan

Thank you for the example @zmilan it helps so much :)

cherifGsoul avatar Apr 27 '15 11:04 cherifGsoul

@cherifGsoul You are welcome. It is simple code and I still learning Yii2 and all great new features that we get with it. I can say that it is really amazing now (even I like Yii1 too, it is still amazing framework, even it is outdated now)...

zmilan avatar Apr 27 '15 12:04 zmilan

@zmilan me too Im still learning the YII2, thanks again :)

cherifGsoul avatar Apr 28 '15 09:04 cherifGsoul

@cherifGsoul you are welcome. I hope I will catch some more time so I can help more with examples and tutorials. I will more and more familiar with Yii2 with each day, since it is currently my main tool most of time during my full day job... I hope @cebe will get chance to check my example and give us some feedback about it - is it on right track or there is some better way to use it. Thanks in advance.

zmilan avatar Apr 28 '15 10:04 zmilan

had no time to read everything but this comment sounds like you are mixing up two things. There is no "old way" but two different concepts you can use for different things. Seems similar confusion as in https://github.com/yiisoft/yii2/issues/8137#issuecomment-94425556. you should read up about the concepts of DI and service locator.

cebe avatar Apr 29 '15 22:04 cebe

@cebe , thanks for your time and comment. I agree totally and do it with intention, because in our old application we was use 'aws' like Yii component, so I add it for easy porting our code... but I understand that is not needed and that is mixing two way. My comment about 'old way' was because there wasn't DI in Yii1... I also read everything about DI and service locator on Yii docs and related links... Only reason why i mixing up two things was easier porting our old code that already use Yii::app()->aws...

zmilan avatar Apr 30 '15 05:04 zmilan

@zmilan Relying on interfaces is a good start point, the YII2 docs http://www.yiiframework.com/doc-2.0/guide-concept-di-container.html give good examples but real world show case will be better IMO

cherifGsoul avatar May 11 '15 09:05 cherifGsoul

@cherifGsoul Yes I use that page and related links like guide. It is really great feature and I specially like how easy u can inject anything inside controllers, actions, ..., anywhere... it make much more easier to write code...

zmilan avatar May 11 '15 10:05 zmilan