getting started with dependency injection using...
TRANSCRIPT
![Page 1: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/1.jpg)
Getting Started with
Dependency Injection using
Spring.NET
Joe Walton
Orlando .NET Code Camp
March 31, 2012
![Page 2: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/2.jpg)
Agenda
Software Design Checklist
Building a better house
What is…
Case Study
And it does that too?
![Page 3: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/3.jpg)
Software Design Checklist
Testable (reliability)
– Test Driven Development (TDD)
– Continuous Integration (CI)
– MSTest, NUnit, Selenium
Reusable (share)
– No more redundant code!
Maintainable (capacity to change)
– Change without pain!
Separable (layered)
– Decomposing into functional layers
![Page 4: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/4.jpg)
A very RAD example…
![Page 5: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/5.jpg)
The email feature is great, but can
it send a text message too?
![Page 6: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/6.jpg)
Straw Scorecard
Testable (reliability)
Reusable (share)
Maintainable (capacity to change)
Separable (layered)
![Page 7: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/7.jpg)
Let’s build a better house…
![Page 8: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/8.jpg)
What are we doing now?
Dependency Injection Principle (DIP)
– Introduce abstractions (code to interface)
Improved Separation and Reusability
– Separation of Concerns (SoC)
Decomposing system into layered architecture
DI is a form of SoC – configuration is separated
– Single Responsibility Principle (SRP)
Dependency Injection (DI) Pattern
– Dependencies injected at creation
– Constructor, setter and method style injection
![Page 9: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/9.jpg)
Show us the code!
![Page 10: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/10.jpg)
Stick Scorecard
Testable (reliability)
Reusable (share)
Maintainable (capacity to change)
Separable (layered)
![Page 11: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/11.jpg)
We can do better…
![Page 12: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/12.jpg)
Inversion of Control (IoC)
Client delegates control to IoC container
– Removes client knowledge of how to create objects
– Inverted relationship
Looks and smells like Factory Design Patterns
– Framework for the plumbing
Pick your flavor
– Unity, Castle Windsor, Ninject, Spring.NET,
StructureMap
![Page 13: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/13.jpg)
More code please!
![Page 14: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/14.jpg)
Brick Scorecard
Testable (reliability)
Reusable (share)
Maintainable (capacity to change)
Separable (layered)
![Page 15: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/15.jpg)
.NET Configuration File
...
<configSections>
<sectionGroup name="spring">
<section name="context"
type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects"
type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
...
<spring>
<context>
<resource uri="config://spring/objects" />
<resource uri="file://Config/services-context.xml" />
<resource uri=“assembly://MyAssembly/MyProject/common-context.xml" />
</context>
<objects xmlns="http://www.springframework.net">
...
</objects>
</spring>
...
Optional
Optional
External Configuration
Metadata
![Page 16: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/16.jpg)
Configuration Metadata
...
<object id=“MyService”
name=“MyServiceName”
type=“CodeCamp.Services.MyService, CodeCamp.Services”
factory-method=“CreateInstance”
factory-object=“ServiceFactory”
parent=“InheritedObject”
init-method=“Initialize”
destroy-method=“Cleanup”
singleton=“true or false”>
<constructor-arg name=“Enabled” value=“true” />
<constructor-arg type=“string” value=“true” />
<constructor-arg index=“0” value=“true” />
<property name=“MyProperty1” ref=“MyObject” />
<property name=“MyProperty2” expression=“MyExpression” />
<property name=“MyList1”>
<list>
<value>A</value>
<value>B</value>
</list>
</property>
</object>
...
Lifecycle
Scope
Constructor
injection
Setter
injection
Instantiation
Inheritance
![Page 17: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/17.jpg)
Client
using Spring.Context;
using Spring.Context.Support;
Context Registry:
...
IApplicationContext context = ContextRegistry.GetContext();
IService service = (IService)context.GetObject(“MyObject");
Resource Registry:
...
IApplicationContext context = new XmlApplicationContext(“file://common.xml”, “assembly://MyAssembly/MyService/services.xml”);
IService service = (IService)context.GetObject(“MyObject");
![Page 18: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/18.jpg)
It really does work!
Batch Job Framework
– 75 assorted C/C++ console apps
Generate various files
Email, Copy/Move, FTP, Load Data, etc.
– 150 shell scripts
Call apps and pass parameters
– Scheduled and On-demand
BMC Control-M
![Page 19: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/19.jpg)
Advantages
Reduces coupling
Change is not pain!
Increases code reuse
Improves code maintainability
Simplifies testing
Orchestrate the collaboration of objects
![Page 20: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/20.jpg)
What is Spring.NET?
Enterprise application framework
You are here!
![Page 21: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/21.jpg)
Spring.Web.MVC Example
![Page 22: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/22.jpg)
Spring.NET
http://www.springframework.net/
“Led and sustained by SpringSource, a division of VMware, Spring.NET is an open source application framework that makes building enterprise .NET applications easier.”
Projects – Code-based Configuration
– Visual Studio Add-in
– Social - establish connections with Software-as-a-Service (SaaS) Providers including Facebook and Twitter to invoke APIs.
![Page 23: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/gettingstartedwithdependencyinjectionus… · Dependency Injection Principle (DIP) –Introduce abstractions](https://reader033.vdocuments.us/reader033/viewer/2022060511/5f2950a78b22433af060f7df/html5/thumbnails/23.jpg)
Thank you!
http://joemwalton.com