相关疑难解决方法(0)

如何实现与EF和NHibernate一起使用的工作单元

我正在开发一个工作单元实现,它在Entity Framework 4.1和NHibernate中都有效.在下面找到我的实现细节的框架

IUnitOfWork定义

public interface IUnitOfWork
{
    IRepository<LogInfo> LogInfos { get; }
    IRepository<AppInfo> AppInfos { get; }
    void Commit();
    void Rollback();
}
Run Code Online (Sandbox Code Playgroud)

IRepository定义

public interface IRepository<T> where T : class, IEntity
{
    IQueryable<T> FindAll();
    IQueryable<T> FindWhere(Expression<Func<T, bool>> predicate);
    T FindById(int id);
    void Add(T newEntity);
    void Remove(T entity);
}
Run Code Online (Sandbox Code Playgroud)

在NHibernate中实现UoW

public class NHibernateUnitOfWork : IUnitOfWork, IDisposable
{
    public ISession Session { get; private set; }

    public NHibernateUnitOfWork(ISessionFactory sessionFactory)
    {
        _sessionFactory = sessionFactory;
        Session = _sessionFactory.OpenSession();
        _transaction = Session.BeginTransaction();
    }

    public …
Run Code Online (Sandbox Code Playgroud)

linq nhibernate unit-of-work entity-framework-4

15
推荐指数
2
解决办法
3319
查看次数