代码改变世界

【原】Spring.NET 学习笔记1 入门

2010-09-08 21:00  bugfly  阅读(630)  评论(1编辑  收藏  举报

 

 

   最近学习Spring.NET,所以写下每一个用过的例子的Demo,这篇是入门Demo,主要是简述IOC为何出现,如何应用Spring.NET到项目中,配置等。由于文笔丑陋,所以直接上代码来描述。我使用的是Spring.NET 1.3.0  下载地址http://www.springframework.net/download.html

 

以下是Demo的项目布局图

 

 

1)IPeople接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
public interface IPeople
{
void speak();
}
}

 

1)IWeapon接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
public interface IWeapon
{
string getName();
}
}

 

3)IWeapon接口的武器实现类,Sword  剑类。

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
class Sword:IWeapon
{
public string getName()
{
return "Sword";
}
}
}

 

 

4)IWeapon接口的武器实现类,Gun  枪类。

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
class Gun:IWeapon
{
public string getName()
{
return "Gun";
}
}
}

 

 

5)IPeople接口的实现类 StupidMan 蠢人类。

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
public class StupidMan
{
protected string Name;
protected IWeapon Weapon;

public StupidMan()
{
this.Name = "StupidMan";
//人类和具体的武器类耦合了,如果要更改武器类型,就要改所有这个类里面由具体的武器类散播下的碎片。
this.Weapon = new Sword();
}

public void speak()
{
Console.WriteLine(
"My name is "+this.Name);
Console.WriteLine(
"I am using a " + Weapon.getName());
}
}
}

注释:可以看出来StupidMan类耦合了具体的武器类Sword,Sword的生命周期在StupidMan里初始化,这里会导致一个问题,当要更换武器的时候,就必须修改StupidMan类来调整需求,而StupidMan类散落了Sword这个具体类的碎片,维护成本会随着功能复杂而提高!,所以类如其名,Stupid~^ ^

 

6)IPeople接口的实现类 SmartMan 聪明人类。

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
public static class WeaponFactory
{
public static IWeapon equipWeapon()
{
return new Gun();
}
}
}

注释:工厂类 

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
class SmartMan:IPeople
{
protected string Name;
protected IWeapon Weapon;

public SmartMan()
{
this.Name = "SmartMan";
//通过介入工厂,统一管理武器对象,将耦合推到工厂类,维护效率大大提升。
this.Weapon = WeaponFactory.equipWeapon();
}

public void speak()
{
Console.WriteLine(
"My name is "+this.Name);
Console.WriteLine(
"I am using a " + Weapon.getName());
}
}
}

注释:可以看出SmartMan介入了工厂方法模式去解藕和具体的武器类的关系,把耦合关系转嫁到工厂类本身,把本来零散的对象碎片统一用一个工厂来管理,维护成本大大降低,然而无论工厂多么完美,也要依赖具体的工厂接口来完成需求。

 

7)IPeople接口的实现类 SuperMan 超级人类。

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
public class SuperMan:IPeople
{
protected string Name;
protected IWeapon Weapon;

public SuperMan()
{
this.Name = "Hero";
}

public void speak()
{
Console.WriteLine(
"My name is "+this.Name);
Console.WriteLine(
"I am using a " + Weapon.getName());
}
}
}

注释: 可以看出SuperMan类很清洁,没有具体的武器对象,没有工厂,那究竟如何知道用的是什么武器?下面我讲用一个单元测试来展示效果。

 

8)一个单元测试类。

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using NUnit.Framework;
using Spring.Context;
using Spring.Context.Support;
using Spring.Objects.Factory;

namespace NunitTest
{
[TestFixture]
class ClassTest
{
private IPeople m_people;

[TestFixtureSetUp]
public void init()
{
string[] xmlPath = new string[]{
"file://Xml/Object.xml"
};

IApplicationContext n_context
= new XmlApplicationContext(xmlPath);
IObjectFactory n_obFactory
= (IObjectFactory)n_context;

m_people
=(IPeople)n_obFactory.GetObject("xSuperMan");
}

[TestFixtureTearDown]
public void release()
{
m_people
= null;
}

[Test]
public void test_SuperMan()
{
this.m_people.speak();
}
}
}

注释:用XML文件配置对象的依赖关系,然后载入这个XML文件。通过Spring.NET容器来初始化对象。以下是Object.xml文件。

 

9)Object.xml  当然也可以配置到web.config或者app.config,但这里没涉及到WEB项目,所以直接配置到外部XML文件中。

代码
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://www.springframework.net
http://www.springframework.net/xsd/spring-objects.xsd"
>

<object id="xSuperMan" type="Model.SuperMan, Model">
<property name="Weapon" ref="xGun"/>
</object>
<object id="xSword" type="Model.Sword, Model"/>
<object id="xGun" type="Model.Gun, Model"/>
</objects>

 

 

 10)Nunit运行结构。

 

 

 本文完整Demo源码

 

结语:入门Spring.NET到此结束。