Dashboard > iBATIS DataMapper > Home > Frequently Asked Questions > Is there a simple way to use transactions in my service layer without having to use a seperate dao.config file
Is there a simple way to use transactions in my service layer without having to use a seperate dao.config file
Added by Ron Grabowski, last edited by Nathan Maves on Sep 15, 2005  (view change)
Labels: 
(None)


The NPetshop and NPetshop2 sample applications use a service class to broker calls from the presentation layer to the persistance layer. An example UserService is below:

UserService.cs
public class UserService : BaseService
    {
    private static UserService instance = new UserService();
    private IUserDao userDao = null;
    
    private UserService()
    {
    userDao = base.daoManager.GetDao(typeof(IUserDao)) as IUserDao;
    }
    
    public static UserService GetInstance()
    {
    return instance;
    }
    
    public bool Update(User user)
    {
    if (user== null)
    {
    throw new ArgumentNullException("user");
    }
    
    base.daoManager.BeginTransaction();
    try
    {
    bool result = (userDao.Update(user) == 1);
    base.daoManager.CommitTransaction();
    return result;
    }
    catch
    {
    base.daoManager.RollBackTransaction();
    throw;
    }
    }

Calls to userDao are intercepted by a code proxy and invoked against the implementation defined in the dao.config file. If you're not using the DataAccess framework expliclity (i.e. no dao.config file) and would still like support for transactions in your service layer, calls to transaction methods (Begin, Commit, Rollback, etc.) can be pushed back into the BaseService class:

BaseService.cs
public abstract class BaseService
    {
    public virtual void BeginTransaction()
    {
    IBatisNet.DataMapper.Mapper.Instance().BeginTransaction();
    }
    
    public virtual void CommitTransaction()
    {
    IBatisNet.DataMapper.Mapper.Instance().CommitTransaction();
    }
    
    public virtual void RollBackTransaction()
    {
    IBatisNet.DataMapper.Mapper.Instance().RollBackTransaction();
    }
    }
UserService.cs
public class UserService : BaseService
    {
    private static UserService instance = new UserService();
    private IUserDao userDao = null;
    
    private UserService()
    {
    userDao = new UserSqlMapDao();
    }
    
    public static UserService GetInstance()
    {
    return instance;
    }
    
    public bool Update(User user)
    {
    if (user== null)
    {
    throw new ArgumentNullException("user");
    }
    
    base.BeginTransaction();
    try
    {
    bool result = (userDao.Update(user) == 1);
    base.CommitTransaction();
    return result;
    }
    catch
    {
    base.RollBackTransaction();
    throw;
    }
    }

Notice that when userDao is assigned to a new instance of a concrete class, the code proxy is bypassed.


Site running on a free Atlassian Confluence Open Source Project License granted to OSS. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators