17
www.orbitone.com Raas van Gaverestraat 83 B-9000 GENT, Belgium E-mail [email protected] Website www.orbitone.com Tel. +32 9 265 74 20 Fax +32 9 265 74 10 VAT BE 456.457.353 Bank 442-7059001-50 (KBC) 10 May, 2009 Inversion Of Control - Spring.NET Overview

Inversion Of Control: Spring.Net Overview

Embed Size (px)

DESCRIPTION

Introduction to Spring.NET, Inversion of Control. Spring.NET is an open source application framework that makes building enterprise .NET applications easier.

Citation preview

Page 1: Inversion Of Control: Spring.Net Overview

www.orbitone.com

Raas van Gaverestraat 83B-9000 GENT, Belgium E-mail [email protected] Website www.orbitone.com

Tel. +32 9 265 74 20Fax +32 9 265 74 10VAT BE 456.457.353Bank 442-7059001-50 (KBC)

10 May, 2009 Inversion Of Control - Spring.NET Overview

Page 2: Inversion Of Control: Spring.Net Overview

Inversion Of Control - Spring.NET Overview2

Dependencies and Dependency Injection

10 May, 2009

Page 3: Inversion Of Control: Spring.Net Overview

Inversion Of Control - Spring.NET Overview3

What is Dependency Injection?

Dependency injection (DI) in programming refers to the process of supplying an external dependency to a software component.

It is a specific form of inversion of control where the concern being inverted is the process of obtaining the needed dependency.

10 May, 2009

Page 4: Inversion Of Control: Spring.Net Overview

Inversion Of Control - Spring.NET Overview4

But … what is Inversion of control?

Inversion of control, or IoC, is an abstract principle describing an aspect of some software architecture designs in which the flow of control of a system is inverted in comparison to the traditional architecture.

Control flow is expressed in imperative programming in the form of a series of instructions or procedure calls. Instead of specifying a sequence of decisions and procedures to occur during the lifetime of a process, the user of a IoC framework writes the desired responses linked to particular events or data requests.

10 May, 2009

Page 5: Inversion Of Control: Spring.Net Overview

Inversion Of Control - Spring.NET Overview5

IOC Container

10 May, 2009

Page 6: Inversion Of Control: Spring.Net Overview

Inversion Of Control - Spring.NET Overview6

Aha…and what is an IOC container?

10 May, 2009

Page 7: Inversion Of Control: Spring.Net Overview

Inversion Of Control - Spring.NET Overview7

Spring.NET

an application framework for buidling Enterprise .NET applications

an IoC Container (Inversion of Control) that manages and injects dependencies on behalf of developers (DI = Dependency Injection)

10 May, 2009

Page 8: Inversion Of Control: Spring.Net Overview

Inversion Of Control - Spring.NET Overview8

And more…

10 May, 2009

Page 9: Inversion Of Control: Spring.Net Overview

Inversion Of Control - Spring.NET Overview9

IoC Containers for .NET

PicoContainer.NET: lightweight and highly embeddable IoC container

StructureMap: lightweight Inversion of Control (IoC) Container written in C#; can improve the architectural qualities of .NET apps by reducing the mechanical costs of good design techniques

Castle: Tools for application development including small IoC container

Spring.NET: full featured IoC container (port of Java version)

10 May, 2009

Page 10: Inversion Of Control: Spring.Net Overview

Inversion Of Control - Spring.NET Overview10

Practice First approach - Dependencies

public class ExampleObject {

private AnotherObject objectOne; // dependencies

private YetAnotherObject objectTwo;

private int i;

public AnotherObject ObjectOne {

set { this.objectOne = value; }

}

public YetAnotherObject ObjectTwo {

set { this.objectTwo = value; }

}

public int IntegerProperty {

set { this.i = value; }

}

}

10 May, 2009

Page 11: Inversion Of Control: Spring.Net Overview

Inversion Of Control - Spring.NET Overview11

Configuration I

Preferred way to create Object Factories and Application contexts is via configuration:<configuration> <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"/> </context> <objects> ... </objects> </spring></configuration>

Handlers used for Spring configuration section

Where to find object configuration, e.g. file, assembly, config

Configuration of Spring managed objects

10 May, 2009

Page 12: Inversion Of Control: Spring.Net Overview

Inversion Of Control - Spring.NET Overview12

Configuration II

The configuration tells Spring.NET how objects depend on each other:

<object id="exampleObject" type="Examples.ExampleObject, ExamplesLibrary">

<property name="objectOne" ref="anotherExampleObject"/>

<property name="objectTwo" ref="yetAnotherObject"/>

<property name="IntegerProperty" value="1"/>

</object>

<object id="anotherExampleObject" type="Examples.AnotherObject, ExamplesLibrary"/>

<object id="yetAnotherObject" type="Examples.YetAnotherObject, ExamplesLibrary"/>

Object name Object type: namespace path + class, assembly

Properties referring to other objects: often called dependencies or collaborators

10 May, 2009

Page 13: Inversion Of Control: Spring.Net Overview

Inversion Of Control - Spring.NET Overview13

Usage

Now instantiating an ApplicationContext is simple:

IApplicationContext ctx = ContextRegistry.GetContext();ExampleObject person = (ExampleObject)ctx.GetObject(“exampleObject ");

ID that should appear in object

configuration

10 May, 2009

Page 14: Inversion Of Control: Spring.Net Overview

Inversion Of Control - Spring.NET Overview14

Example application

10 May, 2009

Page 15: Inversion Of Control: Spring.Net Overview

Inversion Of Control - Spring.NET Overview15

Solve the dependencies with Spring.NET IOC

Demo

10 May, 2009

Page 16: Inversion Of Control: Spring.Net Overview

16 Inversion Of Control - Spring.NET Overview10 May, 2009

http://en.wikipedia.org/wiki/Inversion_of_control http://martinfowler.com/articles/injection.html http://abdullin.com/wiki/inversion-of-control-ioc.html http://www.springframework.net/doc-latest/reference/html/objects.html http://www.developer.com/net/csharp/article.php/3722931/Dependency-Inje

ction-with-SpringNet.htm

Resources

Page 17: Inversion Of Control: Spring.Net Overview

17 Inversion Of Control - Spring.NET Overview

www.orbitone.com

10 May, 2009