samdark / yii2-cookbook

Yii 2.0 Community Cookbook
1.45k stars 296 forks source link

How to use DI Container (suggestion) #51

Open cherifGsoul opened 9 years ago

cherifGsoul commented 9 years ago

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.

cebe commented 9 years ago

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?

cherifGsoul commented 9 years ago

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

zmilan commented 9 years ago

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.

cherifGsoul commented 9 years ago

@zmilan Yes please

zmilan commented 9 years ago

@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.

cherifGsoul commented 9 years ago

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

zmilan commented 9 years ago

@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)...

cherifGsoul commented 9 years ago

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

zmilan commented 9 years ago

@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.

cebe commented 9 years ago

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.

zmilan commented 9 years ago

@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...

cherifGsoul commented 9 years ago

@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

zmilan commented 9 years ago

@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...