getting started with dependency injection using...
TRANSCRIPT
Getting Started with
Dependency Injection using
Spring.NET
Joe Walton
Orlando .NET Code Camp
March 31, 2012
Agenda
Software Design Checklist
Building a better house
What is…
Case Study
And it does that too?
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
A very RAD example…
The email feature is great, but can
it send a text message too?
Straw Scorecard
Testable (reliability)
Reusable (share)
Maintainable (capacity to change)
Separable (layered)
Let’s build a better house…
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
Show us the code!
Stick Scorecard
Testable (reliability)
Reusable (share)
Maintainable (capacity to change)
Separable (layered)
We can do better…
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
More code please!
Brick Scorecard
Testable (reliability)
Reusable (share)
Maintainable (capacity to change)
Separable (layered)
.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
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
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");
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
Advantages
Reduces coupling
Change is not pain!
Increases code reuse
Improves code maintainability
Simplifies testing
Orchestrate the collaboration of objects
What is Spring.NET?
Enterprise application framework
You are here!
Spring.Web.MVC Example
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.
Thank you!
http://joemwalton.com