ioc introduction

13
SharePoint Patterns & Practices IoC, DI, Service Locator Ha Doan

Upload: ha-manh

Post on 27-Jan-2015

107 views

Category:

Technology


3 download

DESCRIPTION

Inversion of control, dependency injection, service locator

TRANSCRIPT

Page 1: IoC introduction

SharePoint Patterns & Practices

IoC, DI, Service LocatorHa Doan

Page 2: IoC introduction

Agenda

1. Inversion of Control (IoC) 2. Dependency Injection (DI)3. Service Locator4. Discussion5. Demo6. Service Locator in Sharepont 20107. Service Locator in Domea

Page 3: IoC introduction

• The main control of the program was inverted, moved away from you to the framework.

• In Console app:print "enter your name"read nameprint "enter your address"read addressetc...store in database

Inversion of Control (IoC)

Page 4: IoC introduction

• GUI Appwhen the user types in field a, store it in NAMEwhen the user types in field b, store it in ADDRESSwhen the user clicks the save button, call StoreInDatabase

=> Control is inverted

IoC

Page 5: IoC introduction

IoC

IoC

Page 6: IoC introduction

IoC

Page 7: IoC introduction

DI

• DI is a subtype of IOC and is implemented by constructor injection, setter injection or method injection

Page 8: IoC introduction

Service Locator

The service locator pattern is a design pattern used in software development to encapsulate the processes involved in obtaining a service with a strong abstraction layer

Page 9: IoC introduction

DEMO

1. Setup IoC Container2. Register services (components)3. Using components

Page 10: IoC introduction

Discusstion: Why DI & Service Locator????

Page 11: IoC introduction

• Namespace: Microsoft.Practices.SharePoint.Common.dll

• Get service locator:IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent();

• Add service:var typeMappings = serviceLocator.GetInstance<IServiceLocatorConfig>();typeMappings.RegisterTypeMapping<IService1, Service1>();

• Get serviceIService1 service1 = serviceLocator.GetInstance<IService1>();

Sharepoint Service Locator

Page 12: IoC introduction

Questions

Page 13: IoC introduction

Thank you!