Getting Started with Dependency Injection using...

23
Getting Started with Dependency Injection using Spring.NET Joe Walton Orlando .NET Code Camp March 31, 2012

Transcript of Getting Started with Dependency Injection using...

Page 1: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

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/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

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/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

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/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

A very RAD example…

Page 5: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

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/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

Straw Scorecard

Testable (reliability)

Reusable (share)

Maintainable (capacity to change)

Separable (layered)

Page 7: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

Let’s build a better house…

Page 8: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

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/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

Show us the code!

Page 10: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

Stick Scorecard

Testable (reliability)

Reusable (share)

Maintainable (capacity to change)

Separable (layered)

Page 11: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

We can do better…

Page 12: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

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/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

More code please!

Page 14: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

Brick Scorecard

Testable (reliability)

Reusable (share)

Maintainable (capacity to change)

Separable (layered)

Page 15: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

.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/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

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/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

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/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

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/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

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/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

What is Spring.NET?

Enterprise application framework

You are here!

Page 21: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

Spring.Web.MVC Example

Page 22: Getting Started with Dependency Injection using Springjoemwalton.com/blog/docs/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

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/GettingStartedwithDependencyInjection... · Getting Started with Dependency Injection using Spring.NET

Thank you!

http://joemwalton.com