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。

相关问题