Spring.NET学习笔记1——控制反转(基础篇) Level 200

来源:互联网 发布:linux查看普通用户 编辑:程序博客网 时间:2024/05/29 03:29


http://www.cnblogs.com/GoodHelper/archive/2009/10/25/Spring_NET_IoC.html


在学习Spring.NET这个控制反转(IoC)和面向切面(AOP)的容器框架之前,我们先来看一下什么是控制反转(IoC)。

  控制反转(Inversion of Control,英文缩写为IoC),也叫依赖注入(Dependency Injection)。我个人认为控制反转的意思是依赖对象(控制权)发生转变,由最初的类本身来管理依赖对象转变为IoC框架来管理这些对象,使得依赖脱离类本身的控制,从而实现松耦合。

我们先来看一段代码


namespace Dao
{
    
public interface IPersonDao
    
{
        
void Save();
    }


    
public class PersonDao : IPersonDao
    
{
        
public void Save()
        
{
            Console.WriteLine(
"保存 Person");
        }

    }

}


namespace SpringNetIoC
{
    
class Program
    
{
        
private static void NormalMethod()
        
{
            IPersonDao dao 
= new PersonDao();
            dao.Save();
            Console.WriteLine(
"我是一般方法");
        }

    }

}

 

Program必然需要知道IPersonDao接口和PersonDao类。为了不暴露具体实现,我可以运用设计模式中的抽象工厂模式(Abstract Factory)来解决。

 

复制代码
namespace DaoFactory
{
    
public static class DataAccess
    {
        
public static IPersonDao CreatePersonDao()
        {
            
return new PersonDao();
        }
    }
}
复制代码

 


namespace SpringNetIoC
{
    
class Program
    {        
private static void FactoryMethod()
        {
            IPersonDao dao 
= DataAccess.CreatePersonDao();
            dao.Save();
            Console.WriteLine(
"我是工厂方法");
        }
    }
}

这时,Program只需要知道IPersonDao接口和工厂,而不需要知道PersonDao类。然后我们试图想象,要是有这样的工厂框架帮我们管理依赖的对象就好了,于是控制反转出来了。

 


<?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">
      
<description>一个简单的控制反转例子</description>


      
<object id="PersonDao" type="Dao.PersonDao, Dao" />


    
</objects>

  
</spring>

</configuration>

 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dao;
using DaoFactory;
using Spring.Context;
using Spring.Context.Support;

namespace SpringNetIoC
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
//NormalMethod();  // 一般方法
            
//FactoryMethod();  // 工厂方法
            IoCMethod();  // IoC方法"

            Console.ReadLine();
        }

        
private static void NormalMethod()
        {
            IPersonDao dao 
= new PersonDao();
            dao.Save();
            Console.WriteLine(
"我是一般方法");
        }

        
private static void FactoryMethod()
        {
            IPersonDao dao 
= DataAccess.CreatePersonDao();
            dao.Save();
            Console.WriteLine(
"我是工厂方法");
        }

        
private static void IoCMethod()
        {
            IApplicationContext ctx 
= ContextRegistry.GetContext();
            IPersonDao dao 
= ctx.GetObject("PersonDao"as IPersonDao;
            
if (dao != null)
            {
                dao.Save();
                Console.WriteLine(
"我是IoC方法");
            }
        }
    }
}

一个简单的控制反转程序例子就实现了。

这样从一定程度上解决了Program与PersonDao耦合的问题,但是实际上并没有完全解决耦合,只是把耦合放到了XML 文件中,通过一个容器在需要的时候把这个依赖关系形成,即把需要的接口实现注入到需要它的类中。我个人认为可以把IoC模式看做是工厂模式的升华,可以把IoC看作是一个大工厂,只不过这个大工厂里要生成的对象都是在XML文件中给出定义的。

代码下载

我最近也是在学习Spring.NET,如果有和我不同意见的朋友可以给我留言或者发送EMail,我们可以约定时间共同学习和探讨。


0 0