Spring.net架构示例(含Aop和Ioc)源码

时间:2022-12-24 02:03:08

最近写了一个Spring.net的架构。

一、架构主图

Spring.net架构示例(含Aop和Ioc)源码

架构图的数据流程走向是:

UI层=》UILogic》=》Service》Business=》DataAccess

中间使用到的公共类库,如,Aop和Container

二、Default.aspx页面代码

<%@ Page Language="C#" AutoEventWireup="true"  Inherits="ST.Spring.UILogic.UserManage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div
</div>
</form>
</body>
</html>

举一个ioc配置示例:

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net" > <description>An example that demonstrates simple IoC features.</description>
<!-- 环绕通知 -->
<object id="UserAdvice" type="ST.Spring.Aop.UserAdvice,ST.Spring.Aop"></object> <!-- UserBll class -->
<object id="IUserBll" type="Spring.Aop.Framework.ProxyFactoryObject">
<!-- 拦截目标 -->
<property name="target" ref="UserBll" />
<!-- 具体通知 -->
<property name="interceptorNames">
<list>
<value>UserAdvice</value>
</list>
</property>
</object>
<object id="UserBll" type="ST.Spring.Business.UserBll,ST.Spring.Business">
</object> </objects>

IUserBll是接口名,继承了是Spring.Aop.Framework.ProxyFactoryObject的ProxyFactoryObject类,被UserBll引用或者说实现。UserBll继承了ST.Spring.Business.UserBll类。

一句话,ioc就是为了在.cs业务类中,通过接口名创建实现对象。

var userBll = ObjectContainer.CreateObject<IUserBll>();

三、UILogic页面的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ST.Spring.Container;
using ST.Spring.DataModel;
using ST.Spring.IService; namespace ST.Spring.UILogic
{
public class UserManage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var userService = ObjectContainer.CreateObject<IUserService>();
User result = userService.AddUser(new User() { UserName = "xqh", Sex = "男", Password = "123" }); //Response.Write(user.Singleton() + "<br/>");
Response.Write("添加用户:" + (result != null ? "成功" : "失败") + "<br />");
////Response.Write(result.UserName + result.Sex + result.Password + "<br/>");
//Response.Write(user.SendMsg());
Response.End();
}
}
}

四、IService.cs和Service.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ST.Spring.DataModel; namespace ST.Spring.IService
{
public interface IUserService
{
/// <summary>
/// 添加用户
/// </summary>
/// <param name="u"></param>
/// <returns></returns>
User AddUser(User u); /// <summary>
/// 发送消息
/// </summary>
/// <returns></returns>
string SendMsg();
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ST.Spring.Container;
using ST.Spring.DataModel;
using ST.Spring.IBusiness;
using ST.Spring.IService; namespace ST.Spring.Service
{
/// <summary>
/// 用户service
/// </summary>
public class UserService : IUserService
{
private IUserBll UserBll { get; set; }
private string _msg;
private string _constructor; //构造函数注入
public UserService(string constructor)
{
this._constructor = constructor;
} //通过属性注入 相当于替代了 IUser _user = new UserDAO把这个过程交给容器来处理 /// <summary>
/// 消息内容通过属性注入
/// </summary>
public string MsgInfo
{
get { return this._msg; }
set { this._msg = value; }
} /// <summary>
/// 添加用户
/// </summary>
/// <param name="u"></param>
/// <returns></returns>
public User AddUser(User u)
{
var userBll = ObjectContainer.CreateObject<IUserBll>();
return userBll.AddUser(u);
} /// <summary>
/// 发送消息
/// </summary>
/// <returns></returns>
public string SendMsg()
{
return "注入的MsgInfo值是:" + MsgInfo + "<br/>" + "_constructor通过构造函数注入的值:" + _constructor;
}
}
}

五、Aop类UserAdvice.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AopAlliance.Intercept; namespace ST.Spring.Aop
{
/// <summary>
/// 环绕通知
/// </summary>
public class UserAdvice : IMethodInterceptor
{
#region IMethodInterceptor 成员 public object Invoke(IMethodInvocation invocation)
{
string methodName = invocation.Method.Name; //可以拦截方法进行日志、权限等操作
if (methodName == "AddUser")
{
//throw new Exception(invocation.Method.Name);
}
object returnValue = invocation.Proceed();
//return "被拦截的方法:" + methodName + "<br/>" + "返回的值:" + returnValue.ToString();
return returnValue;
} #endregion
}
}

六、IOC容器创建类ObjectContainer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Context;
using Spring.Context.Support; namespace ST.Spring.Container
{
public static class ObjectContainer
{
//使用自定义的配置文件---获取容器
private static IApplicationContext Ctx { get; set; } public static TObject CreateObject<TObject>()
{
Ctx = new XmlApplicationContext(string.Format("~/SpringConfig/{0}.xml", typeof(TObject).Assembly.GetName().Name)); var str = string.Empty;
var t = typeof(TObject).FullName;
if (t.LastIndexOf('.') > 0)
{
str = t.Substring(t.LastIndexOf('.') + 1, t.Length - t.LastIndexOf('.') - 1);
}
return (TObject)Ctx.GetObject(str);
}
}
}

创建方法如下:

var userService = *OBJECTCONTAINER.CREATEOBJECT<IUSERSERVICE>();*
User result = userService.AddUser(new User() { UserName = "xqh", Sex = "男", Password = "123" });

下载:http://pan.baidu.com/s/1hqCGuFU