我的存储库如下所示:
public class SqlRepository<T> : IRepository<T> where T : class
{
private ExchangeSiteContext _dbContext;
private DbSet<T> _dbSet;
#region [Constructor]
public SqlRepository(ExchangeSiteContext context)
{
_dbContext = context;
_dbSet = context.Set<T>();
}
#endregion
/// <summary>
/// Gets the DbContext of the repository
/// </summary>
public ExchangeSiteContext DbContext
{
get
{
return this._dbContext;
}
}
/// <summary>
/// Get a list of entities
/// </summary>
/// <returns>List of type T entities</returns>
public IQueryable<T> GetList()
{
return this._dbSet.AsQueryable();
}
/// <summary>
/// Get a list of entities by a predicate
/// </summary>
/// <param name="predicate">The predicate</param>
/// <returns>IQueryable of T</returns>
public IQueryable<T> GetList(Expression<Func<T, bool>> predicate)
{
return this._dbSet.Where(predicate).AsQueryable();
}
...
...
}
我的工作单元是这样的:
public class SqlUnitOfWork : IUnitOfWork, IDisposable
{
#region [Private Variables]
private ExchangeSiteContext _dbContext;
private BicycleSellerListingRepository _bicycleSellerListingRepository;
private UserProfileRepository _userProfileRepository;
#endregion
#region [Constructor]
public SqlUnitOfWork()
{
this._dbContext = new ExchangeSiteContext();
}
#endregion
#region [Custom Repositories]
public BicycleSellerListingRepository BicycleSellerListingRepository
{
get
{
if (this._bicycleSellerListingRepository == null)
this._bicycleSellerListingRepository = new BicycleSellerListingRepository(this._dbContext);
return this._bicycleSellerListingRepository;
}
}
public UserProfileRepository UserProfileRepository
{
get
{
if (this._userProfileRepository == null)
this._userProfileRepository = new UserProfileRepository(this._dbContext);
return this._userProfileRepository;
}
}
#endregion
///
/// Generic repository
///
public SqlRepository<T> GenericRepository<T>() where T : class
{
return new SqlRepository<T>(this._dbContext);
}
public void Commit()
{
this._dbContext.SaveChanges();
}
public void Dispose()
{
this._dbContext.Dispose();
this._dbContext = null;
}
}
我的存储库都是通用的。我的工作单元有一些自定义存储库,主要用于我无法执行通用操作的情况。
我的问题是,这看起来对吗?我以前从未创建过存储库或工作单元。这似乎工作得很好,但我不确定我是否忽略了一些东西。