Dependency Injection & Inversion of Control

Presenter: Trung Nguyen Thanh Duration: 2 hours

Agenda
IoC and DI Ways for us Some DI techs

Solution .The principles of IoC .Issue .IoC and DI .Ways of implement IoC Constructor Setter and Getter Interface based DI Service Locator .

L.Issue How to Build Better Software ? + Loose coupling + S. DIP) +« . OCP. ISP.I.O. LSP.D principles (SRP.

Issue The biggest issue is tight coupling between classes .

.Solution The solution definitely revolves around shifting the object creation control from the customer class to someone else.

an xml configuration file).What are IoC & DI http://msdn. Instead. .com/en-us/library/aa973811.aspx Inversion of Control (IoC) means that objects do not create other objects on which they rely to do their work. they get the objects that they need from an outside source (for example.microsoft.

What are IoC & DI http://msdn. usually by a framework component that passes constructor parameters and set properties.aspx Dependency Injection (DI) means that this is done without the object intervention. .com/en-us/library/aa973811.microsoft.

What are IoC & DI http://en.org/wiki/Inversion_of_control http://en.wikipedia.org/wiki/Dependency_injection .wikipedia.

‡ Abstraction should not depend on details.The principles of IoC ‡ Main classes aggregating other classes should not depend on the direct implementation of the aggregated classes. . They should depend on abstraction by using interface or abstract class. details should on abstraction.

Ways of implement IoC .

Constructor .

Constructor Good: decoupling between classes. . Bad: this solution is not suited for the client who can only use default constructor.

Setter and Getter .

Can use while client only uses default constructor Bad: We use property then it can break the encapsulation rule of object oriented programming.Setter and Getter Good: decoupling between classes. .

Interface based DI .

Interface based DI We will implement an interface from IoC framework. Good: resolve issues of two solutions above. Bad: we must define new interface based DI . IoC framework will use the interface method to inject the object in the main class.

Service Locator .

The service locator class does not create instances of the child object. This is a good solution. it provides a methodology to register and find the services which help in creating objects. .Service Locator We will use service locator to obtain instance of the child object.

‡ Create a child which implement interface above .Ways for us ‡ Create an interface which defines commons behaviors that a child must has.

Ways for us .

this member will locate our child object ‡ And in parent. we will define a child member of interface about. we will pass the object through the constructor.Ways for us ‡ In parent. .

Ways for us .

Some DI techs Unity Castle Windsor StructureMap Ninject Atutofac « .

Unity ‡ Web site: http://unity. ‡ Code initialization is easy .May 2011 released ‡ Initialization code and XML. Nice library. Great library for Microsoft or the highway shops.codeplex.com/ ‡ Current version: Unity 2.1 For Desktop and Silverlight . but XML configuration is a pain in the butt.

Unity Without configuration file .

Unity With configuration file .

Demo .

Question? .