Monday, January 23, 2017

QueryFirst. Your SQL is Coming OUT.

Query-first is a visual studio extension for working intelligently with SQL in C# projects. Use the provided .sql template to develop your queries. When you save the file, Query-first runs your query, retrieves the schema and generates two classes and an interface: a wrapper class with methods Execute(), ExecuteScalar(), ExecuteNonQuery() etc, its corresponding interface, and a POCO encapsulating a line of results.
Write your SQL in the SQL window! Syntax validated, Intellisense for your tables and columns.

The generated C# wrapper for your query is nested below the query in Solution Explorer
Your query stays intact in its own .sql file. It can be edited, validated and test-run "in-situ" with design-time mock inputs and intellisense for your tables and columns. For performance, it is compiled into your binary as a ManifestResourceStream. In your application code, running your query takes one line of code, and returns a POCO (or an IEnumerable of POCOs) with meaningful parameter and property names, so enabling intellisense for your input parameters and results. The interface and POCO are directly usable for unit testing.

The generated code stands alone. There is no runtime dll and no reflection. The only dependencies are System libraries. You can quietly forget about ADO : Command, Connection, Reader and parameter creation are all handled for you. At no point do you have to remember the name of a column, or its type, or its index in the reader. And you've absolutely nothing new  to learn, provided you still remember how to write SQL :-)

The extension puts a command in your Tools menu that will run all queries in your application and regenerate all wrapper classes. As such, all queries in your app are continuously integration-tested against the dev DB, and changes in your database schema will directly produce compilation errors in your application. If the query is broken, the wrapper classes will not compile. If the query runs but your application code tries to access columns that are no longer present in the result, the error will point at the line in your code that tries to access the missing property.

Interested? Stay tuned :-)