Difference between the Inversion of Control(IOC) and DI (Dependency Injection) : The main goal of IOC and DI is removed the dependencies of your application and make it's loosely coupled. Let's Understand IOC using the Simple mailing Application work flow.
1.) End User sends the mail.
2). Applications(mail client) waiting for mail on the other end.
3). If no mail found then goes to step 2 other wise go to step 4.
4). Display the mail.
5). User work is go ahead.
Here the flow is the Sequential. The Program control itself. Now Using the IOC the Mail program delegates the Controller someone else who drive the flow. In our mail application if we make it's event base then our application flow like that.
1). End User sends the mail.
2). User work go ahead.
3). Application mail event activated then it fires and mail will be displayed.
Now application flow is not sequential but it's now event based. So the control is inverted. Now now rather than the controlling flow from the internal program it controlled by the event.You can delegate the control flow by the event, callback delegate,observer pattern and DI many other ways . IOC is general parent term
while the DI is subset of the IOC. Using the IOC concept you can inverted the flow of you application.
DI provide the object which they needs.So rather than the dependencies create themself they are injected by external sources.Here below example the Customer class want to the log4 dependencies to log the error. So rather then create in the class we injected via constructor.So using that approch we can achive the
decoupling in our application.
Inversion of control :- It’s a generic term and implemented in several ways (events, delegates etc).
Dependency injection :- DI is a subtype of IOC and is implemented by constructor injection, setter injection or method injection.
No comments:
Post a Comment