简单的Spring.Net示例
Spring.NET是Java上Spring框架的.net移植版,目前版本为RC2,包含全功能的IOC容器,后续版本将包含AOP、ASP.NET、Remoting和数据存取。
IOC将接口的具体实现实例化过程转移,不再采用硬编码,改由IOC容器来实例化,IOC容器读取配置文件,再通过反射来实例化接口的具体实现,达到调用调用者和被调用者解耦目的,与工厂模式相比解耦更彻底。
关于IOC的更多信息可以参考文章
http://www.martinfowler.com/articles/injection.html
相关资源下载:
http://www.springframework.net/
程序也可以从这里下载
http://sourceforge.net/projects/springnet/
这个例子演示一下动物吃草,狗吃肉,羊吃草,例子中可能有不当之处,仅仅是为了演示,请勿钻牛角尖
1、新建一个项目,引用Spring.Core;
2、新建两接口:IAnimal和IFood
public interface IAnimal
{
string Eat(int qty);//吃多少
}
public interface IFood
{
string Name//食物名
{
get;
set;
}
string Unit//食物的计量单位
{
get;
}
}
3、新建两动物:Dog和Sheep
public class Dog : IAnimal
{
private IFood _food;
private string _name;
public Dog(string name)//狗名
{
_name = name;
}
public IFood Food//吃的
{
set
{
_food = value;
}
get
{
return _food;
}
}
public string Eat(int qty)//吃了啥
{
return string.Format("Dog {0} ate {1} {2} {3}",_name,qty,_food.Unit,_food.Name);
}
}
public class Sheep : IAnimal
{
...//同Dog,如果你愿意,可以重构一下用继承:)
public string Eat(int qty)
{
return string.Format("Cat {0} ate {1} {2} {3}",_name,qty,_food.Unit,_food.Name);
}
}
4。新建两配置文件:app.config和sheep.config
app.config内容:
<?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>
<description>An example that demonstrates simple IoC features.</description>
<!--
动物接口的具体实现,狗名旺财,吃鲜猪肉
-->
<object name="anAnimal" type="SpringTest.Dog, SpringTest">
<constructor-arg index="0">
<value>WangCai</value>
</constructor-arg>
<property name="food">
<ref object="trueFood" />
</property>
</object>
<!--
实现真正的食物
-->
<object name="trueFood" type="SpringTest.Meat, SpringTest">
<property name="Name">
<value>Fresh Pig Meat</value>
</property>
</object>
</objects>
</spring>
</configuration>
sheep.config内容:
<?xml version="1.0" encoding="utf-8" ?>
<objects>
<description>An example that demonstrates simple IoC features.</description>
<!--
动物接口的具体实现,多利羊吃草
-->
<object name="anAnimal" type="SpringTest.Sheep, SpringTest">
<constructor-arg index="0">
<value>Dolly</value>
</constructor-arg>
<property name="food">
<ref object="trueFood" />
</property>
</object>
<object name="trueFood" type="SpringTest.Grass, SpringTest">
<property name="Name">
<value>Green Grass</value>
</property>
</object>
</objects>
5、新建一窗体,放两按钮,一文本框
using System.Configuration;
using Spring.Context;
using Spring.Context.Support;
加入方法:
private void Display(IApplicationContext ctx)
{
try
{
IAnimal a = ctx.GetObject("anAnimal") as IAnimal;
this.textBox1.Text = a.Eat(3);
}
catch (Exception ex)
{
this.textBox1.Text = ex.Message;
}
}
按钮事件:
Display(ConfigurationSettings.GetConfig("spring/context") as IApplicationContext);
Display(new XmlApplicationContext("sheep.config"));
附:如果需要原程序,请去油箱csharpdp@163.com获取,为防误删除,放在已删除文件夹,口令为csharpdp。
如果运行时出错并显示..Spring.Core强名称...请运行sn -Vr *,65e474d141e25e07