一、新建一个项目,命名为:Service,添加对项目DAO,Model的引用。引入:Spring.Aop.dll,Spring.Core.dll,Spring.Data.dll,Spring.Data.NHibernate21.dll,Spring.Web.dll 文件
二、添加接口及其实现
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Model; namespace Service { public interface IUserService { object Save(Users entity);
Users Get(object id); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Model; using Spring.Transaction.Interceptor; namespace Service { public class UserService : IUserService { public DAO.IRepository<Model.Users> UsersRepository { get; set; } public object Save(Users entity) { return UsersRepository.Save(entity); } public Users Get(object id) { return UsersRepository.Get(id); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Service { public interface IRolesService { } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Service { public class RolesService:IRolesService {public DAO.IRepository<Model.Roles> RolesRepository { get; set; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Service { public interface IPermissionService { } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Service { public class PermissionService:IPermissionService {
public DAO.IRepository<Model.Permission> PermissionRepository { get; set; }
}
}
由于用到Model层中Users对象,所以要引入一下项目Model,然后
1 using Model;
二、配置文件
1 <?xml version="1.0" encoding="utf-8" ?> 2 <objects xmlns="http://www.springframework.net"> 3 4 <object id="transactionManager" 5 type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate21"> 6 <property name="DbProvider" ref="DbProvider"/> 7 <!--HibernateTransactionManager通过注入sessionfactory. 8 然后在得到session,把session包装成SessionHolder(),并通过threadlocal来对象的实现和线程的绑定(threadlocal实现重点) 9 最后到线程中的session取得的Transaction--> 10 <property name="SessionFactory" ref="NHibernateSessionFactory"/> 11 </object> 12 13 <!-- 事务拦截器,激活事务管理器所必须的 --> 14 <!-- Spring.net使用TransactionInterceptor声明式事物配置--> 15 <object id="transactionInterceptor" type="Spring.Transaction.Interceptor.TransactionInterceptor, Spring.Data"> 16 <property name="TransactionManager" ref="transactionManager"/> 17 <property name="TransactionAttributeSource"> 18 <object type="Spring.Transaction.Interceptor.AttributesTransactionAttributeSource, Spring.Data"/> 19 </property> 20 </object> 21 22 <!--设置TransactionProxyFactoryObject事物代理,添加需要拦截的方法--> 23 <!--如果你不喜欢使用特性来标注事务([Transaction]),Spring.NET为NHibernate提供的事务代理是 TransactionProxyFactoryObject。--> 24 <object id="BaseTransactionManager" type="Spring.Transaction.Interceptor.TransactionProxyFactoryObject, Spring.Data" abstract="true"> 25 <!--spring获取hibernate的事物管理--> 26 <property name="PlatformTransactionManager" ref="transactionManager"/> 27 <property name="TransactionAttributes"> 28 <name-values> 29 <add key="Save*" value="PROPAGATION_REQUIRED"/> 30 <!--key属性为Save*,意思是拦截所有以Save开头的方法--> 31 <add key="Set*" value="PROPAGATION_REQUIRED"/> 32 <add key="Finish*" value="PROPAGATION_REQUIRED"/> 33 <add key="Update*" value="PROPAGATION_REQUIRED"/> 34 <add key="Delete*" value="PROPAGATION_REQUIRED"/> 35 <add key="Add*" value="PROPAGATION_REQUIRED"/> 36 <add key="Get*" value="PROPAGATION_SUPPORTS,readOnly"/> 37 <add key="Find*" value="PROPAGATION_SUPPORTS,readOnly"/> 38 <add key="Load*" value="PROPAGATION_SUPPORTS,readOnly"/> 39 <add key="*" value="PROPAGATION_REQUIRED"/> 40 </name-values> 41 </property> 42 </object> 43 44 <!--为rolesService.cs类添加TransactionProxyFactoryObject事物代理--> 45 <object id="RolesService" parent="BaseTransactionManager"> 46 <!--将 TransactionProxyFactoryObject的 Target属性注入业务处理层的类,这样Spring.NET会为该类包装上事务--> 47 <property name="Target"> 48 <object type="Service.RolesService,Service"> 49 <property name="RolesRepository" ref="dao.roles"/><!--name对应RolesService.cs文件中RolesRepository属性--> 50 </object> 51 </property> 52 </object> 53 54 <object id="UsersService" parent="BaseTransactionManager"> 55 <property name="Target"> 56 <object type="Service.UserService,Service"> 57 <property name="UsersRepository" ref="dao.users"/> 58 </object> 59 </property> 60 </object> 61 62 <object id="PermissionService" parent="BaseTransactionManager"> 63 <property name="Target"> 64 <object type="Service.PermissionService,Service"> 65 <property name="PermissionRepository" ref="dao.permission"/> 66 </object> 67 </property> 68 </object> 69 70 </objects>
最后,设置配置文件属性: