Fundamentals of Dependency Injection and popular libraries in Android
Dependency
injection is quite interesting topic and to understand it perfectly it is very
important to learn the basics of it and learn it in depth. So I am planning to explain
it in two parts.
This post will
explain what dependency injection is, its benefit and different popular DI
options which we can use in our Android projects. It seems little theoretical
to you to grasp the basics. So let’s gets started…
“With good basics you’ll have endless
options”
What is DI?
Any business
application is made up of two or more classes and these classes collaborate
with each other to perform some operation. Traditionally, we create the instance
of dependent class object and do the required operations. When applying Dependency
Injection, the objects are given their dependencies at creation time by some
external entity that coordinates each object in the system. It means dependencies
are injected into objects.
In other
words, Dependency injection is a style of object creation in which an objects
are created by an external entity, or technique whereby one object supplies the
dependencies of another object.
It is built
upon the concept of Inversion of Control
which says “Rather than low level code calling up to high level code, high
level code can receive lower level code that it can call down to. This inverts
the typical control pattern seen in procedural programming.”
How DI works?
Most
dependency injectors rely on reflection to create and inject dependencies. Reflection is awesome but is very slow
and time consuming. Also it perform dependency resolution at runtime which leads
unexpected errors and crashes the application.
On the other
hand some injectors uses a Pre-compiler
that creates all the classes (object graph) it needs to work using Annotation Processor.
An Annotation Processor is a way to read the compiled files during build time
to generate source code files to be used in the project. So it perform the
dependency resolution before the application runs and avoid unexpected errors.
Dependency Injection Modes
There are
three common means for a client to accept a dependency injection:
· Constructor based injection
· Setter method
· Interface
Benefits of DI
Dependency
Injection reduces to write boiler plate code and make the development process
smooth. Implementing proper dependency injection in our apps allows us to have:
· Loose coupling
· Easily testable code
· Code reusability
Popular Libraries comparison
The
available popular dependency injection libraries for Android are:
· RoboGuice
· ButterKnife
· Dagger
· Android Annotation
Let’s
compare above dependency injection libraries
Roboguice
injects code at runtime using reflection. Using RoboGuice, we can inject Views,
Drawable, Resources, System Service, or any other objects. Because of using
reflection it is slow.
Traditional
code:
TextView textView = (TextView)
findViewById(R.id.textView);
Roboguice
inject view code:
@InjectView(R.id.textView) TextView
textView;
Butterknife
uses annotation processing to generate modified Java classes based on
annotations. It allows to annotate the views and OnClickListeners. Butterknife
also includes findById methods which
simplify code that still has to find views on a View, Activity, or Dialog.
Traditional
code:
TextView textView = (TextView)
findViewById(R.id.textView);
Butterknife code:
@BindView(R.id.
textView) TextView textView;
Traditional
code:
button.setOnClickListener(new
OnClickListener(){
@override
public void
onClick(View view){
}
})
Butterknife
code:
@OnClick(R.id.button)
Dagger is
designed for low-end devices. It is based on the Java Specification Request
(JSR) 330. It uses code generation and is based on annotations. The generated
code is very relatively easy to read and debug.
Dagger 2
uses the following annotations:
- @Module and @Provides:
define classes and methods which provide dependencies
- @Inject: request dependencies.
Can be used on a constructor, a field, or a method
- @Component: enable selected
modules and used for performing dependency injection
Conclusion
Using dependency
injection framework may be attractive because it simplify the code we write and
also provide an adaptive environment that's useful for testing and other
configuration changes.
RoboGuice is
a cool idea in theory and very powerful. It does a lot of the same things
Butterknife can do but it does them at runtime using reflection. This causes a
performance impact.
Dagger and Butterknife
seems to handle both the dependency inject and view injection with less of a
performance impact.
On next post
we will get some practical and coding work.
Stay tuned
for upcoming articles. For any quires or suggestions, feel free to hit me on Twitter Google+
To find more
interesting topics on Software development follow me at Medium.
Gexton Provides the Best web development in Hyderabad.
ReplyDeleteI agree with this, no wifi games free are being loved by many phone gamers.
ReplyDeleteNice information... Keep update more.
ReplyDeleteAndroid Training in velachery
This is awesome. Hai Author Good Information that i found here,do not stop sharing and Please keep updating us..... Thanks.
ReplyDeleteHire android developers
android development company
Your device has become leggy and freezes all the time and you don't have enough space to take more pictures or install apps or your battery has started draining quicker than ever and your device overheats and needs to cool down then this app works for you in a perfect and great manner.
ReplyDeleteAndroid Cleaner and Booster
Phone Clean
Clean Master
Thanks for sharing. Keep sharing
ReplyDeleteThanks For Sharing . It IS very helpful For Everyone ....
ReplyDeleteIf You Are Looking Best Android training in Chandigarh click here
very interesting information, thanks for sharing with us this topic.
ReplyDeleteAndroid training in Chandigarh
Android and IOS Application: The professionals working in this company make sure to design user-friendly app and with creative designs.
ReplyDeletehttp://omnisttechhub.com/app-development/
This information you provided in the blog that is really unique I love it!! Thanks for sharing such a great blog Keep posting..
ReplyDeleteAndroid training in Noida
nice post..thanks for sharing..
ReplyDeleteweb hosting company in chennai
website hosting chennai
website hosting in chennai
web hosting services in chennai
cheap web hosting
Nice post, I really like the content posted by you. Thank you for sharing these details here with us.
ReplyDeleteWeb Design Company | Web development Lucknow
Thanks,,,
ReplyDeleteyour blog is awesome,,
Best Software Training Centre in Chennai | Software Training Centre in Chennai
Mobile App Development.
ReplyDeleteWeb Development.
Web Design Company.
Web Services.
IOS Development.
Superb Blog. Thank You hadoop big data classes in pune
ReplyDeletevery nice post and blog are very informative thanks for sharing this post. it's really very helpful.
ReplyDeleteAndroid training in Chandigarh
Its a good informative article.
ReplyDeleteFind out all the info regarding android
Androiddose
Very useful blog, Thank you for sharing this information
ReplyDeleteAndroid App Development Company: https://www.biztechcs.com/android-application-development/
Nice blog..it was so informative..Thanks for sharing..keep update selenium training in chennai
ReplyDeleteselenium training in velachery
Learned a lot from your blog. Good creation and hats off to the creativity of your mind. Share more like this.
ReplyDeleteBlue Prism Training Institute in Chennai
Blue Prism course in Chennai
Blue Prism Training in Adyar
RPA Training in Chennai
RPA courses in Chennai
UiPath Training in Chennai
A great deal of informative and advantageous information imparted by you. This might benefit many seekers. One can learn and speak English in an effective way, just by downloading English Learning App on your own smartphone, which you can use whenever and wherever you want to practice your communication skills with experts.
ReplyDeleteEnglish with experts | English Speaking App
I would like to thank you much about this great post. Its very useful and helpful for anyone looking for tips to help him learn and master in Angularjs. I like your writing style and I hope you will keep doing this good working.
ReplyDeleteAngularjs Coaching in Bangalore
Angularjs Institute in Bangalore
Best Angularjs Training in Bangalore
Best Android Training in Bangalore
Android Developer Course in Bangalore
Android App Development Training in Bangalore
Awesome post. Really you are shared very informative concept... Thank you for sharing. Keep on updating…
ReplyDeleteArticle submission sites
Education
good work done and keep update more.i like your information's and
ReplyDeletethat is very much useful for readers.
Angularjs Training Bangalore
Angularjs courses in Bangalore
AngularJS Training in Ambattur
AngularJS Training in Guindy
nice post..
ReplyDeleteerp training institute in chennai
erp training in chennai
tally erp 9 training in chennai
tally erp 9 training institutes
Thanks for information , This is very useful for me.
ReplyDeleteKeep sharing Lean Six Sigma Green Belt Training Bangalore
Thanks.Nice explanation
ReplyDeleteiWatch service center chennai | apple iphone service center in chennai | Mobile service center in chennai | apple service center chennai | apple iphone service center in chennai | iphone service chennai
" Write more; that’s all I have to say. It seems as though you relied on the video to make your point. You know what you’re talking about, why waste your intelligence on just posting videos to your blog when you could be giving us something enlightening to read?
ReplyDelete"
Authorized iphone service center in Chennai | iphone service center in chennai | Mobile service center in chennai | Authorized iphone service center in Chennai | iphone service center in chennai
Good Post. I like your blog. Thanks for Sharing
ReplyDeleteJava course in Delhi
Incredible writing. Fantabulous Post. Thanks for sharing.
ReplyDeleteXamarin Training in Chennai
Xamarin Course in Chennai
Xamarin Training Institutes in Chennai
Xamarin Training
Xamarin Course
Xamarin Training Course
Xamarin Training in Anna Nagar
Xamarin Training in Tnagar
Great work. It shows your in-depth knowledge. Your article is very thought-provoking.
ReplyDeleteNode JS Training in Chennai
Node JS Course in Chennai
Node JS Training Institutes in chennai
Node JS Course
Node JS Training in T Nagar
Node JS Training in Anna Nagar
Node JS Training in Porur
Node JS Training in Adyar
This comment has been removed by the author.
ReplyDeleteReally Liked the information you have provided. I have an article relaed to it. I was searching about it on the internet and I found an amazing site on a Profile. The provided Article was about a site that provides working modded android apps. The name of the site was “Fineapkapps”. The Article was very halpful, You should read that. Click Here to reach that amazing article: New Modded Apps.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThanks for sharing this valuable information and we collected some information from this post.
ReplyDeleteCorporate training in Machine learning
The Web Design Company in Dubai helps in checking the local intent query and uses the approach for ranking the website in the popular search engines like Google, Yahoo, etc. by finding the keywords. Simply by typing the keywords in the tool, one can come to know about the search volume.
ReplyDeleteVisit Visit Website www.wscentre.com
I Remmber that it is the very nice effective post about blog. I am searching this type of post then I got finally in this post. Anyone can check Best Clipping path Service and Clipping path servicedetails.Thanks for You Sharing.
ReplyDeleteIt is a very nice blog. Really it is a very international destination post. Thanks for sharing this post.
ReplyDeleteWebsite development company in Bangladesh
ReplyDeleteThanks for sharing such a good content with us. keep share these kind of content.i would like to read more.
Android Course in Delhi
This is very good content you share on this blog. it's very informative and provide me future related information.
ReplyDeleteArtificial Intelligence Certification Course
Java Certification Course
AWS Certification Course
Machine Learning Certification Course
Data Science Certification Course
DevOps Certification Course
Students अपने विश्वविद्यालयों के Results के बारे में नवीनतम अपडेट की जाँच करें। Timetable-Result.com यह साइट आपकी सभी क्वेरी को समझने में मदद करेगी है।
ReplyDeleteBA Exam result Part 1st, 2nd, Final Year
BCom Exam result 1st, 2nd, 3rd semester
I love the way you write and share your niche! Very interesting and different! Keep it coming!
ReplyDeleteWeb Design Gloucester
SEO Gloucester
Hydrogen Fun Facts
ReplyDeleteHydrogen -
The Most Abundant Element of the Universe. The hydrogen atom is the first and simplest atom, containing only a single proton and electron. Atomic mass: 1 u.This is also one of the fun facts about hydrogen.
Hey there! Very good article.
ReplyDelete🚀 Unlock financial freedom with Money Back Term Insurance! 💰 It's not just insurance; it's your key to security and flexibility. 🌟 Explore the path to a brighter financial future! #MoneyBackInsurance
Nice post, I really like the content posted by you. Thank you for sharing these details here with us.
ReplyDeleteManjummel Boys" captivates with its heartfelt portrayal of friendship amidst the vibrant culture of Kerala. The film delves into the complexities of youth, weaving a narrative rich in emotion and authenticity. With stellar performances and a compelling storyline, it leaves a lasting impression, resonating with audiences long after the credits roll.
This comment has been removed by the author.
ReplyDelete🚗💥 Who Pays in a Car Accident? 💥🚗
ReplyDeleteUnderstand who pays for damages after an accident based on Car Accident insurance, state laws, and fault determination. Stay informed and drive safe! #CarAccident #AutoInsurance #InsuranceTips #SafeDriving
🔹 Types of Insurance
🔹 Fault Determination
🔹 Special Cases
🔹 What to Do After an Accident
"This was really helpful ,I completely agree with your pespective. Great job.
ReplyDeleteOnline Data Science Course