Presentation is loading. Please wait.

Presentation is loading. Please wait.

Welcome to CODE SPREAD Simple Concepts of Coding | Programming.

Similar presentations


Presentation on theme: "Welcome to CODE SPREAD Simple Concepts of Coding | Programming."— Presentation transcript:

1 Welcome to CODE SPREAD Simple Concepts of Coding | Programming

2 DI is Dependency Injection. It is defined as Dependency injection is a software design pattern that allows removing hard-coded dependencies and making it possible to change them, whether at run-time or compile- time.. There is one major point we should always remember, Inversion of Control is a principle and Dependency Injection is the implementation

3 Q: How DI can be implemented? A: In Dependency Injection each component must declare a list of dependencies required to perform the task. At run-time a special component (generally) called an IoC Container performs binding between these components.

4 Q: How DI can be implemented? (contd.) A: There are three ways to implement Dependency Injection. Constructor Injection Setter Injection Interface-based injection

5 Example: We define an interface first and the classes which inherits this interface Interface IAccount { //Definition } public class SavingsAccount : IAccount { //Implementation } public class CurrentAccount : IAccount { //implementation }

6 Constructor Injection : We define a class CalculateInterest and Interface define above will be passed to the constructor of the CalculateInterest class. public class CalculateInterest { private IAccount account; public CalculateInterest(IAccount account) { this.account=account; } }

7 Constructor Injection : (contd.) Next, we will check our code. IAccount savingsAccount = new SavingsAccount(); IAccount currentAccount = new CurrentAccount(); CalculateInterest calculateSavingsInterest = new CalculateInterest(savingsAccount); // when using CalculateInterest class for SavingsAccount CalculateInterest calculateCurrentInterest = new CalculateInterest(currentAccount); // when using CalculateInterest class for CurrentAccount

8 Constructor Injection : (contd.) Drawback One of the major drawback of constructor injection is that once the class is instantiated, it is difficult to change the objects dependency This drawback is corrected in setter injection.

9 Setter Injection : To resolve problem faced in constructor injection, We will not pass the interface as parameter but we will assign it to a property of the class.

10 Setter Injection : (contd.) Create public property of Interface in the CalculateInterest class. public class CalculateInterest { private IAccount _account; public IAccount Account { get { return _account; } set { _account= value; }

11 Setter Injection : (contd.) Now instantiate the class and assign the required property. IAccount savingsAccount = new SavingsAccount(); IAccount currentAccount = new CurrentAccount(); CalculateInterest calculateInterest = new CalculateInterest(); calculateInterest.Account = savingsAccount; // when using CalculateInterest class for SavingsAccount calculateInterest.Account = currentAccount; // when using CalculateInterest class for CurrentAccount

12 Setter Injection : (contd.) Its quite clear and provides more flexibility even after creation of the object.

13 Interface Injection: We can provide more flexibility by creating a method in the class CalculateInterest which will have interface as a parameter. public class CalculateInterest { private IAccount account; public SetAccount(IAccount account) { this.account=account; } }

14 Interface Injection: (contd.) Lets see the call also. IAccount savingsAccount = new SavingsAccount(); IAccount currentAccount = new CurrentAccount(); CalculateInterest calculateInterest = new CalculateInterest(); calculateInterest.SetAccount(savingsAccount); // when using CalculateInterest class for SavingsAccount calculateInterest.SetAccount(currentAccount); // when using CalculateInterest class for CurrentAccount

15 There is one more way to apply IoC, Service Locator. Service Locator: In Service Locator, dependency is not injected through any medium but retrieved through the medium of a static method.

16 Service Locator: (contd.) Declare a static class and a static method inside it. static class AccountType { public static IAccount getAccountType() { //Implementation }

17 Service Locator: (contd.) Now call this method from your class public class CalculateInterest { private IAccount account; public CalculateInterest(IAccount account) { this.account=AccountType.getAccountType(); } }

18 We have learned basic concepts, importance of DI and ways of implementation. I hope after viewing this article, many complexities revolving around DI and IOC will get resolved. Please mail your comments or articles at admin@codespread.comadmin@codespread.com

19 Thanks!! CODE SPREAD Simple Concepts of Coding | Programming


Download ppt "Welcome to CODE SPREAD Simple Concepts of Coding | Programming."

Similar presentations


Ads by Google