Top Banner
1 Introduction to Spring .NET Erich Eichinger Core Committer Spring.NET Head of Development, diamond:dogs Copyright 2008 SpringSource. Copying, publishing, or distributing without expressed written permission is prohibited.
57

Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –becomes

Jun 06, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

1

Introduction to Spring .NET

Erich Eichinger

Core Committer Spring.NET

Head of Development, diamond:dogs

Copyright 2008 SpringSource. Copying, publishing, or distributing without expressed written permission is prohibited.

Page 2: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

2

Agenda

• The who, what, why of Spring.NET

• Feature overview

• Dependency Injection

• Data Access and Declarative Transaction Management

• Aspect-Oriented programming

• Tour of Spring.NET specific features

• Future Directions

• Summary

Page 3: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

3

Spring for .NET

• Spring.NET provide comprehensive infrastructural support for developing enterprise .NET™ applications

– Apache License - Commercial-friendly

– Created, supported and sustained by SpringSource

– Integrates with other frameworks and solutions

– .NET 1.0/1.1/2.0

• Spring Framework for Java has shown real-world benefits

– 9 out of the world‟s 10 largest banks use Spring Java

– Architectural concepts and patterns are applicable to .NET

Page 4: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

4

History

• Project started 2004

– 1.0 Release 2005

– 1.1 GA 2007

• Basic Stats

– 8 assemblies

– 24K LOC, 1K types

– No circular refs

– Unit tests

• In production

– For example, see Mercado Electronico Case Study

Page 5: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

5

Spring’s “Nature”

• Inversion of Control (IoC) container

– Configure classes using Dependency Injection (DI)

• Aspect Oriented Programming (AOP)

– Declaratively apply functionality to classes

• Support libraries to tame complex APIs

– Transaction Management, ADO.NET, ASP.NET

• Spring deals with the plumbing

– Address end-to-end requirements rather than one tier

– Can be one stop shop or just use certain sub-systems.

– Consistent programming model

Page 6: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

6

Spring .NET 1.1 Feature Set

• If you are familiar with Spring Java you will feel right at home– <bean/> becomes <object/>

– class= becomes type=

• Spring.NET 1.1 ~= Spring Java 1.2 + schemas

– IoC Container

– AOP

– Testing (NUnit)

• Features with .NET adaptations

– Transaction Management

– ADO.NET, NHibernate

– Spring Services

Page 7: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

7

Unique to Spring.NET

• ASP.NET Framework

• Expression Language

– Powerful object navigation

• UI agnostic

– Data binding framework

– Validation framework

• Aspect Library

– Retry, Exception Translation, Caching

• Dynamic Reflection Library

• Remoting Support

Page 8: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

8

Agenda

• The who, what, why of Spring.NET

• Feature overview

• Dependency Injection

• Data Access and Declarative Transaction Management

• Aspect-Oriented programming

• Tour of Spring.NET specific features

• Future Directions

• Summary

Page 9: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

9

Spring .NET’s IoC Container

• Heart of Spring .NET

• Facilitates full stack plain object-based development

• Within any environment

– ASP.NET, WinForms/WPF, Web Services/WCF, COM+, Console, Unit Tests.

• By providing

– A powerful object factory that manages the instantiation, configuration, decoration, and assembly of your business objects

– Object Factory -> „Container‟

Page 10: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

10

Using the container

• Provide the container configuration metadata

– Instructions on how create the object

– Constructor and/or setter injection?

– Singleton or new instance?

– Call custom lifecycle methods?

– Add additional behavior?

• Metadata formats

– Most common is XML (think XAML)

– Programmatic (C#)

– Attributes (Annotations)

Page 11: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

11

Constructor Injectionpublic class SimpleBankService : IBankService

{

private IAccountDao accountDao;

public SimpleBankService(IAccountDao accountDao)

{

this.accountDao = accountDao;

}

// business methods follow …

}

<object id="bankService” type=“SimpleBankService, MyAssembly">

<constructor-arg name=“accountDao“ ref=“accountDao”/>

</object>

<object id=“accountDao” type=“SimpleAccountDao, MyDaoAssembly">

<property name=“MaxResults” value=“100”/>

...

</object>

Page 12: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

12

Property Injectionpublic class SimpleBankService : IBankService

{

private IAccountDao accountDao;

public IAccountDao AccountDao

{

get { return accountDao; }

set { accountDao = value; }

}

// business methods follow …

}

<object id="bankService" type=“SimpleBankService, MyAssembly“

lazy-init=“true”>

<property name=“AccountDao” ref=“accountDao” />

</object>

<object id=“accountDao” type=“SimpleAccountDao, MyDaoAssembly">

...

</object>

Page 13: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

13

Creating and configuring container

• IApplicationContext

– The IoC container

• Create using „new‟ or configure via App.config

IApplicationContext context =

new XmlApplicationContext("assembly://MyAssembly/MyProject/objects.xml");

IBankService bankService = (IBankService) context.GetObject("bankService");

Page 14: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

14

Container configuration<configuration>

<configSections>

<sectionGroup name="spring">

<section name="context"

type="Spring.Context.Support.ContextHandler, Spring.Core"/>

</sectionGroup>

</configSections>

<spring>

<context>

<resource uri=“assembly://MyAssembly/MyProject/objects.xml"/>

</context>

</spring>

</configuration>

IApplicationContext context = ContextRegistry.GetContext();

IBankService bankService = (IBankService) context.GetObject("bankService");

Page 15: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

15

Spring.NET IoC Summary

• 1st order as feature rich as Spring Java

• Container implementation similar enough to allow easy migration of features

– Attribute based configuration

– Scripted objects (IronPython/IronRuby)

– Will sync some new features in future releases

• If nothing else, use DI to push your application in the direction of following best practices!

– Loose coupling -> easier to test -> resiliency to change

Page 16: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

16

Agenda

• The who, what, why of Spring.NET

• Feature overview

• Dependency Injection

• Data Access and Declarative Transaction Management

• Aspect-Oriented programming

• Tour of Spring.NET specific features

• Future Directions

• Summary

Page 17: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

17

Spring Data Access Goals

• Wide range of data access strategies and technologies to choose from

– APIs tend to be complex and verbose

– Accounts for much of code in an application

– Multiple APIs for transaction management and quirks

• Provide simple and consistent approach to data access across persistence technologies

– Simplify usage

– Logical, technology neutral exception hierarchy

– Transaction management abstraction

Page 18: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

18

Problems with traditional ADO.NET

• Results in redundant, error prone code

• Hard to write provider independent code

• Code is coupled to transaction API

• Verbose parameter management

Page 19: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

19

Redundant Codepublic IList FindAllPeople() {

IList personList = new ArrayList();

try

{

using (SqlConnection connection = new SqlConnection(connectionString))

{

string sql = "select Name, Age from ...";

using (SqlCommand command = new SqlCommand(sql, connection))

{

connection.Open();

using (SqlDataReader reader = command.ExecuteReader())

{

while (reader.Read())

{

string name = reader.IsDBNull(0) ? string.Empty : reader.GetString(0);

int age = reader.IsDBNull(1) ? 0 : reader.GetInt32(1);

Person person = new Person(name, age);

personList.Add(person);

}

}

}

}

}

catch (Exception e) { //throw application exception }

return personList;

}

Page 20: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

20

Redundant Codepublic IList FindAllPeople() {

IList personList = new ArrayList();

try

{

using (SqlConnection connection = new SqlConnection(connectionString))

{

string sql = "select Name, Age from ...";

using (SqlCommand command = new SqlCommand(sql, connection))

{

connection.Open();

using (SqlDataReader reader = command.ExecuteReader())

{

while (reader.Read())

{

string name = reader.IsDBNull(0) ? string.Empty : reader.GetString(0);

int age = reader.IsDBNull(1) ? 0 : reader.GetInt32(1);

Person person = new Person(name, age);

personList.Add(person);

}

}

}

}

}

catch (Exception e) { //throw application exception }

return personList;

}

The bold matters - the

rest is boilerplate

Null values

could be

handled better

What about transaction

management?

Page 21: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

21

AdoTemplate: Lightweight Mapping

public class PersonDao : AdoDaoSupport {

private string cmdText = "select Name, Age from Person";

public virtual IList<Person> FindAllPeople() {

return AdoTemplate.QueryWithRowMapperDelegate<Account>(CommandType.Text,

cmdText,

delegate(IDataReader dataReader, int rowNum) {

Person person = new Person();

person.Name = dataReader.GetString(0);

person.Age = dataReader.GetInt32(1);

return person;

});

}

}

}

Specify the command

Do the work for each iteration

Page 22: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

22

AdoTemplate in a Nutshell

int userCount = (int) adoTemplate.ExecuteScalar(

CommandType.Text,

"SELECT COUNT(0) FROM USER");

• Acquisition of the connection

• Creation of the command

• Participation in the transaction

• Execution of the statement

• Processing of the result set

• Handling of any exception

• Display or rollback on warnings

• Dispose of the reader, command

• Dispose of the connection

All handled

by the

template

Page 23: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

23

DAO implementation - AdoTemplate

• Can still „fall-down‟ to lowest level

private string cmdText =

"select count(*) from Customers where PostalCode = @PostalCode";

public virtual int FindCountWithPostalCode(string postalCode)

{

return AdoTemplate.Execute<int>(delegate(DbCommand command)

{

command.CommandText = cmdText;

DbParameter p = command.CreateParameter();

p.ParameterName = "@PostalCode";

p.Value = postalCode;

command.Parameters.Add(p);

return (int) command.ExecuteScalar();

});

}

Page 24: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

24

Transaction Management

• How to satisfy the requirement

– “The service layer must be transactional”

• Adding boilerplate code in the service layer (programmatic transaction management)

– Is prone to errors; of omission, cut-n-paste

– Ties implementation to transaction implementation

• The solution

– Declarative transaction management

– “Say what to do, not how to do it”

Page 25: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

25

* Promotion to distributed transaction for common designs

Always distributed for Oracle, Sybase, DB2, MySql

** Only for WCF services

What we want to do most often is:

– Declarative with local transactions

Local Distributed Declarative

ADO.NET

EnterpriseServices

System.Transactions *

WCF** *

.NET Transaction Management

Page 26: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

26

Spring .NET Transaction Management

• Consistent model for different transaction APIs

• IPlatformTransactionManager

– AdoTransactionManager

– ServiceDomainPlatformTransactionManager

– TxScopePlatformTransactionManager

– HibernateTransactionManager

• Declarative transaction demarcation strategies

– XML or Attributes

• Using a different transaction manager is a change of configuration, not code

Page 27: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

27

Declarative Transactions using Attributes public class SimpleBankService : IBankService {

[Transaction()]

public Account Create(string name){

Account account = accountDao.Create(name)

if (RequiresSecurity(account)) {

securityDao.CreateCredentials(account);

}

return account;

}

. . .

}

<object id=“bankService" type=“MyServices.SimpleBankService, MyAssembly“>

<property name=“AccountDao” ref=“accountDao” /><property name=“SecurityDao” ref=“securityDao” />

</object>

<tx:attribute-driven/>

Page 28: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

28

Declarative Transactions using XML<object id="bankService”

type=“MyServices.SimpleBankService, MyAssembly">

. . .

</object>

<tx:advice id="txAdvice">

<tx:attributes>

<tx:method name="Get*"

timeout="1000" isolation="RepeatableRead"

no-rollback-for="SillyException"/>

</tx:attributes>

</tx:advice>

<object id="serviceOperation“

type=“RegularExpressionPointcut">

<property name="pattern" value=“MyServices.*Service.*"/>

</object>

<aop:config>

<aop:advisor pointcut-ref="serviceOperation”

advice-ref="txAdvice"/>

</aop:config>

What to do…

Where to do it…

Tie them together

Page 29: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

29

Agenda

• The who, what, why of Spring.NET

• Feature overview

• Dependency Injection

• Data Access and Declarative Transaction Management

• Aspect-Oriented programming

• Tour of Spring.NET specific features

• Future Directions

• Summary

Page 30: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

30

Aspect Oriented Programming (AOP)

• Allows a software component to be decorated with additional behavior

– In a generic, targeted manner

• The container is responsible for “weaving in” the behavior

– Behavior is implemented in a single location (what)

– Specify where to apply behavior (where)

– An aspect encapsulates the where + what

• Complements Object-Oriented Programming

• Terminology

– „what‟ = advice, „where‟ = pointcut

Copyright 2004-2006, Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.

Page 31: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

31

AOP in Spring.NET

• No dynamic proxies provided by BCL

• Spring creates dynamic proxy using Reflection.Emit

• Methods non-virtual by default

– Interception for interfaces or virtual methods

• Future integration with PostSharp

– Compile time IL code weaver

– [Configurable] and [Aspect] support

• AOP schema for configuration

– No pointcut language

Page 32: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

32

Pointcuts

• Match attribute type

– <tx:attribute-driven/> is XML shorthand for this

• Match namespace or type via regular expression

• Match spring objects by „name‟

• Control flow

– Dynamic

– Match if execution is „beneath‟ a class in the call stack

• Custom

• Composable

Page 33: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

33

Agenda

• The who, what, why of Spring.NET

• Feature overview

• Dependency Injection

• Data Access and Declarative Transaction Management

• Aspect-Oriented programming

• Tour of Spring.NET specific features

• Future Directions

• Summary

Page 34: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

34

Spring Expression Language (SpEL)

• Enables object graph navigation

– Properties, methods, aggregators . . .

• Motivation

– Adds significant value to IoC containers

– Fills in the cracks to script simple behavior

• Needed for

– Data binding, validation, aspect library . . .

• Forget hand-coded reflection

• Generally useful in any application

Page 35: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

35

SpEL Features

• Object graph navigation

• Method invocation

• Object construction

• Arithmetic operations

• Logical operations

• List projection and selection

• Collection aggregators and processors

• User defined functions (closures)

Page 36: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

36

SpEL Usage

• ExpressionEvaluator

– Simple to use, but not very performant

– Parses expression for each execution

– Useful for one-off evaluations

• Use Expression.Parse and IExpression for repeated execution

Person aleks = new Person(“Aleks”, Gender.Male, 32);

ExpressionEvaluator.GetValue(aleks, “Name”); // Aleks

ExpressionEvaluator.GetValue(aleks, “Age”); // 32

ExpressionEvaluator.GetValue(aleks, “Gender == Gender.Male”); // true

ExpressionEvaluator.SetValue(aleks, “Name”, “Aleksandar”);

Page 37: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

37

IoC Container integration

• SpEL used to evaluate property name

• „ref‟, „value‟ and also „expression‟

<object id=“person” type=“Person, MyApp”>

<property name=“Children[0]” ref=“anotherChild”/>

<property name=“Address.City” value=“New York”/>

<property name=“FavoriteDate” expression=“DateTime.Today”/>

</object>

Page 38: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

38

Aspect Oriented Programming

• Common use-cases

– Logging

– Transaction Management

– Caching

– Exception Translation

– Performance Monitoring

– Object Pooling

– Custom Business Rules

– Security

Page 39: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

39

Aspect Library

• Configure pre-built aspects

• Example: Exception Translation

– Configuration using DSL

• Other actions

– log

– translate

– replace

– return

– swallow

on exception name ArithmeticException

wrap MyServices.ServiceOperationException

Page 40: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

40

Aspect Library and Expression Language

• A „Little Language‟

– Small but powerful

• Lets you add a little bit of glue code

• If writing more than a few lines

– Subclass advice and write C# based implementation

on exception

( #e is T(SqlException) &&

#e.Errors[0].Number in { 154, 165, 178 } )

translate

new DataAccessException(„Error in #method.Name‟, #e)

Page 41: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

41

Retry Aspect

• Remote calls are unreliable

• If remote operation is idempotent, can retry until achieve success

– Can apply advice based on attribute [Idempotent]

• Similar approach as exception advice

on exception name ArithmeticException retry 3x delay 1s

Page 42: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

42

Retry Advice Configuration

• Leverage SpEL

– Specify formula for retry interval

– Specify exception to act upon

<object name="exceptionHandlingAdvice"

type="Spring.Aspects.RetryAdvice, Spring.Aop">

<property name="retryExpression"

value="on exception name ArithmeticException retry 3x delay 1s"/>

</object>

on exception name FaultException

retry 3x rate (1*#n + 0.5)

Page 43: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

43

Agenda

• The who, what, why of Spring.NET

• Feature overview

• Dependency Injection

• Data Access and Declarative Transaction Management

• Aspect-Oriented programming

• Tour of Spring.NET specific features

• Future Directions

• Summary

Page 44: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

44

Spring ASP.NET Framework Goals

• “Embrace and extend” ASP.NET

• Pain points with ASP.NET are addressed

– Pages depend on middle-tier services, how to obtain?

– Data binding is only in one direction and supported only by some controls

– Need to manage data model supporting the page

– Lifecycle methods should be at higher level of abstraction

– Data validation is tied to the UI and is simplistic

• Simplify ASP.NET development as much as possible by filling in the gaps

Page 45: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

45

DI for Pages, Controls, Modules, Providers

• DI features work with standard ASP.NET page and controls

<object type="Login.aspx">

<property name="Title" value="Hello World"/>

<property name="Authenticator"

ref="authenticationService"/>

</object>

<object type="CustomControl.ascx">

<property name="Message" value=“Hello from Control"/>

</object>

Page 46: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

46

Handling form submission:Without Spring.NET

Copyright 2006 Solutions for Human Capital Inc. and Interface21 Ltd. Copying, publishing, or distributing without expressed written permission is prohibited.

public class MyPage : Page

{

public void ProcessBuyOrder(object sender, EventArgs args)

{

try

{

string stockSymbol = txtStockSymbol.Text;

int numberOfShares = int.Parse(txtNumberOfShares.Text);

BuyOrder order = new BuyOrder(stockSymbol, numberOfShares);

ITradingService tradingService = ServiceLocator.GetService(...);

OrderConfirmation confirmation = tradingService.ProcessOrder(order);

Context.Items["confirmation"] = confirmation;

Server.Transfer("BuyConfirmation.aspx");

}

catch (ParseException e)

{

// handle exception (sometimes this is difficult as well)

}

}

}

Page 47: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

47

Handling form submission: With Spring.NET

public class MyPage : Spring.Web.UI.Page

{

private BuyOrder order;

private OrderConfirmation confirmation;

private ITradingService tradingService;

// properties omitted

protected override InitializeDataBindings()

{

BindingManager.AddBinding(“txtStockSymbol.Text”, “Order.StockSymbol”);

BindingManager.AddBinding(“txtNumberOfShares.Text”, “Order.NumberOfShares”)

.SetErrorMessage(“Invalid Number of Shares”, “errNumberOfShares”);

}

public void ProcessBuyOrder(object sender, EventArgs args)

{

if (ValidationErrors.IsEmpty && Validate(order, orderValidator))

{

confirmation = tradingService.ProcessOrder(order);

SetResult(“buyConfirmation”);

}

}

}

Page 48: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

48

Handling form submission with Spring.NET's DataBindingPanel// .aspx.cs

public class MyPage : Spring.Web.UI.Page {

private BuyOrder order;

public void ProcessBuyOrder(object sender, EventArgs args) {

if (ValidationErrors.IsEmpty && Validate(order, orderValidator)) {

confirmation = tradingService.ProcessOrder(order);

SetResult(“buyConfirmation”);

}

}

}

// .aspx

<spring:DataBindingPanel runat="server">

<asp:TextBox runat="server"

ID="txtNumberOfShares"

BindingTarget="Order.NumberOfShares"

MessageId=" Invalid Number of Shares"

ErrorProviders="errNumberOfShares" />

...

</spring:DataBindingPanel>

Page 49: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

49

Spring ASP.NET Framework Summary

• DI enable ASP.NET

• Bi-directional data binding

• UI independent Data Validation

• Object scopes

– application, session, request

• Code becomes more business and less infrastructure focused

– Data model management

Page 50: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

50

Agenda

• The who, what, why of Spring.NET

• Feature overview

• Dependency Injection

• Data Access and Declarative Transaction Management

• Aspect-Oriented programming

• Tour of Spring.NET specific features

• Future Directions

• Summary

Page 51: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

51

Future Directions

• Spring 1.2 (Spring)

– Attribute driven DI

– VS.NET IDE Wizards

– Container improvements ~2.0/2.5

• Spring 2.0 (Summer)

– Many new features

– Most have been under development for a while

– Will make separately available for download before Summer

Page 52: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

52

Future Directions (2)

• Ecosystem projects

– Spring.NET IDE

– ICache implementations

– Threading library (think java.util.concurrent)

Page 53: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

53

Spring 2.0

• Container Generics

• Scheduling (Quartz.NET)

• PostSharp integration

• [Aspect], [Configurable]

• Messaging

– NMS, NMS WCF Channel

• WCF Exporters

• WinForms

• Scripted Objects

• Spring Integration

• System.Web.Mvc

• Monitoring (WMI)

Page 54: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

54

Agenda

• The who, what, why of Spring.NET

• Feature overview

• Dependency Injection

• Data Access and Declarative Transaction Management

• Aspect-Oriented programming

• Tour of Spring.NET specific features

• Future Directions

• Summary

Page 55: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

55

Who is using Spring .NET

• Mercado Eletrônico: Leading Latin American B2B– See case study in .NET Developers Journal

• Siemens

• Banking

• Oracle Consulting (Israel)

• diamond:dogs Web Consulting (Austria)

– Knorr

– sportnet

– Panorama Tours

– ATV

– Libro

• A global leader in the online travel booking space

Page 56: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

56

Links

• Download from www.springframework.net

– Many samples and extensive reference manual

• Support, Training, Consulting available from SpringSource

– www.springsource.com

Page 57: Introduction to Spring - JUGS · 2008-03-10 · Spring .NET 1.1 Feature Set •If you are familiar with Spring Java you will feel right at home –<bean/>becomes <object/>

57

Q&A