標籤:style blog http io ar color 使用 sp for
一、建立一個項目,命名為:Service
二、添加介面及其實現
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Model;namespace Service{ public interface IUserService { object Save(Users entity); }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Model;namespace Service{ public class UserService:IUserService { public object Save(Users entity) { throw new NotImplementedException(); } }}
由於用到Model層中Users對象,所以要引入一下項目Model,然後
1 using Model;
二、設定檔
<?xml version="1.0" encoding="utf-8" ?><objects xmlns="http://www.springframework.net"> <object id="transactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate21"> <property name="DbProvider" ref="DbProvider"/> <!--HibernateTransactionManager通過注入sessionfactory. 然後在得到session,把session封裝成SessionHolder(),並通過threadlocal來對象的實現和線程的綁定(threadlocal實現重點) 最後到線程中的session取得的Transaction--> <property name="SessionFactory" ref="NHibernateSessionFactory"/> </object> <!-- 事務攔截器,啟用交易管理員所必須的 --> <!-- Spring.net使用TransactionInterceptor聲明式事物配置--> <object id="transactionInterceptor" type="Spring.Transaction.Interceptor.TransactionInterceptor, Spring.Data"> <property name="TransactionManager" ref="transactionManager"/> <property name="TransactionAttributeSource"> <object type="Spring.Transaction.Interceptor.AttributesTransactionAttributeSource, Spring.Data"/> </property> </object> <!--設定TransactionProxyFactoryObject事物代理,添加需要攔截的方法--> <!--如果你不喜歡使用特性來標註事務([Transaction]),Spring.NET為NHibernate提供的事務代理是 TransactionProxyFactoryObject。--> <object id="BaseTransactionManager" type="Spring.Transaction.Interceptor.TransactionProxyFactoryObject, Spring.Data" abstract="true"> <!--spring擷取hibernate的事物管理--> <property name="PlatformTransactionManager" ref="transactionManager"/> <property name="TransactionAttributes"> <name-values> <add key="Save*" value="PROPAGATION_REQUIRED"/> <!--key屬性為Save*,意思是攔截所有以Save開頭的方法--> <add key="Set*" value="PROPAGATION_REQUIRED"/> <add key="Finish*" value="PROPAGATION_REQUIRED"/> <add key="Update*" value="PROPAGATION_REQUIRED"/> <add key="Delete*" value="PROPAGATION_REQUIRED"/> <add key="Add*" value="PROPAGATION_REQUIRED"/> <add key="Get*" value="PROPAGATION_SUPPORTS,readOnly"/> <add key="Find*" value="PROPAGATION_SUPPORTS,readOnly"/> <add key="Load*" value="PROPAGATION_SUPPORTS,readOnly"/> <add key="*" value="PROPAGATION_REQUIRED"/> </name-values> </property> </object> <!--為rolesService.cs類添加TransactionProxyFactoryObject事物代理--> <object id="rolesService" parent="BaseTransactionManager"> <!--將 TransactionProxyFactoryObject的 Target屬性注入業務處理層的類,這樣Spring.NET會為該類封裝上事務--> <property name="Target"> <object type="Serivce.rolesService,Manager"> <property name="RolesDAO" ref="dao.roles"/> </object> </property> </object> <object id="UsersManager" parent="BaseTransactionManager"> <property name="Target"> <object type="Manager.UsersManager,Manager"> <property name="UsersDAO" ref="dao.users"/> </object> </property> </object> <object id="PermissionManager" parent="BaseTransactionManager"> <property name="Target"> <object type="Manager.PermissionManager,Manager"> <property name="PermissionDAO" ref="dao.permission"/> </object> </property> </object></objects>
最後,設定設定檔屬性:
SSH.net之Service層