dependency injection

Dependency Injection in PHP

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

Every project will have lots of classes. Few classes will depend on other classes.  For example, we have a Library class which gets the name of the books from the database. So our library class is dependent on the database class.

 In the above example,  the library object will have full access to database which is what we want. But the way, we are providing this access is not great.

The Library class is tightly coupled with the Database class, and while this might not look bad on this particular example. Let’s imagine that we want to reuse the Library class elsewhere, it will fail if the database class is not included.

Now lets look at this code.

That’s a nice way of injecting the Database class inside the Library class.  And the Library class can now be used in any application. We just have to pass the database object to the Library class to the application. But what if the parameters are more in the constructor ?

Like this:

Over the time it gets messy.  I don’t like this much.

So the next good way to take over this situation is by using the setter injection.

This is way too nice and neat. But you will soon realize that over the period of time, this will start smelling bad. If you have to use this class at 5 different places, you will have to initiate this 5 times, writing the same code. That’s crap.

What now ? We need something that is more robust and can control class initialization at one place.  Lets look at the below code.

We can then call this by:

This code is much more flexible and it has lot of benefits. Lets say you want to access books from two different database.

Dependency injection really is the answer to more maintainable, testable, modular code. Its very easy to do unit tests with such modular code. 

Conclusion:

According to me, the constructor should not be overloaded with parameters.  Its better to inject the dependencies as and when required. However few users prefer to pass everything together in the constructor for easy tracing. So this might differ from users to users. Dependency Injection greatly reduces cost in the long run and makes the code highly scalable with less effort.

If you have any points, please do comment.

angular-featured

Tutorial: AngularJS Fundamentals

In this tutorial i will explain AngularJS Fundamentals with a user management demo application.

I have been very happy with jQuery and didnt pay much attention to AngularJS, thinking it is similar to jQuery. But i was so dead wrong.  After reading and exploring this Framework, i fell in love with it instantly.  Without a doubt,  

AngularJS is quickly becoming the dominant JavaScript framework for professional web development.

What is AngularJS ?

In short its a javascript framework and follows the concept of MVC. It is used to create Single Page Applications , shortly known as SPA. It makes development and testing really a cake walk.

How does it help ?

Lets say, we need to create a complete Ajax based application. So, what all would you account for ? I suppose the below points would be valid.

  1. Make sure the urls change and can be parsed by bots for better SEO.
  2. Your app might depend on other libraries or classes which you might want to inject only when its required.
  3. You will want the UI to react (like update/insert) whenever you make some changes.
  4. The navigation should be smooth and robust and other views should be able to switch in and out easily.
  5. You will want to make your code more maintainable and scalable.

AngularJS is the solution for all the above mentioned points. It takes care of everything. 

How do i use ?

AngularJS has got a very comprehensive documentation here.  In this post, i will help you to get started.

I will create a user management application to insert, edit and delete users. Watch the demo here before getting started.

Demo

Organize your project in the below way:

angular-dir

Screenshot of our application:

angularapp

index.html

Here we have included the required js files.

In line 16, ng-app=”APP” defines the scope of your application. You can place it in the <html> tag as well. It is used to auto bootstrap the angularjs application.

ng-controller attaches a controller class to the view. Which means all our business logic can be written inside UserController.

ng-view : All your views will be positioned inside this div. Based on the url, the view gets pushed in and out.

partials/users.html

The first thing you’ll notice in this template is the use of expressions (“{{“ and “}}”) to return variable values. In AngularJS, expressions allow you to execute some computation in order to return a desired value.

ng-repeat directive is one of the most commonly used and serves to define your template scope when looping through collections. In the example above, it replicates a line in the table for each user in users.

 partials/addUser.html

We are now making use of the ng-model directive; this directive will bind your text field to the $scope.user.fname variable and make sure that its value is always up-to-date with the input value. 

So, when we want to edit a user or add a user, ng-model will be responsible to get the data for editing a user and also to update the data.

ng-click allows us to specify a custom behavior when an element is clicked. So, on clicking this link, saveUser() gets triggered.

 js/services.js

First we need to fetch the data from the server. To facilitate communication with HTTP servers, AngularJS provides the $http and $q service.  When we make an asynchronous ajax call, we need to wait for the response which is then used in the view.  So it is important to wait for the response before we push in the view. $q promises to do that.

js/controllers.js

Now its time to hook our controller. The UserController is passed with  parameters – $scope,  UserService and $routeParams.

The $scope variable is supposed to link your controller and views. In particular, it holds all the data that will be used within your template.

We pass UserService to receive the data from the ajax call. We create this in services.js

$routeParams allows you to receive the current set of URL parameters.

js/app.js

app.js hooks up everything together. We pass our controllers and services in this.

ngRoute is a dependency lib to get information about the url parameters.  This is required by $routeProvider to work.

The $routeProvider is responsible to get the appropriate view inside the ng-view directive, defined in index.html

If the url is something like http://localhost/angulardemo/#/add, then it will push the addUser.html view and link it with the controller AddUserController.

You can download the source code here.

Conclusion:

It is little difficult to explain each and every line but with little bit of javascript knowledge, few of the stuff can be figured out. In the demo, i have used twitter bootstrap to make the UI look nice.

Finally, remember that AngularJS is really powerful in what it does and we have barely touched the surface of it.

If you have any queries in following up with this post, comment !

Webblog of Abhishek Saha