inversion of control: spring.net overview

Post on 10-May-2015

3.279 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

www.orbitone.com

Raas van Gaverestraat 83B-9000 GENT, Belgium E-mail info@orbitone.com 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

Inversion Of Control - Spring.NET Overview2

Dependencies and Dependency Injection

10 May, 2009

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

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

Inversion Of Control - Spring.NET Overview5

IOC Container

10 May, 2009

Inversion Of Control - Spring.NET Overview6

Aha…and what is an IOC container?

10 May, 2009

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

Inversion Of Control - Spring.NET Overview8

And more…

10 May, 2009

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

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

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

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

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

Inversion Of Control - Spring.NET Overview14

Example application

10 May, 2009

Inversion Of Control - Spring.NET Overview15

Solve the dependencies with Spring.NET IOC

Demo

10 May, 2009

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

17 Inversion Of Control - Spring.NET Overview

www.orbitone.com

10 May, 2009

top related