码迷,mamicode.com
首页 > Web开发 > 详细

SSH.net之Service层

时间:2014-12-02 20:34:29      阅读:198      评论:0      收藏:0      [点我收藏+]

标签: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>

 

 

最后,设置配置文件属性:

bubuko.com,布布扣

SSH.net之Service层

标签:style   blog   http   io   ar   color   使用   sp   for   

原文地址:http://www.cnblogs.com/lippor/p/4138225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!