使用Spring.Net 1.3.2实现Container(IoC)(一)IoC、DI、Spring.NET、Ioc Container、 ClassicContainer

IoC(Inversion of Control,控制反转)

IoC别名DI(Dependency Injection,依赖注入)。Spring将IoC和DI看作等同的概念。
IoC是一种架构模式。
IoC其原理是基于OO设计原则的The Hollywood Principle(好莱坞原则):Don't call us, we'll call you(你不用找我们,我们会找你的),就是由容器来控制业务对象之间的依赖关系,而非传统实现中,由代码直接操控,这也就是所谓“控制反转”的概念所在,控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。控制权的转移带来的好处就是降低了业务对象之间的依赖程度。

Spring.NET

Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序。它提供了很多方面的功能,比如依赖注入、面向方面编程(AOP)、数据访问抽象及ASP.NET扩展等等。Spring.NET以Java版的Spring框架为基础,将Spring.Java的核心概念与思想移植到了.NET平台上。

Spring.NET的IoC容器

一、编程方式的容器

在 Spring.NET 中,对于通过编程方式使用容器的环境,提供了 Spring.Context.Support.StaticApplicationContext,我们可以直接创建这个容器,并加入一些配置。

Spring.Context.Support.StaticApplicationContext context 
= new Spring.Context.Support.StaticApplicationContext();
context.RegisterPrototype("album1", typeof(Album), null);
Album album = (Album)context.GetObject("album1");
 
二、Xml 方式的容器
在开发中,我们通常通过 XML 配置文件来完成配置。Spring.NET 提供了 Spring.Context.Support.XmlApplicationContext,此时,对象的配置信息写在一个 xml 的配置文件中,当然了,这个配置文件有特定的格式,这些规定以 Xml Schema 的形式保存在 Spring.NET\doc\schema 文件夹的 spring-objects-1.3.xsd 中。
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object id="album1" type="Album"></object>
<object id="album2" type="Album"></object>
</objects>
 
Spring.Context.Support.XmlApplicationContext context
= new Spring.Context.Support.XmlApplicationContext("objects.xml");
Album album = (Album)context.GetObject("album1");
 

MVCQuick的IoC容器

实现代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Spring.Context.Support;
using Spring.Objects;
using System.Collections;

namespace MVCQuick.Framework.Container
{
public class ClassicContainer
{
private static StaticApplicationContext context;

static ClassicContainer()
{
context = new StaticApplicationContext();
}

public static void Register<T>()
{
Register<T>(false);
}

public static void Register<T>(bool isSingleton)
{
if (!isSingleton)
{
context.RegisterPrototype(typeof(T).FullName, typeof(T), new MutablePropertyValues());
}
else
{
context.RegisterSingleton(typeof(T).FullName, typeof(T), new MutablePropertyValues());
}
}

public static void Register<T>(string name)
{
Register<T>(name, false);
}

public static void Register<T>(string name, bool isSingleton)
{
if (!isSingleton)
{

context.RegisterPrototype(name, typeof(T), new MutablePropertyValues());
}
else
{
context.RegisterSingleton(name, typeof(T), new MutablePropertyValues());
}
}

public static void Register<T>(string name, IDictionary properties)
{
Register<T>(name, properties, false);
}

public static void Register<T>(string name, IDictionary properties, bool isSingleton)
{
if (!isSingleton)
{
context.RegisterPrototype(name, typeof(T), new MutablePropertyValues(properties));
}
else
{
context.RegisterSingleton(name, typeof(T), new MutablePropertyValues(properties));
}
}

public static object GetObject(string name)
{
return context.GetObject(name);
}

public static object GetObject<T>()
{
var objects = context.GetObjectsOfType(typeof(T));

if (objects.Count > 0)
{
return objects.Cast<DictionaryEntry>().First().Value;
}

return null;
}

public static object GetObject(Type type)
{
var objects = context.GetObjectsOfType(type);

if (objects.Count > 0)
{
return objects.Cast<DictionaryEntry>().First().Value;
}

return null;
}

public static IDictionary GetObjects<T>()
{
return context.GetObjectsOfType(typeof(T));
}

public static IDictionary GetObjects(Type type)
{
return context.GetObjectsOfType(type);
}

public static object ConfigureObject(object target, string name)
{
return context.ConfigureObject(target, name);
}
}
}

单元测试代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using MVCQuick.Framework.Container;
using System.Collections;

namespace MVCQuick.Framework.Tests
{
[TestFixture]
public class ContainerTests
{
[Test]
public void Test1()
{
ClassicContainer.Register<Album>("album1");
object o1 = ClassicContainer.GetObject("album1");
Assert.IsNotNull(o1);
Album album1 = o1 as Album;
Assert.IsNotNull(album1);

IDictionary propertyValues2 = new Hashtable();
propertyValues2.Add("Id", 1);
propertyValues2.Add("Title", "海阔天空");
ClassicContainer.Register<Album>("album2", propertyValues2);
Album album2 = ClassicContainer.GetObject("album2") as Album;
Assert.IsNotNull(album2);
Assert.AreEqual(album2.Id, 1);
Assert.AreEqual(album2.Title, "海阔天空");

ClassicContainer.Register<List<Album>>("albumList3");
List<Album> albumList3 = ClassicContainer.GetObject("albumList3") as List<Album>;
Assert.IsNotNull(albumList3);
albumList3.Add(album2);
Assert.AreEqual(albumList3[0].Id, 1);

}
}
}

 

源代码下载:http://mvcquick.codeplex.com/  

posted @ 2011-10-16 15:30  GuYoung  阅读(1868)  评论(1编辑  收藏  举报