您的位置:首页 > 运维架构

深入浅出AOP(二)--IOC容器

2015-06-28 22:23 387 查看
上一篇,用的静态代理实现了AOP,实际上,AOP就是一种思想,实现的方式有很多种,而要实现AOP,将提供的非业务类的方法(服务类)放在容器中,更加高级一点。

IOC就是提供了一种容器。

AOP+IOC实现:

整体的解决方案:



在这个里面,我们首先写Model:

<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Text;

namespace Spring.Demo.Model
{
/// <summary>
/// 用户类
/// </summary>
public class Users
{
/// <summary>
/// 编号
/// </summary>
private int _oid;
public int Oid
{
get { return _oid; }
set { _oid = value; }
}

/// <summary>
/// 姓名
/// </summary>
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}

/// <summary>
/// 性别
/// </summary>
private string _sex;
public string Sex
{
get { return _sex; }
set { _sex = value; }
}

/// <summary>
/// 年龄
/// </summary>
private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
}
}</span>


在写Service:

<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Text;
namespace Spring.Demo.Service
{
public interface IUsers
{
/// <summary>
/// 返回用户的详细信息的方法
/// </summary>
/// <returns></returns>
string GetUserInfo();
}
}
</span>


写Conpontext类:

<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Text;
using Spring.Demo.Service;
using Spring.Demo.Model;

namespace Spring.Demo.Compontext
{
public class UsersCompontents : IUsers
{
public UsersCompontents()
{ }

#region 获取用户信息
public string GetUserInfo()
{
Users user = new Users();
user.Oid = 1;
user.Name = "Beniao";
user.Sex = "Boy";
user.Age = 25;

return string.Format("编号:{0}--->姓名:{1}--->性别:{2}--->年龄:{3}",
user.Oid,
user.Name,
user.Sex,
user.Age);
}
#endregion
}
}</span>


在测试中首先建立APP.config文件,该文件划定了Spring容器要盛放的对象:

<span style="font-size:18px;"><?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>
<!--这的配置根据实际的程序来的,UsersCompontents是程序集Spring.Demo.Compontext下的一个类-->
<object name="Users"
type="Spring.Demo.Compontext.UsersCompontents,Spring.Demo.Compontext">
</object>
</objects>
</spring>
</configuration></span>


在测试Porgram中:

<span style="font-size:18px;">using Spring.Context;
using Spring.Demo.Service;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace Sping.Demo.SimpleTest
{
class Program
{
static void Main(string[] args)
{
//从config文件中取得程序集信息
IApplicationContext context = ConfigurationManager.GetSection("spring/context")
as IApplicationContext;
//调用方法
//Users为config文件里的配置节
//<object name="Users"
//        type="Spring.Demo.Compontext.UsersCompontents,Spring.Demo.Compontent">
//</object>
IUsers user = context.GetObject("Users") as IUsers;
Console.WriteLine(user.GetUserInfo());
Console.Read();
}
}
}
</span>


有了容器,我们的AOP就可以从将服务了都放在容器中了。

AOP就变成了这样:



我们将serivce都放在容器中,用的时候,直接在容器中进行配置就可以了,这样,就减少了AOP和Service之间的耦合。

IOC容器的两个接口:



未完待续。。。。。。

参阅博客:http://www.csharpwin.com/csharpspace/3183.shtml
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  aop