大话spring.net之IOC

      在学习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文件里给出定义的。

原文地址:https://www.cnblogs.com/zfyouxi/p/5137442.html