2015-10-09 68 views
0

我正在使用nhibernate和ninject的存儲庫/工作模式單元。我有一個通用的存儲庫,工作單元提供會話工廠。到目前爲止,它一直在努力工作,但現在我已經遇到了困難。我的項目現在要求我有第二個數據庫。我無法圍繞如何使數據庫的回購/工作單元通用化。下面是從here獲得我的代碼,:使用ninject和nhibernate使用repo模式的多個數據庫

庫:

public interface IUnitOfWork 
{ 
    void BeginTransaction(); 
    void Commit(); 
} 

public class UnitOfWork : IUnitOfWork 
{ 
    private static readonly ISessionFactory _sessionFactory; 
    private ITransaction _transaction; 

    public ISession Session { get; private set; } 

    static UnitOfWork() 
    { 
     // Initialise singleton instance of ISessionFactory, static constructors are only executed once during the 
     // application lifetime - the first time the UnitOfWork class is used 

     _sessionFactory = Fluently.Configure() 
         .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("CONN"))) 
         .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.Load("MyAssembly"))) 
         .CurrentSessionContext<WebSessionContext>() 
         .BuildSessionFactory(); 
    } 

    public UnitOfWork() 
    { 
     Session = _sessionFactory.OpenSession(); 
    } 

    public void BeginTransaction() 
    { 
     _transaction = Session.BeginTransaction(); 
    } 

    public void Commit() 
    { 
     try 
     { 
      _transaction.Commit(); 
     } 
     catch 
     { 
      _transaction.Rollback(); 
      throw; 
     } 
     finally 
     { 
      Session.Close(); 
     } 
    } 
} 

我唯一的想法是有獨立的倉庫和工作類的單位爲每個:

public interface IRepository<T> where T : class 
{ 
    IQueryable<T> GetAll(); 
    T GetById(Guid id); 
    void Create(T entity); 
    void Update(T entity); 
    void Delete(Guid id); 
} 

public class Repository<T> : IRepository<T> where T : class 
{ 
    private UnitOfWork _unitOfWork; 
    public Repository(IUnitOfWork unitOfWork) 
    { 
     _unitOfWork = (UnitOfWork)unitOfWork; 
    } 

    protected ISession Session { get { return _unitOfWork.Session; } } 

    // CRUD operations... 
} 

工作單位數據庫。這對我來說似乎很難看,因爲它會造成大量的代碼重複。有沒有辦法使數據庫級別的repo/uow通用以及實體類型?

我看到的其他選項是NCommon可能能夠爲我處理這個問題。如果建議這樣做,我願意採取這種方式,但是我還沒有準備好立即就寢,因爲該項目在2年內還沒有更新。

回答

1

我花了一段時間,但這裏是我想出了的情況下,任何人都需要這樣的:

修改回購:

public interface IRepository<TEntity, TContext> where TEntity : class where TContext : DatabaseContext 
{ 
    IQueryable<TEntity> GetAll(); 
    TEntity GetById(Guid id); 
    void Create(TEntity entity); 
    void Update(TEntity entity); 
    void Delete(Guid id); 
} 

public class Repository<TEntity, TContext> : IRepository<TEntity, TContext> where TEntity : class where TContext : DatabaseContext 
{ 
    private UnitOfWork<TContext> _unitOfWork; 
    public Repository(IUnitOfWork<TContext> unitOfWork) 
    { 
     _unitOfWork = (UnitOfWork<TContext>)unitOfWork; 
    } 

    protected ISession Session { get { return _unitOfWork.Session; } } 

    public IQueryable<TEntity> GetAll() 
    { 
     return Session.Query<TEntity>(); 
    } 

    public TEntity GetById(Guid id) 
    { 
     return Session.Get<TEntity>(id); 
    } 

    public void Create(TEntity entity) 
    { 
     Session.Save(entity); 
    } 

    public void Update(TEntity entity) 
    { 
     Session.Update(entity); 
    } 

    public void Delete(Guid id) 
    { 
     Session.Delete(Session.Load<TEntity>(id)); 
    } 
} 

修改工作單元:

public class UnitOfWork<TContext> : IUnitOfWork<TContext> where TContext : DatabaseContext 
{ 
    private ISessionFactory _sessionFactory; 
    private ITransaction _transaction; 

    public ISession Session { get; private set; } 

    public UnitOfWork(TContext context) 
    { 
     if (_sessionFactory == null) 
     { 
      _sessionFactory = context.GetSessionFactory(); 
     } 

     Session = _sessionFactory.OpenSession(); 

    } 

    public void BeginTransaction() 
    { 
     _transaction = Session.BeginTransaction(); 
    } 

    public void Commit() 
    { 
     try 
     { 
      _transaction.Commit(); 
     } 
     catch 
     { 
      _transaction.Rollback(); 
      throw; 
     } 
     finally 
     { 
      Session.Close(); 
     } 
    } 
} 

DatabaseContext:

public interface DatabaseContext 
{ 
    ISessionFactory GetSessionFactory(); 
} 

public class QualityControlDatabaseContext : DatabaseContext 
{ 
    public ISessionFactory GetSessionFactory() 
    { 

     return Fluently.Configure() 
         .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("QCConnection"))) 
         .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.Load("QCEntities"))) 
         .CurrentSessionContext<WebSessionContext>() 
         .BuildSessionFactory(); 
    } 
} 
public class SAPDatabaseContext : DatabaseContext 
{ 
    public ISessionFactory GetSessionFactory() 
    { 

     return Fluently.Configure() 
         .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("SAPConnection"))) 
         .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.Load("SAPEntities"))) 
         .CurrentSessionContext<WebSessionContext>() 
         .BuildSessionFactory(); 
    } 
} 
相關問題