从OO到AOP,一路走来就是眼花缭乱的术语,蒙蔽了这些东西的本来面目。如果你还在驻足,那你就该尝试着看看这个软件开发的世界里,是谁在狂欢!
Martin Fowler 很大师,两篇论文就搞的开发界鸡飞狗跳、鸡犬不宁。一篇当然是 《Inversion of Control Containers and the Dependency Injection pattern》 (04年 控制反转和依赖注入) ,另一篇则是《 Continuous Integration》(06年 持续集成)。如果提这两篇论文,则不能不说在.net开发中与之相关的框架或者工具:Spring.Net和C ruiseControl.Net,而前一个也是我们要学习的对象。
八卦:中国的体制怎么没有催生出写这些论文的人呢,论文不必是该死的教授才能写的吧!
再八卦:题外的话,研究一个人的东西先要看他的拥有技能和所处环境,否则Context就不对了。对于Martin Fowler.我只能说深入OO,到设计模式,到重构,到测试驱动,到敏捷编程。一路下来,再认识认识Kent Beck,Rod Johnson 。想必收获不小。
Spring.Net“翻译”自Java版本的Spring。这篇文章展示一些基本问题,关于理论性的东西最好去读读上面推荐的论文,不做累述。
环境配置
1.下载最新的框架 Spring.Net 1.1.1. 并安装。
2.新建测试的Windows Application 程序。 引用Spring.Core.dll到工程下。
3.增加配置文件App.Config,配置Spring.Net信息如下:
<?
xml version="1.0" encoding="utf-8"
?>
<
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
xmlns
="http://www.springframework.net"
>
</
objects
>
</
spring
>
</
configuration
>
objects标签是我们配置加载对象的地方。
加载对象
看过PetShop源代码的人对这项功能可能很熟悉,不再累述,举例说明:
以下代码加载Speaker类。
Speaker 类的代码。
using
System;
using
System.Collections.Generic;
using
System.Text;

namespace
SpringExample

{
public class Speaker

{
private string _name = "Not Modify";

public string Name

{

get
{ return _name; }

set
{ _name = value; }
}
private IName _nameInterface;
public Speaker()

{
}
public Speaker(string name)

{
_name = name;
}
public IName NameInterface

{

set
{ _nameInterface = value; }

get
{ return _nameInterface; }
}

}
}
按照以下步骤加载Speaker类。
1.在App.Config中配置Speaker 类的信息,注意在objects标签下。
<
object
name
="Speaker"
type
="SpringExample.Speaker, SpringExample"
>
</
object
>
2.以下为调用代码
using
Spring.Context;
using
Spring.Context.Support;

private
void
button1_Click(
object
sender, EventArgs e)

{

/**//*普通调用*/
IApplicationContext ctx = ContextRegistry.GetContext();
Speaker speaker = (Speaker)ctx.GetObject("Speaker");
MessageBox.Show(speaker.Name);
}
执行结果为“No Modify”
属性注入和构造函数注入
注入方式有几种,可以参考Rod Johnson的《Spring框架高级编程》(Java)。 这里只以上述两种方式举例。
Speaker类的NameInterface属性是获取IName这样的接口,我们可以在Spring.Net中配置信息,让Speaker创建后就已经有了一个可以使用的IName接口。
以下为IName和NameImpl类的代码。
using
System;
using
System.Collections.Generic;
using
System.Text;

namespace
SpringExample

{
public interface IName

{
string MyName();
}
}
using
System;
using
System.Collections.Generic;
using
System.Text;

namespace
SpringExample

{
public class NameImpl:IName

{

IName Members#region IName Members

public string MyName()

{
return "From Spring";
}

#endregion
}
}
1.配置App.Config,为Speaker类的NameInteface属性注入NameImpl类。
<
object
name
="Speaker"
type
="SpringExample.Speaker, SpringExample"
>
<
property
name
="NameInterface"
ref
="Impl"
/>
</
object
>
<
object
name
="Impl"
type
="SpringExample.NameImpl, SpringExample"
>
</
object
>
2.调用代码如下:
using
Spring.Context;
using
Spring.Context.Support;

private
void
button3_Click(
object
sender, EventArgs e)

{

/**//*属性注入*/
IApplicationContext ctx = ContextRegistry.GetContext();
Speaker speaker = (Speaker)ctx.GetObject("Speaker");
MessageBox.Show(speaker.NameInterface.MyName());
}
执行结果是"From Spring".
构造函数注入:
注意看Speaker类有一个含有一个参数的构造函数,我们这次要配置该参数的值由配置文件传入。
1.配置App.Config,为Speaker类的构造函数传入参数。
<
object
name
="Speaker"
type
="SpringExample.Speaker, SpringExample"
>
<
constructor-arg
index
="0"
value
="From Construct"
/>
</
object
>
2.调用代码如下:
using
Spring.Context;
using
Spring.Context.Support;

private
void
button2_Click(
object
sender, EventArgs e)

{

/**//*构造注入*/
IApplicationContext ctx = ContextRegistry.GetContext();
Speaker speaker = (Speaker)ctx.GetObject("Speaker");
MessageBox.Show(speaker.Name);
}
执行结果为:From Consturct
好了剩下的就是大家举一反三,从三到万了。
Martin Fowler 很大师,两篇论文就搞的开发界鸡飞狗跳、鸡犬不宁。一篇当然是 《Inversion of Control Containers and the Dependency Injection pattern》 (04年 控制反转和依赖注入) ,另一篇则是《 Continuous Integration》(06年 持续集成)。如果提这两篇论文,则不能不说在.net开发中与之相关的框架或者工具:Spring.Net和C ruiseControl.Net,而前一个也是我们要学习的对象。
八卦:中国的体制怎么没有催生出写这些论文的人呢,论文不必是该死的教授才能写的吧!
再八卦:题外的话,研究一个人的东西先要看他的拥有技能和所处环境,否则Context就不对了。对于Martin Fowler.我只能说深入OO,到设计模式,到重构,到测试驱动,到敏捷编程。一路下来,再认识认识Kent Beck,Rod Johnson 。想必收获不小。
Spring.Net“翻译”自Java版本的Spring。这篇文章展示一些基本问题,关于理论性的东西最好去读读上面推荐的论文,不做累述。
环境配置
1.下载最新的框架 Spring.Net 1.1.1. 并安装。
2.新建测试的Windows Application 程序。 引用Spring.Core.dll到工程下。
3.增加配置文件App.Config,配置Spring.Net信息如下:


















objects标签是我们配置加载对象的地方。
加载对象
看过PetShop源代码的人对这项功能可能很熟悉,不再累述,举例说明:
以下代码加载Speaker类。
Speaker 类的代码。






















































按照以下步骤加载Speaker类。
1.在App.Config中配置Speaker 类的信息,注意在objects标签下。















属性注入和构造函数注入
注入方式有几种,可以参考Rod Johnson的《Spring框架高级编程》(Java)。 这里只以上述两种方式举例。
Speaker类的NameInterface属性是获取IName这样的接口,我们可以在Spring.Net中配置信息,让Speaker创建后就已经有了一个可以使用的IName接口。
以下为IName和NameImpl类的代码。










































1.配置App.Config,为Speaker类的NameInteface属性注入NameImpl类。





2.调用代码如下:













执行结果是"From Spring".
构造函数注入:
注意看Speaker类有一个含有一个参数的构造函数,我们这次要配置该参数的值由配置文件传入。
1.配置App.Config,为Speaker类的构造函数传入参数。




2.调用代码如下:













执行结果为:From Consturct
好了剩下的就是大家举一反三,从三到万了。