简体   繁体   English

我应该在MVC 4中将SQL查询存储在哪里?

[英]Where should I store SQL queries in MVC 4?

I have a MVC4-based project with some SQL-queries. 我有一个基于MVC4的项目,带有一些SQL查询。 They are currently stored in project resources (*.resx), but it is very hard to edit the queries inside the resource editor. 它们当前存储在项目资源(* .resx)中,但是很难在资源编辑器中编辑查询。

Is there a specialized SQL query storage? 有专门的SQL查询存储吗? where should I store my queries? 我应该在哪里存储查询?

You can put your queries in your database as stored procedures. 您可以将查询作为存储过程放入数据库中。 This gives you encapsulation of your database logic - a 'separation of concerns'. 这使您可以封装数据库逻辑-“关注点分离”。

No, there is no specialized storage for SQL queries. 不,没有用于SQL查询的专用存储。

Resource files sounds a bit unusual, I would rather put them in constant strings in my code, encapsulated in a datalayer. 资源文件听起来有点不寻常,我宁愿将它们放在代码中的常量字符串中,封装在数据层中。 Possible within each method that uses the SQL queries where parameters are applied, etc. 在使用SQL查询的每种方法中都可以应用参数,等等。

However I would really recommend avoiding plain old SQL and use Entity Framework or LINQ2SQL instead. 但是,我确实建议避免使用普通的旧SQL,而应使用Entity Framework或LINQ2SQL。

I solved it with T4 . 我用T4解决了。 Now I have Queries directory in my project and SQLGenerator.tt in it. 现在,我的项目中有Queries目录,其中有SQLGenerator.tt Here is my template source code: 这是我的模板源代码:

<#@ template  debug="true" hostSpecific="true" #>
<#@ output extension=".cs" #>
<#@ Assembly Name="System.Core" #>
<#@ Assembly Name="System.Windows.Forms" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #> 

<#var sqlScriptsContent = ReadSql(); #>

namespace MathApplication.SQL
{
public class Queries
{
<# foreach(var file in sqlScriptsContent) { #>
    public string <#= file.Key #> = @"<#= file.Value #>";
<# } #>
}
}

<#+ 
public Dictionary<string, string> ReadSql() {
    var filePaths = Directory.GetFiles(Host.ResolvePath("."));

    var result = new Dictionary<string, string>();

    foreach (var filename in filePaths) {
        if (filename.EndsWith(".sql")) {
            result[Path.GetFileNameWithoutExtension(filename)] = System.Text.RegularExpressions.Regex.Replace(File.ReadAllText(filename), @"\s+", " ").Replace("\"", "'");
        }
    }

    return result;
    }
#>

This code assembles all *.sql files in Queries directory to one class. 此代码将Queries目录中的所有*.sql文件组装为一个类。

Why not to use Entity Framework as ORM? 为什么不将实体框架用作ORM?

It's great technology in a link with ASP.NET MVC. 与ASP.NET MVC链接时,这是一项伟大的技术。

Also you could move SQL to stored procedures in MSSQL. 您也可以将SQL移至MSSQL中的存储过程。 In this case I would suggest to use Database Project to store your SQL in your solution. 在这种情况下,我建议使用数据库项目将SQL存储在解决方案中。

Anyway storing SQL in code or resources is not a great practice. 无论如何,在代码或资源中存储SQL并不是一个好习惯。

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

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