dependency injection

38
Dependency Injection Подход к архитектуре объектно- ориентированных систем Андрей Кулешов Деловые решения

Upload: getdevnet

Post on 22-May-2015

3.050 views

Category:

Technology


1 download

DESCRIPTION

Видеозапись: http://getdev.net/Event/dependency-injection Рассказ о внедрении зависимостей (Dependency Injection), зачем оно нужно, откуда оно пошло и развивалось. Виды Dependency Injection, разница между ними и рекомендации к применению. Расказ про декораторы (decorators). Рассказ про абстрактные фабрики. Рассказ про управление временем жизни. Рассказ про IoC -контейнеры вообще и Castle Windsor в частности.

TRANSCRIPT

  • 1.

2. ? - - IoC- 3. - - - , , - - , , , . , -, . . 4. , ( ), , , 5. (dependency) - , , , .. : - ; - - ; - , ; - . , 6. : - IDbConnection connection = new SqlServerConnection(source=localhost); , , , , , , , , Sql Server, , 7. 8. Inversion of Control : IoC public class Basket{ public Basket(IDbConnection connection){ //save connection... } } - -, . 9. DI DI DI DI DI IoC 10. , , (Service Locator) Dependency Injection -, , 11. 12. ? . , . NullReferenceException : (constructor injection) (property injection) (methods injection) (ambient context) 13. Constructor Injection , public class GreetingsManager{private readonly IWriter _writer; public GreetingsManager(IWriter writer) { if(writer == null) throw new ArgumentNullException("writer", "Writer was not provided"); _writer = writer; }......} 14. Constructor Injection 15. Property Injection public class GreetingsManager { private IWriter _writer = null; , public IWriter Writer { get { 90% if(_writer == null) , { _writer = new ConsoleWriter(); } , return _writer; NullObject }, - set , { if(_writer != null) throw new( InvalidOperationException( GreetingsManager "You cant change writerConsoleWriter implementation after usage");. ) _writer = value; } } 16. Property Injection 17. Method Injection , , (, ) , public string RunPlugin(SomeValue value, ISomeContext context) { if (context == null) { throw new ArgumentNullException("context"); } return context.Name; } 18. Method Injection 19. Ambient Context , ( ) Ambient Context ( ) 20. Ambient Context 21. , , , , 22. Open/Closed , , , , . 23. ? , (Composition Root) , , ASP.NET MVC ControllerFactory 24. Composition Root 25. Lifestyle Singletone , Transient PerThread Pooled , PerWebRequest , Web-PerGraph Scoped LazyTransient, , 26. IoC , IoC 99% , , IoC 27. IoC- ?, AutoFac Castle Windsor Dynamo.Ioc Funq Hiro LightCore LightInject LinFu Munq Ninject Petite Simple Injector Spring.NET StructureMap TinyIoc Unity 28. Castle Windsor IoC- .NET 3.0.4001 (FluentAPI), XML (Installer), Interception - 29. 30. (Interception) cross-cuttingconcerns (, , , ..) , , 31. Interception Castle Windsor 32. Castle Windsor Interface-based Factories Castle Windsor Delegate-based Factories Func, 33. Castle Windsor 34. Constructor Over-Injection public HomeController(IBookRepository rep,ICurrentUser user,ICurrencyFormatter formatter,IDateTimeProvider dateTimeProvider,IBookReturnmentPolicy policy,IBookOrderService bookOrderService,IBookSearchService bookSearchService,IScheduler scheduler,IEmailService emailService,ISmsSender smsSender){ } IDisposable- 35. GetDev.NETASP.NET MVC 3Castle Windsor Composition Root CustomRoleProvider Service Locator ;) Linq to Sql interface IUnitOfWork : IDisposable{ITable Users{ get; set; } } IAccountService 36. ? ! [email protected]://www.solforbiz.com http://GetDev.NET 37. Dependency Injection in .NET by Mark Seemannhttp://manning.com/seemann/ Castle Windsor homepagehttp://stw.castleproject.org/Windsor.MainPage.ashx Krzysztof Komics bloghttp://kozmic.pl/ IoC-http://www.palmmedia.de/Blog/2011/8/30/ioc-container-benchmark-performance-comparison IoC-http://code.google.com/p/net-ioc-frameworks/ 38. IoC Container usage: Patterns and anti-pattern (KrzysztofKomic)http://kozmic.pl/presentations/ Inversion of Control/Dependency Injection Pattern(Hammet Verissimo & Michael Puleio)http://channel9.msdn.com/posts/PP-Symposium-2010-Inversion-of-ControlDependency-Injection-Pattern-Hammet-Verissimo--Michael-Puleio Channel 9http://channel9.msdn.com/ Search > Dependency Injection ||Inversion of Control -> Enter