简体   繁体   English

使用Linq to SQL存储库可能的性能注意事项

[英]Possible Performance Considerations using Linq to SQL Repositories

I have an ASP.NET MVC application that uses Linq to SQL repositories for all interactions with the database. 我有一个ASP.NET MVC应用程序,它使用Linq到SQL存储库进行与数据库的所有交互。

To deal with data security, I do trimming to filter data to only those items to which the user has access. 为了处理数据安全性,我会修剪以仅将数据过滤到用户有权访问的项目。 This occurs in several places: 这发生在几个地方:

  1. Data in list views 列表视图中的数据
  2. Links in a menu bar 菜单栏中的链接
  3. A treeview on the left hand side containing links to content 左侧的树视图包含指向内容的链接
  4. Role-based security 基于角色的安全性
  5. A special security attribute, inheriting from AuthorizeAttribute , that implements content-based authorization on every controller method. 一种特殊的安全属性,继承自AuthorizeAttribute ,在每个控制器方法上实现基于内容的授权。

Each of these places instantiates a repository, which opens a Linq to Sql DataContext and accesses the database. 这些位置中的每一个都实例化一个存储库,该存储库打开Linq to Sql DataContext并访问数据库。 So, by my count, each request for a page access opens at least six separate Linq to SQL DataContexts. 因此,根据我的统计,每个页面访问请求至少打开六个独立的Linq to SQL DataContexts。

Should I be concerned about this from a performance perspective, and if so, what can be done to mitigate it? 我是否应该从绩效角度关注这一点,如果是这样,可以采取哪些措施来缓解这种情况?

Good question, I don't believe you're going to have anymore issues here than with normal query operation. 好问题,我不相信你在这里会遇到比普通查询操作更多的问题。 The translation from Linq Expressions to sql queries is a relatively involved process, but will probably pale in comparison to the actual execution of the query and network latency. 从Linq Expressions到sql查询的转换是一个相对复杂的过程,但与查询的实际执行和网络延迟相比可能会变得苍白。

In almost all of my MVC applications I use a BaseController. 在几乎所有的MVC应用程序中,我都使用BaseController。 Typically I will use a factory to create an instance of the DataContext in the BaseController constructor (unit tests pass in a mock factory instance, the null constructor results in a default instance of the factory being created). 通常,我将使用工厂在BaseController构造函数中创建DataContext的实例(单元测试在模拟工厂实例中传递,null构造函数导致正在创建的工厂的默认实例)。 While not strictly necessary, I do Dispose the created DataContext in OnResultExecuted in the BaseController. 虽然不是绝对必要,但我会在BaseController中将OnResultExecuted中创建的DataContext处理掉。 Both the "default" data context and the factory are exposed as protected properties on the BaseController class so they can be used by all inheriting controllers. “默认”数据上下文和工厂都在BaseController类上公开为受保护属性,因此所有继承控制器都可以使用它们。

If I need some operations to take place outside the "default" DataContext, I simply use the context factory to create a new, separate instance as necessary. 如果我需要在“默认”DataContext之外进行某些操作,我只需使用上下文工厂根据需要创建一个新的独立实例。

I confess that I do this mostly for convenience and code legibility. 我承认我这样做主要是为了方便和代码易读性。 I think the data contexts themselves are lightweight enough that I don't gain much performance. 我认为数据上下文本身足够轻巧,我没有获得太多性能。 I no longer, however, have to write the using (var context = ... code in every action method. 但是,我不再需要在每个操作方法中编写using (var context = ... code)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM