1

我正在創建一個ASP.NET MVC 3電子商務網站,我目前正在管理區域中添加/編輯產品。爲產品頁面創建用戶界面我正在使用Telerik MVC控件。TabStrip中的多個telerik MVC網格不支持ninject和實體框架,工作單元,存儲庫模式

我的問題是,當我加了第二Telerik的網格,其均通過Ajax調用從數據庫中檢索數據,我收到下面列出了幾個不同的錯誤:

{"There is already an open DataReader associated with this Command which must be closed first."}

{"The connection was not closed. The connection's current state is connecting."}

數據庫上下文代碼

public interface IUnitOfWork 
{ 
    void Commit(); 
} 

public class GSPDataContext : DbContext, IUnitOfWork 
{ 
    /* (omitted) IDbSet's for entities */ 
    public GSPDataContext() 
     : base("GSPConnectionString") 
    { 

    } 

    public virtual IDbSet<T> DbSet<T>() where T : class 
    { 
     return Set<T>(); 
    } 

    public virtual void Commit() 
    { 
     base.SaveChanges(); 
    } 
} 

通用庫代碼

public class Repository<T> : IRepository<T> where T : class 
{ 
    private GSPDataContext m_dataContext; 
    private readonly IDbSet<T> m_entity; 

    public Repository(GSPDataContext dataContext) 
    { 
     if (dataContext == null) 
      throw new ArgumentException(); 

     m_dataContext = dataContext; 

     m_entity = m_dataContext.Set<T>(); 
    } 

    public T GetById(int id) 
    { 
     return this.m_entity.Find(id); 
    } 

    public void Insert(T entity) 
    { 
     if (entity == null) 
      throw new ArgumentException(); 

     this.m_entity.Add(entity); 

     //this.m_dataContext.SaveChanges(); 
    } 

    public void Delete(T entity) 
    { 
     if (entity == null) 
      throw new ArgumentException(); 

     this.m_entity.Remove(entity); 

     //this.m_dataContext.SaveChanges(); 
    } 

    public virtual IQueryable<T> Table 
    { 
     get 
     { 
      return this.m_entity; 
     } 
    } 
} 

Ninject代碼

private static void RegisterServices(IKernel kernel) 
    { 
     //Customer 
     kernel.Bind<IAddressValidationService>().To<AddressValidationService>().InRequestScope(); 
     kernel.Bind<ICustomerService>().To<CustomerService>().InRequestScope(); 
     kernel.Bind<ICustomerProductService>().To<CustomerProductService>().InRequestScope(); 

     //Authentication 
     kernel.Bind<IOpenIDLoginService>().To<OpenIDLoginService>().InRequestScope(); 
     kernel.Bind<IAuthenticationService>().To<FormsAuthenticationService>().InRequestScope(); 

     //Products 
     kernel.Bind<IProductService>().To<ProductService>().InRequestScope(); 
     kernel.Bind<IRecentlyViewedProductService>().To<RecentlyViewedProductService>().InRequestScope(); 
     kernel.Bind<IProductPictureService>().To<ProductPictureService>().InRequestScope(); 
     kernel.Bind<ICategoryService>().To<CategoryService>().InRequestScope(); 
     kernel.Bind<IPictureService>().To<PictureService>().InRequestScope(); 

     //Shopping Cart 
     kernel.Bind<IShoppingCartService>().To<ShoppingCartService>().InRequestScope(); 

     //Shipping and Payment 
     kernel.Bind<IShippingService>().To<ShippingService>().InRequestScope(); 
     kernel.Bind<IPaymentService>().To<PaymentService>().InRequestScope(); 

     //Orders 
     kernel.Bind<IOrderCalculationService>().To<OrderCalculationService>().InRequestScope(); 
     kernel.Bind<IOrderProcessingService>().To<OrderProcessingService>().InRequestScope(); 
     kernel.Bind<IOrderService>().To<OrderService>().InRequestScope(); 

     // 
     kernel.Bind<IEncryptionService>().To<EncryptionService>().InRequestScope(); 
     kernel.Bind<ILogger>().To<LoggingService>().InRequestScope(); 
     kernel.Bind<IWebManager>().To<WebManager>().InRequestScope(); 

     //Messages 
     kernel.Bind<IEmailService>().To<EmailService>().InRequestScope(); 
     kernel.Bind<IMessageTemplateService>().To<MessageTemplateService>().InRequestScope(); 
     kernel.Bind<IWorkflowMessageService>().To<WorkflowMessageService>().InRequestScope(); 

     //Data 
     kernel.Bind<GSPDataContext>().ToSelf().InSingletonScope(); 
     kernel.Bind<IUnitOfWork>().ToMethod(ctx => ctx.Kernel.Get<GSPDataContext>()).InSingletonScope(); 
     kernel.Bind(typeof (IRepository<>)).To(typeof (Repository<>)).InRequestScope(); 

     kernel.Bind<IWorkContext>().To<WebWorkContext>().InRequestScope(); 
    } 

我懷疑它是與如何ninject是管理各種服務的壽命,但我不知道我需要做的,使其工作。

任何意見將不勝感激。

感謝

UPDATE

據聖雷莫的評論我改變我的代碼如下:

//Data 
    kernel.Bind<GSPDataContext>().ToSelf().InRequestScope(); 
    kernel.Bind<IUnitOfWork>().ToMethod(ctx => ctx.Kernel.Get<GSPDataContext>()).InRequestScope(); 
    kernel.Bind(typeof (IRepository<>)).To(typeof (Repository<>)).InRequestScope(); 

,現在我收到以下錯誤:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

有什麼建議嗎?

回答

1

不,它與Ninject如何管理生命週期無關。但它必須做你如何配置生命週期。

爲每個請求使用新的DbContext是很重要的。這必須是InRequestScope。