NHibernate在一个事务中保存并提交

时间:2013-08-21 11:30:19

标签: c# nhibernate unit-of-work

我有以下代码创建一个新书签并向其添加一个或多个标签。如果标签尚不存在,则会创建标签并将其添加到书签中。

Bookmark bookmark = new Bookmark();
bookmark.Title = request.Title;
bookmark.Link = request.Link;
bookmark.DateCreated = request.DateCreated;
bookmark.DateModified = request.DateCreated;
bookmark.User = _userRepository.GetUserByUsername(request.Username);

IList<Tag> myTags = _tagRepository.GetTags(request.Username);
IList<string> myTagsToString = myTags.Select(x => x.Title).ToList<string>();

foreach (var tag in request.Tags)
{
    if (myTagsToString.Contains(tag))
    {
        Tag oldTag = myTags.SingleOrDefault(x => x.Title == tag);
        bookmark.Tags.Add(oldTag);
    }
    else
    {
        Tag newTag = new Tag();
        newTag.Title = tag;
        newTag.User = _userRepository.GetUserByUsername(request.Username);
        newTag.DateCreated = request.DateCreated;
        newTag.DateModified = request.DateCreated;
        bookmark.Tags.Add(newTag);
    }
}

_bookmarkRepository.Add(bookmark);
_uow.Commit();

我实施了工作单元,但我不确定我是否正确地执行了此操作。我使用save方法后跟提交。 save方法将书签和标签插入数据库,commit方法对联结表进行插入(书签中有许多标签和标签有很多书签)。

所以一切都正确插入。但是如果我删除了提交方法,书签和标签仍会被插入。但他们没有插入连接表。这是否意味着这些插入不在同一个事务中,因为不需要提交将书签和标签保存到数据库中?只有在保存标签和书签之间的关系时才需要提交。

编辑:

存储库

public abstract class Repository<T, TEntityKey> where T : IAggregateRoot
{
    private IUnitOfWork _uow;

    public Repository(IUnitOfWork uow)
    {
        _uow = uow;
    }

    public void Save(T entity)
    {
        SessionFactory.GetCurrentSession().SaveOrUpdate(entity);
    }

    public void Add(T entity)
    {
        SessionFactory.GetCurrentSession().Save(entity);
    }

    public void Remove(T entity)
    {
        SessionFactory.GetCurrentSession().Delete(entity);
    }

    public IEnumerable<T> FindAll()
    {
        ICriteria criteriaQuery = SessionFactory.GetCurrentSession().CreateCriteria(typeof(T));

        return (List<T>)criteriaQuery.List<T>();
    }
}

的UnitOfWork

public class NHUnitOfWork : IUnitOfWork
{
    public void RegisterAmended(IAggregateRoot entity)
    {
        SessionFactory.GetCurrentSession().SaveOrUpdate(entity);
    }

    public void RegisterNew(IAggregateRoot entity)
    {
        SessionFactory.GetCurrentSession().Save(entity);
    }

    public void RegisterRemoved(IAggregateRoot entity)
    {
        SessionFactory.GetCurrentSession().Delete(entity);
    }

    public void Commit()
    {
        using (ITransaction transaction = SessionFactory.GetCurrentSession().BeginTransaction())
        {
            try
            {
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw;
            }
        }
    }
}

BookmarkRepository

public class BookmarkRepository : Repository<Bookmark, int>, IBookmarkRepository
{
    public BookmarkRepository(IUnitOfWork uow)
        : base(uow)
    {
    }
}

更新: 我将NHUnitOfWork更改为:

public class NHUnitOfWork : IUnitOfWork
{
    private ITransaction _transaction;

    public void RegisterAmended(IAggregateRoot entity)
    {
        SessionFactory.GetCurrentSession().SaveOrUpdate(entity);
    }

    public void RegisterNew(IAggregateRoot entity)
    {
        SessionFactory.GetCurrentSession().Save(entity);
    }

    public void RegisterRemoved(IAggregateRoot entity)
    {
        SessionFactory.GetCurrentSession().Delete(entity);
    }

    public void BeginTransaction()
    {
        _transaction = SessionFactory.GetCurrentSession().BeginTransaction();
    }

    public void Commit()
    {
        using (_transaction)
        {
            try
            {
                _transaction.Commit();
            }
            catch (Exception ex)
            {
                _transaction.Rollback();
                throw;
            }
        }
    }
}

所以我可以这样使用它:

_uow.BeginTransaction();

Bookmark bookmark = new Bookmark();
bookmark.Title = request.Title;
bookmark.Link = request.Link;
bookmark.DateCreated = request.DateCreated;
bookmark.DateModified = request.DateCreated;
bookmark.User = _userRepository.GetUserByUsername(request.Username);

IList<Tag> myTags = _tagRepository.GetTags(request.Username);
IList<string> myTagsToString = myTags.Select(x => x.Title).ToList<string>();

foreach (var tag in request.Tags)
{
    if (myTagsToString.Contains(tag))
    {
        Tag oldTag = myTags.SingleOrDefault(x => x.Title == tag);
        bookmark.Tags.Add(oldTag);
    }
    else
    {
        Tag newTag = new Tag();
        newTag.Title = tag;
        newTag.User = _userRepository.GetUserByUsername(request.Username);
        newTag.DateCreated = request.DateCreated;
        newTag.DateModified = request.DateCreated;
        bookmark.Tags.Add(newTag);
    }
}

_bookmarkRepository.Add(bookmark);
_uow.Commit();

我在NHUnitOfWork实现中添加了一个begin transaction。这意味着我需要在任何select或insert之前调用_uow.BeginTransaction()并在最后调用_uow.Commit()。如果我看一下NHibernate Profiler,这似乎有效。如果这是错的,请告诉我:)

1 个答案:

答案 0 :(得分:0)

您的Save方法未在事务中执行,因为您在Commit方法中关闭并打开事务。

正确的实施方式是在调用Save之前启动事务