Where to put sql when using dapper?

前端 未结 3 1721
野的像风
野的像风 2021-02-07 00:04

I\'m using dapper for a mvc3 project at work, and I like it. However, how are you supposed to layer the application when using dapper? Currently I just have all my sql stuffed d

3条回答
  •  梦毁少年i
    2021-02-07 00:40

    Using a resource file is really useful for us. We create .sql files in a folder call /Sql and drag them into the 'Files' section of our SqlResource object. The 'Strings' section of the resource file is really clean and easy for smaller snippets of sql (e.g. functions we may be querying).

    So, our sql looks like:

    var reports = conn.Query(SqlResource.Blahs_get, new {parentId, region});
    

    This keeps the repositories real clean. And there are additional benefits to having all of your sql in a resource file in that you can iterate over the entries and potentially query the db with PARSEONLY to make sure that if db objects change your queries would break (note that this is mostly but not 100% reliable).

    So, to conclude, for us Resource files keep things real clean, but to Marc Gravell's point they are not for reusability within the production code...each sql statement should only be used by one point in your application.

提交回复
热议问题