我在一个小型 .NET 4.5、MVC4、EF5 项目上使用存储库模式。
我注意到,如果我更改从存储库传递的 IQueryable 对象的结果,则生成的 SQL 不太理想。
例如,在我的存储库中:
public IQueryable<Entry> GetEntries()
{
// (db is an instance of the data context)
return db.Entries.Where(e => e.UserId == WebSecurity.CurrentUserId);
}
在我的控制器中,我限制返回的行数并对其进行排序:
public ActionResult Index()
{
// (repo is an instance of the repository object)
var entries = repo.GetEntries().Take(10).OrderByDescending(o => o.Created)
return View(entries);
}
这会生成以下 SQL:
DECLARE @p__linq__0 int = 1
SELECT
[Project1].[UserId] AS [UserId],
[Project1].[Id] AS [Id],
[Project1].[Created] AS [Created],
[Project1].[LastModified] AS [LastModified],
[Project1].[BodyOriginal] AS [BodyOriginal],
[Project1].[BodyFormatted] AS [BodyFormatted],
[Project1].[FormatterVersion] AS [FormatterVersion],
[Project1].[BodyDigest] AS [BodyDigest],
[Project1].[FollowupId] AS [FollowupId],
[Project1].[AddMethod] AS [AddMethod],
[Project1].[Entry_Id] AS [Entry_Id]
FROM ( SELECT
[Extent1].[Id] AS [Id],
[Extent1].[UserId] AS [UserId],
[Extent1].[Created] AS [Created],
[Extent1].[LastModified] AS [LastModified],
[Extent1].[BodyOriginal] AS [BodyOriginal],
[Extent1].[BodyFormatted] AS [BodyFormatted],
[Extent1].[FormatterVersion] AS [FormatterVersion],
[Extent1].[BodyDigest] AS [BodyDigest],
[Extent1].[FollowupId] AS [FollowupId],
[Extent1].[AddMethod] AS [AddMethod],
[Extent1].[Entry_Id] AS [Entry_Id]
FROM [dbo].[Entries] AS [Extent1]
WHERE [Extent1].[UserId] = @p__linq__0
) AS [Project1]
ORDER BY [Project1].[Created] DESC
如您所见,生成的 SQL 是高度冗余的。
如果我更改存储库方法以包含截断和排序的结果:
public IQueryable<Entry> GetEntries()
{
// (db is an instance of the data context)
return db.Entries.Where(e => e.UserId == WebSecurity.CurrentUserId).Take(10).OrderByDescending(o => o.Created);
}
生成的 SQL 更好:
DECLARE @p__linq__0 int = 1
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[UserId] AS [UserId],
[Extent1].[Created] AS [Created],
[Extent1].[LastModified] AS [LastModified],
[Extent1].[BodyOriginal] AS [BodyOriginal],
[Extent1].[BodyFormatted] AS [BodyFormatted],
[Extent1].[FormatterVersion] AS [FormatterVersion],
[Extent1].[BodyDigest] AS [BodyDigest],
[Extent1].[FollowupId] AS [FollowupId],
[Extent1].[AddMethod] AS [AddMethod],
[Extent1].[Entry_Id] AS [Entry_Id]
FROM [dbo].[Entries] AS [Extent1]
WHERE [Extent1].[UserId] = @p__linq__0
ORDER BY [Extent1].[Created] DESC
我该如何克服这个问题,即利用存储库模式,同时仍然可以灵活地修改存储库的结果,而不会创建结构不佳的 SQL?