c# - Ninject Web 应用程序 : All bindings should be InRequestScope()?

标签 c# asp.net asp.net-mvc ninject

我有一个用 asp.net mvc 制作的 Web 应用程序,我正在使用 Ninject 来绑定(bind)接口(interface)。

现在,我有这个:

// Db Context
kernel.Bind<DbContext>().To<DbEntities>().InRequestScope();

// Repositories - which are using instance of DbEntities
kernel.Bind<ICustomerRepository>().To<CustomerRepository>();
kernel.Bind<IProductRepository>().To<ProductRepository>();

// Services - which are using instances of Repositories
kernel.Bind<ICustomerService>().To<CustomerService>();
kernel.Bind<IProductService>().To<ProductService>();

我将 DbContext 绑定(bind)到 RequestScope 中的 DbEntities,因为我想在同一个 Web 请求中使用同一个 DbContext。之后它应该处理它。

但是其他绑定(bind)应该如何呢?默认情况下如何?

例如 IProductRepository 有一个 DbContext 的实例(每个请求一个),也应该是 InRequestScope() 吗?

IProductService 有一个 IProductRepository

实例

绑定(bind)应该如何适合网络应用程序? (而且我不会使服务器的内存过载)

最佳答案

对于 MVC 应用程序,您的配置没问题。如果您在默认 transient 范围或请求范围内绑定(bind)您的存储库,则不会有太大区别。正如 @Mark 在 transient 范围内所述,您的依赖项将作为有界对象的新实例注入(inject),但在事务范围内,它们将根据请求创建一次。如果您想在您的存储库上执行(例如)一些按请求缓存,我更喜欢更多的请求范围并推荐它。

关于c# - Ninject Web 应用程序 : All bindings should be InRequestScope()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13393883/

相关文章:

c# - 按对象类型解析服务的设计模式?

c# - lambda 的 Linq 更新问题

c# - 系统参数异常 : Invalid postback or callback argument

c# - 使用 jQuery 文件上传时,Request.Files 为空 File Upload with Internet Explore 6/9

c# - FromBase64String 因 Kendo 图表而失败

asp.net-mvc - 在 HTMLHelper 类中访问 User.Identity.Name

c# - 从 WebRequest 获取查询字符串

c# - 构建前和构建后事件参数

c# - 如何使用同一模型中的两个下拉菜单实例?

asp.net-mvc - 为什么在 MVC 中使用包会导致内存使用增加