Dependency Injection in C# .NET

Dependency injection basically allows us to create loosely coupled, reusable, and testable objects in your software designs by removing dependencies.

We will take a look into the Object dependencies before digging in more.
Consider a scenario of fetching an employee details and show display in UI. Let us say create a Business logic layer class named EmployeeBAL and a data access layer class named EmployeeDAO

public class EmployeeDao
{
//Some code
}
public class EmployeeBAL
{
var employeeDAO = new EmployeeDao();
//Some code
}

From the above code you will notice one thing that we are creating EmployeeDAO instance inside the Business logic layer class. So here comes the dependency

What is wrong if we have a dependency?

Think about whether your code is unit testable. We cannot fully unit test the EmployeeBAL as it has a dependency on Employee DAO. So we can say as long as the composition of the DAO exists within the BAL we cannot unit test the EmployeeBAL.

You will also notice one more thing here; with this type of implementation you will see a high coupling of BAL and DAL.

How to make it loose coupling?

The basic idea behind Dependency Injection is that you should isolate the implementation of an object from the construction of objects on which it depends.

Coming to the example, we should be isolating the implementation of EmployeeBAL object and the construction of the dependent EmployeeDAO object.

We will see how we can make loosely coupled objects in detail

Constructor based dependency injection
We will have to modify the EmployeeBAL to accept an EmployeeDAO instance within its constructor.

public class EmployeeDao
{
//Some code
}

public class EmployeeBAL
{
EmployeeDao employeeDAO;
public EmployeeBAL(EmployeeDAO employeeDao){
this.employeeDAO = employeeDao;
}
//Some code
}

Property based dependency injection

With property based injection we will have a public getter and setter Property of type EmployeeDao so that the dependency can be externally set.
public class EmployeeBAL
{
Public EmployeeDao EmployeeDataAccess{ get; set; }
}
var employeeBAL = new EmployeeBAL();
EmployeeBAL.EmployeeDataAccess = new EmployeeDao();

Wait!!!

The above ones are just some techniques of injecting the dependency. We are still yet to discuss one more interesting thing Unit Testing.

Are you agreeing that we have removed the DAO creation from the Business Logic EmployeeBAL? Yes it is good but it still depends on the actual instance of EmployeeDao.

Consider the below mentioned implementation of the same sample senarios

interface IDataAccess
{
//Some code
}

class EmployeeDao : IDataAccess
{
//Some code
}

public class EmployeeBAL
{
private IDataAccess dataAccess;
public BusinessFacade(IDataAccess dao)
{
  dataAccess = dao;
}
}

You can notice we are doing a constructor dependency injection but most important thing here
is we are using Interface type than creating a strongly typed object.

The advantage that we are getting here is we can have an in memory data access object of
IDataAccess interface type and we can easily inject the dependency to the EmployeeBAL.

By this way we no need to have the actual database dependency.

Are you happy that we can unit test the BAL without the data access dependency?

Advantages of Dependency Injection

The primary advantages of dependency injection are:
Loose coupling
Centralized configuration
Easily testable

Another Post about DI: Understand Constructor Injection in Dependency Injection.

I found 1 more Link Usable-
Design pattern Inversion of control and Dependency

66 comments:

  1. The future of software testing is on positive note. It offers huge career prospects for talented professionals to be skilled software testers. Best software testing training institute in Chennai | Software Testing Training in Chennai | Software testing course in Chennai

    ReplyDelete
  2. Excellent post!!! Your article helped to under the future of java development. Being an open source platform, java is integrated in most of the software development industries to create rich featured applications. J2EE Training in Chennai | JAVA Training in Chennai

    ReplyDelete
  3. Everyone wants to get unique place in the IT industry’s for that you need to upgrade your skills, your blog helps me improvise my skill set to get good career, keep sharing your thoughts with us.
    Regards,
    sas training in Chennai|sas course in Chennai|sas training institute in Chennai

    ReplyDelete
  4. Thanks for sharing such informative guide on .Net technology. This post gives me detailed information about the .net technology. I am working as trainer in leading IT training academy offering Dot Net Training in Chennai and i use your guide to educate my students. .Net Training in Chennai

    ReplyDelete
  5. very informative blog. Helps to gain knowledge about new concepts and techniques. Thanks for posting information in this blog
    Dotnet Training in Chennai

    ReplyDelete
  6. i need more clear cut concept in that topic it is really useful for many persons.


    software testing training in chennai


    ReplyDelete
  7. This blog is really informative. I gained more knowledge by reading your post. Thanks for providing us the useful information with us. Your blog is really helpful for me in attaining my knowledge in a right way.

    SAS Training in Chennai

    ReplyDelete
  8. Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

    iOS Training in Chennai

    ReplyDelete
  9. Nice post. I have been reading a lot of stuff on this topic in the last few months, but this article stands out with its simplicity & authenticity. Every passage made profound sense. Thanks a lot for this

    ReplyDelete
  10. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharng this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete

  11. You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...

    Android App Development Company

    ReplyDelete
  12. You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...
    iOS App Development Company
    iOS App Development Company

    ReplyDelete
  13. great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
    web design Company
    web development Company
    web design Company in chennai
    web development Company in chennai
    web design Company in India
    web development Company in India

    ReplyDelete
  14. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving..
    Fitness SMS
    Fitness Text
    Salon SMS
    Salon Text
    Investor Relation SMS
    Investor Relation Text

    ReplyDelete
  15. it is really amazing...thanks for sharing....provide more useful information...
    Mobile app development company

    ReplyDelete
  16. Pretty article! I found some useful information in your blog, thanks for sharing this great content to my vision, keep sharing.. Software Testing Training in Chennai | Dot Net Training in Chennai | Web Design Training in Chennai

    ReplyDelete
  17. This article is very much helpful and i hope this will be an useful information for the needed one. Keep on updating these kinds of informative things...
    Texting API
    Text message marketing
    Digital Mobile Marketing
    Sms API
    Sms marketing

    ReplyDelete
  18. Nice Article Thanks for Shearing this Post,It is useful for me.
    sap training institute in gurgaon

    ReplyDelete
  19. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information, this is useful to me…
    Java Training Institute in Chennai | DotNet Training Institute in Chennai | Web Designing Training Institute in Chennai

    ReplyDelete
  20. I really love reading and following your post as I find them extremely informative and interesting. Java Training Center in Chennai | Best J2EE Training Center in Chennai | No.1 Java Training Institution in Velachery

    ReplyDelete
  21. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well

    PSD to Wordpress
    wordpress website development

    ReplyDelete
  22. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
    data-science-training-in-bangalore

    ReplyDelete
  23. Thanks for sharing such Valuable information on .Net technology. This post gives me detailed information about the .net technology.
    .net Training

    ReplyDelete
  24. Nice blog about .net technology. Helps to gain knowledge on .net learning new things. Thanks a lot
    ASP.net MVC Training

    ReplyDelete
  25. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us.
    Java Training in Chennai | Web Designing Training Institute in Chennai | DotNet Training Institute in Chennai

    ReplyDelete

  26. Impressive blog with lovely information. really very useful article for us thanks for sharing such a wonderful blog... 
    No.1 IOS Training Institute in Velachery | Best Android Training Institute in Velachery | Core Java Training Institute in Chennai

    ReplyDelete
  27. Extraordinary blog. you put Good stuff. All the themes were clarified briefly.Thanks for sharing that post.Keep in blogging...No.1 IOS Training Institute in Velachery | Best Android Training Institute in Velachery | Core Java Training Institute in Chennai

    ReplyDelete
  28. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
    hadoop-training-institute-in-chennai

    ReplyDelete
  29. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
    aws training in Chennai

    ReplyDelete
  30. Those guidelines additionally worked to become a good way to
    recognize that other people online have the identical fervor like mine
    to grasp great deal more around this condition.

    white label website builder

    ReplyDelete
  31. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.

    Hadoop Training institute in Bangalore

    ReplyDelete
  32. I read this article. I think You put a lot of effort to create this article. I appreciate your work.
    Dissertation Writing Services

    ReplyDelete
  33. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
    R Programming Training in Chennai
    R Programming

    ReplyDelete
  34. Thanks for sharing such informative on .Net technology. This post gives me unique information about the .net technology. I am working developer nareshit in Hyderabad Python training in Chennai

    ReplyDelete
  35. Thanks For Sharing Such A valuable Information About The C#.Net Technology.and Provide More Useful Information.

    ReplyDelete
  36. You Provided Very Nice Post About C#.Net Technology Thankyou For Sharing This One..i Learned More To this Post.

    Best Software Training Institute in Chennai

    Best Online Training Institute in Chennai

    ReplyDelete
  37. Thanks for sharing informative blog, it's very useful
    OnlineTraining

    ReplyDelete
  38. Thanks a lot for providing valuable information,i appreciate your work keep sharing.
    https://nareshit.in/software-training-institute-bangalore/

    ReplyDelete
  39. We ar e the Best Software Training Institute in Bangalore with real time examples training by real-time experts

    ReplyDelete
  40. Thanks for sharing the information,keep on updating the content. Best software Training institute in Bangalore

    ReplyDelete
  41. Thanks for sharing the useful content and would like to read this blog regularly to get more stuff from this area.
    Selenium Training in Chennai | Selenium Online Training in Chennai

    ReplyDelete
  42. Thank you for sharing information, this content is very useful to us, please share more info regularly Best Software Training in Chennai

    ReplyDelete
  43. This comment has been removed by the author.

    ReplyDelete
  44. Thanks for sharing the useful content and would like to read this blog regularly to get more stuff from this area.
    Power Bi Online Training

    ReplyDelete
  45. Thanks in favor of sharing such a nice thought, post is pleasant, thats why I have read it completely
    Informatica Online Training

    ReplyDelete
  46. Very nice write-up. I definitely appreciate this website.Keep writing!
    MSBI Training Classes

    ReplyDelete
  47. Populated with sparkling lakes and also rivers, this location of Ireland is a real boaters’ heaven
    Mysql Training Classes

    ReplyDelete
  48. Interested in details on online casinos? Come to us, here is all in full details. Top online casinos in the world Best casino only at BGAOC.

    ReplyDelete

Topics

ADO .Net (2) Ajax (1) Angular Js (17) Angular2 (27) ASP .Net (14) Azure (1) Breeze.js (1) C# (49) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DI (3) Dotnet (22) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (8) Lamda (3) Linq (11) Mongodb (1) MVC (50) NodeJS (7) RDLC (1) Report (1) Sql Server (29) SSIS (3) SSRS (2) UI (1) WCF (13) Web Api (11) Web Service (1) XMl (1)