Sunday, May 27, 2018

Git Merge Vs Rebase

I have used the below steps to test Git Merge and Git Rebase.

1) Init Master,
2) Init Branch.
3) Commit "1", in Branch. . 
4) Commit "2"  in Master. 
5) Commit "3" in Branch. 

Git Merge:  

"Your repository’s commit history is a record of what actually happened."

git checkout mybranch
git merge master 


Note in the above diagram, I have merge the master into branch.


Note that, the second commit is applied after the third commit in my branch, and a new merge commit is created.


Git Rebase:  

"The repository's commit history is the story of how your project was made."

git checkout mybranch
git rebase master. 

Here I have rebased mybranch onto master

Now here instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch.




See that all the branch history is applied on top of master log history.


Way to use

In general the way to get the best of both worlds is to rebase local changes you’ve made but haven’t shared yet before you push them in order to clean up your story, but never rebase anything you’ve pushed somewhere.

Wednesday, May 9, 2018

Dependency Injection, Field, Setter, or Constructor Injection

I was doing some investigation on the different approaches of Dependency Injection in the Spring Framework. Following is a summary of content that I gathered from various websites.


Inversion of Control (IOC)

IoC enables a framework code,  to take control of the flow of a program and make calls to our custom code.


Dependency Injection

Dependency injection is a pattern through which to implement IoC.

The act of connecting objects with other objects, or rather “injecting” objects into other objects, is done by a the framework code  rather than by the objects themselves


Field Injection


Using field injection makes it hard to test , because we do not have a way to inject our dependency into the class we are testing, and we would have to rely on the IOC container to inject this dependency for us.


One of the purposes of Dependency Injection is to decouple code, which in turn should make the code easier to test.

However, it is worthwhile noting that field injection makes the code more elegant than setter or constructor injection.


Setter Injection


In this approach, we can not guarantee that our class would have injected the dependency. However note that, It is good to use setter injection when the dependencies are optional.




This blog Repeat After Me: Setter Injection is a Symptom of Design Problems explains further the disadvantages of using setter injection. 

Another blog explaining why setter injection is not a good idea is Setter injection sucks.

I came across this blog: Constructor vs Setter Injection, happen to explain why setter injection is preferred over constructor injection.


Constructor Injection

Constructor Injection guarantees that the dependency is injected during class creation.





This blog: One correct way to do dependency injection explains why constructor injection is a better option over setter injection.