Drupal 8 why I should use Dependency Injection? Why it is a best practise? - drupal

Every article about dependency injection in Drupal says, that you should use and that it is a best practice.
I know how to use it and I'm using it in my code.
But I don't about a reason why it is best practice to use it? Why it is a best approach and why I should use it? Why we should avoid to call service as static \Drupal::service('database')?
Is it related with performance issues?
In articles which describe Dependency injection in Drupal no information about the reason why to use DI. Just only designation that everyone should use it.

Dependency Injection is a software design approach that allows avoiding hard-coding dependencies and makes it possible to change the dependencies both at runtime and compile time.

Related

Symfony2 - Using old external library without namespaces

Let me preface this by saying that I am normally not a PHP developer, and am a complete beginner when it comes to Symfony2. I was kind of thrown into this project, and we are on a pretty tight deadline.
With that out of the way, here is my problem:
At my company, we have a PHP library for an internal API which is old; it doesn't use namespaces and it doesn't really follow any standard naming convention such as PSR-0 or PEAR. However, I need to use this in our Symfony2 project. Symfony2 obviously relies on namespaces a lot. Am I completely screwed if I don't update the library to follow the "new" conventions, or can I somehow use it anyway? The thing is, it is a pretty huge library, and we simply don't have time to do that right now (someone should have done it ages ago, but that is another topic...).
Theoretically, could I just place the lib under vendor/ and use good old old requires/includes in my bundles? Will this even work? Will I get a mob of angry Symfony/PHP developers after me by doing this?
Or, is there a better way that I'm not aware of?
You can do what you suggested, and you can even use the MapClassLoader, and give it a static map between your classes and your files, so that you don't even need to require files from your library. You don't have to write this map by hand, you can generate it with the class map generator

Does one still require an IOC to use Fluent Security?

Is it possible to run Fluent Security without IOC? My application does not currently use IOC, hence the reason for the question.
Yes, FluentSecurity does not require an IoC-container (it never has).
However, if you need to replace anything in FluentSecurity or if you need dependency injection for your policies, you need a way to provide FluentSecurity with those instances/dependencies.

Dependency injection need some explanation

I have been working alot on MVC3 now a days and use Dependency Injection ninject etc.
I find it useful in Testing, don't make concrete implementation of classes like Model but instead injected.
My Questions:
How do we explain DI. Any simple definition to it.?
The benefits of DI?
Can we use DI in ASP.NET web forms?
Thanks
Dependency injection is eliminating the objects dependencies of concrete classes.
Benefits:
It allowed to use an abstract interface instead of a concrete class.
This makes lots of (large) applications to be more manageable, if you need to swap out a class, its easier to inherit from an interface and make the IoC container switch to another class.
And if you're looking to use DI in ASP.NET Web Forms in the business logic, yes you can.
Dependency injection means giving an object its instance variables.
Here is a great article about it:
http://jamesshore.com/Blog/Dependency-Injection-Demystified.html.
Can't be simplier, I think.
Benefits: Loose coupling. Easy implementation changes by just editing config file. Easy testing with mocks.
In WebForms? Sure, I've been working on a project where we successfully used Castle Windsor to inject our Repositories.
Brad Wilson has a really good ASP.NET MVC 3 Service Location blog series. It may help you see the value.

Ent Lib Unity when to use

i've been trying to get my head around on EntLib 5.1 Unity and it's confusing me a lot http://msdn.microsoft.com/en-us/library/ff660864%28v=PandP.20%29.aspx.
Could anyone please tell me on what type of scenario I can use Unity?
I've a requirement to load the specific dll based on request type. Can Unity be used on this scenario?
Thanks a lot.
For dynamically loading DLLs, you may want to take a look at MEF.
Unity is for decoupling dependencies among classes to make it easier to write testable, reusable code.
It is an Inversion of Control (IoC) container library that makes it easier to do Dependency Injection. There are numerous examples on Stack Overflow. Note that you can configure Unity in code (my preference) or with a configuration file.

Good Resource Loading System

I'm currently looking for a resource management system for actionscript3. More specifically in this case, I'm looking for opinions on off-the-shelf resource loaders/managers from people who have used them,
are they clean/simple to use?
are they designed to be extended?
I plan on using this with an MVC like system, Mate is next on the list, has anyone else used a resource loader in this manner?
masapi
queueloader
bulk-loader
I've been very happy with bulk-loader. We've integrated it with Parsley's MVC and IoC frameworks - http://spicefactory.org/parsley/ - with great success.

Resources