Understanding Dependency Injection… and those pesky containers Miguel A. Castro Architect -

Slides:



Advertisements
Similar presentations
Apache Struts Technology
Advertisements

Spring, Hibernate and Web Services 13 th September 2014.
ASP.NET MVC Best Practices Simone Chiaretta Solution Developer, Avanade 21 Ottobre 2009.
Fabian Vilers Hands on ASP.NET MVC.
Spring.Net Steinar Dragsnes steinar.dragsnes at viz.no.
Real world Windows Phone development Igor
ineta  Solutions Architect & Trainer  Microsoft MVP (10 years and running!)  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the.
Shailen Sukul Senior Solutions Architect EMC BSc | Mct | Mcpd (.Net 2/3.5) | Mcts (Sharepoint (MOSS/WSS), Biztalk, Web, Win, Dist Apps) | Mcsd.NET | Mcsd.
? Data-Driven apps and LightSwitch TechEd sessions Domain-Driven Design,.NET and Loosely-coupled approaches TechEd sessions Windows Apps Modernization.
ineta .NET Architect, Developer, & Trainer  Microsoft MVP (10 years and running!)  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member.
ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers.
Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day.
Architecture Of ASP.NET. What is ASP?  Server-side scripting technology.  Files containing HTML and scripting code.  Access via HTTP requests.  Scripting.
Introduction to the Spring Framework By: Nigusse A. Duguma Kansas State university Department of Computer Science Nov 20, 2007.
Spring Overview, Application demo -Midhila Paineni 09/23/2011 Spring Overview, Application demo9/8/20151.
Presenter - Donn Felker.  Senior Consultant for Microsoft Gold Certified Partner- Statêra.  8 years of experience in developing and architecting enterprise.
Doing Something Useful with Enterprise Library 3.0 Benjamin Day Level: Intermediate.
Building SOLID Software with Dependency Injection Jeremy Rosenberg.
Lesley Bross, August 29, 2010 ArcGIS 10 add-in glossary.
MVC and MVP. References enter.html enter.html
ASP.NET Web API Udaiappa Ramachandran NHDN-Nashua.NET/Cloud Computing UG Lead Blog:
Copyright © 2012 Accenture All Rights Reserved.Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are.
Todd Snyder Development Team Lead Infragistics Experience Design Group.
Building Web Sites with ASP.NET MVC Framework Noam King CTO Sela College
Building an Offline Smart Client using Domain-Driven Design Principles Tim McCarthy.
The Web Architecture and ASP.NET. Slide 2 Review of the Web (1) It began with HTTP and HTML, which delivers static Web pages to browsers which would render.
Copyright © 2005 Finetix LLC All Rights Reserved 0 SpringFramework.Net Developer Session Chris Donnan The Peer Frameworks Series -.Net and Java.
Getting Started with Caliburn.Micro and Windows Phone 7 Gary Ewan Park Twitter: Blog:
DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer You may like to write these down now...
Spring Framework. Spring Overview Spring is an open source layered Java/J2EE application framework Created by Rod Johnson Based on book “Expert one-on-one.
Sander Hoogendoorn Principal Technology Officer Capgemini The Netherlands SESSION CODE: ARC303.
Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group
Castle Manoj Waikar Pune, India.. Introduction Castle aspires to simplify the development of enterprise and web applications. It offers a set of tools.
Using Mock Objects with Test Driven Development Justin Kohlhepp
Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow.blogspot.com.
Design for testability as a way to good coding Simone Chiaretta Architect, Council of the EU December 9 th,
ASP.Net MVC Extensibility, scalability and testability Andrew Locatelli Woodcock.
Spring and DWR Frameworks for Rich Web Enterprise Application Thomas Wiradikusuma Presentation to the 20 th.
Interceptor CS562 Spring 2002 Jan Anand Krishnan Morgan Deters Venkita Subramonian.
Secrets of an Umbraco Ninja Presented by : Aaron powell.com.
ineta  Solutions Architect & Trainer  Microsoft MVP (10 years and running!)  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the.
How to implement the Repository Pattern in an ASP.NET MVC Application Dhananjay Kumar Developer Evangelist – Infragistics Microsoft MVP
ASP.NET 5 Unleashed Javier Lozano
#SPSSAN June 30, 2012 San Diego Convention Center WRITING TESTABLE CODE In SharePoint.
Jean-Claude Trachsel Senior Consultant, Trivadis AG The good news.
Programming with MVVM Miguel A. Castro Architect -
Benjamin Unit Testing & Test-Driven Development for Mere Mortals.
Miguel A. Castro Architect IDesign SESSION CODE: WEB310.
Building rich web applications with ASP.NET AJAX Mike Ormond Developer & Platform Evangelism Group, Microsoft Ltd Developer & Platform Evangelism Group,
Real world Windows Phone development
Advanced Programming in Java
Mark Seemann - Dependency Injection in .NET
Did your feature got in, out or planned?
Building Web Applications with Microsoft ASP
Managed Extensibility Framework
Unit Testing & Test-Driven Development for Mere Mortals
ASP.NET Web Forms Vs. ASP.NET MVC ASP.NET is Microsoft’s big leap after Active Server Pages (ASP), ASP.NET provides flexible and unified web development.
Unit Testing & Test-Driven Development for Mere Mortals
Present by Andie Saizan, MCP
Dependency Injection with MEF and Unity
Interfaces.
Advanced Programming in Java
Titel Dependency Injection.
Advanced Programming in Java
Unit Testing & Test-Driven Development for Mere Mortals
Leveraging ColdSpring To Make Better Applications
Designing For Testability
European conference.
Dependency Injection Mechanism
An Introduction to the Model-View-ViewModel Pattern
Presentation transcript:

Understanding Dependency Injection… and those pesky containers Miguel A. Castro Architect -

ineta .NET Architect, Developer, & Trainer  Microsoft MVP  ASP Insider  VSX Insider  C# Insider  Azure Insider  Member of the INETA Speakers Bureau  Conference Speaker  In IT business since 1986

Have you seen this woman?

Agenda Classes, Dependencies, and Coupling DI Explained & Demoed DI Containers A Tour Through Several Containers –Unity, –NInject, –Castle Windsor –StructureMap –MEF

Agenda Dependency Injection usage –WPF / Silverlight / WinRT –ASP.NET MVC –ASP.NET WebForms (yes, you read right)

Class coupling (why it’s bad) One class depending on another –Cannot exist (compile) without other class Limits functionality to single implementation If classes perform DB work, difficult to test without hitting DB

To decouple, abstract Program to interfaces (or abstract classes) Hosting class uses interfaces –Can receive any implementation –Can have real one for production –Mocked one for testing

DEMO Esentials (coupled and abstracted)

What is Dependency Injection An architectural pattern designed to easily satisfy a class’ dependencies –Allows us to write decoupled code –Facilitate testability –Ease deployment of components Typically implemented with the aid of an object container

The DI Container A repository for definitions typically relating an abstraction to a concrete class Core functionality –Provide facility for registering classes Usually related to interfaces –Provide facility for resolving a request Usually from a given interface (not always)

The DI Container Type associations (registrations) achieved depending on container –Procedural (fluent interface) Unity, NInject, Castle Windsor, StructureMap –Configuration Spring.NET –Declarative (attributes) Managed Extensibility Framework (MEF)

The DI Container Recursively resolves dependencies Injected interface variables –Constructor (usual) –Property Requesting one class (from container) starts chain reaction

DEMO DI Container internals Poor-Man’s DI Container

Unity Microsoft’s contribution to DI Containers Code-based registration XML-based registration Constructor injection Provides interception (aspects) Part of Enterprise Library

NInject Code-based registration Attribute-based registration Constructor injection

Castle Windsor Second oldest DI container for.NET XML-based registration Code-based registration One of the more popular DI containers

StructureMap Oldest DI Container for.NET Code-based registration XML-based registration Attribute-based registration

Spring.NET Port from Spring for Java XML-based registration (originally) Code-based registration Provides many additional features –AOP being a key one Lots of “business framework” functionality

MEF Not a true DI container (so they tell me) –Serves purpose well though Framework for providing add-in architecture to application Provides features not found in DI containers

MEF Uses discovery mechanism to find components (good) –Only interfaces known at compile-time Components coupled to discovery mechanism (bad) –Attribute driven For most DI needs, works nicely –All, in my case

MEF Discovery mechanism allows extra customization –Can extend Export and Import attributes and alter discovery behavior Include with.NET Framework

DEMO DI Containers

Container Comparison ContainerRegistrationInjectionMultiple Constructors UnityProgrammatic, XML Constructor, Limited Property Yes NInjectProgrammaticConstructor, Limited Property Yes Castle WindsorProgrammatic, XML Constructor, Property Yes StructureMapProgrammatic, XML, Discovery Constructor, Limited Property Yes Spring.NETProgrammatic, XML Constructor, Property Yes MEFDiscoveryConstructor, Property No

DI in WPF / Silverlight / WinRT Used to resolve ViewModel classes –Used to resolve dependencies injected into ViewModels –Used to resolve nested ViewModels ViewModels can be tested and test dependency implementations used

DI in ASP.NET MVC Used to resolve controller classes –Used to resolve injected dependencies into controllers Can use a custom controller factory MVC offers dependency resolver Controllers can be tested and test dependency implementations used

DI in ASP.NET WebForms Used to resolve injected dependencies into requested web forms Uses a custom page handler factory Testing code-behind classes still troublesome

DEMO DI Container Usage

Conclusion DI Containers assist in managing components to be used The core principle is usage of interfaces to build decoupled components Containers offer different features –For most part, all accomplish the same thing MEF is first class citizen

References Dependency Injection in.NET –Mark Seemann – Manning Martin Fowler’s DI/IoC article – Tons of info on the web

Miguel A. Castro Thank You