2
使用的UnitOfWork模式我在MVC項目落實的工作格局的單位,哪些選項應該由Ninject
public class UnitOfWork : IUnitOfWork
{
private TransactionScope _transaction;
private readonly BodoShopEntities _db;
public UnitOfWork()
{
_db = new BodoShopEntities();
}
public void Dispose()
{
if (_db != null)
{
_db.Dispose();
}
System.GC.SuppressFinalize(this);
}
public void StartTransaction()
{
_transaction = new TransactionScope();
}
public void Commit()
{
_db.SaveChanges();
_transaction.Complete();
}
public DbContext Db
{
get { return _db; }
}
}
而且存儲庫模式,
public partial interface IRepository<T> where T : class
{
T GetById(object id);
void Insert(T entity);
void Update(T entity);
void Delete(T entity);
IQueryable<T> Table { get; }
T Single(object primaryKey);
T SingleOrDefault(object primaryKey);
bool Exists(object primaryKey);
IUnitOfWork UnitOfWork { get; }
}
在Ninject我用InThreadScope工作的單位,這是對的嗎?
private static void RegisterServices(IKernel kernel)
{
kernel.Bind(typeof(IUnitOfWork)).To(typeof(UnitOfWork)).InThreadScope();
}
使用InThreadScope ASP.NET應用程序中是一個非常糟糕的主意:http://stackoverflow.com/questions/14591422/why-is-perthreadlifetimemanager-在此示例中使用 – Steven
您可能還會發現此Q/A提供信息:http://stackoverflow.com/questions/10585478/one-dbcontext-per-web-request-why – Steven