-
Notifications
You must be signed in to change notification settings - Fork 2
The generated code
Query first generated wrappers give you an interface, a repository class and a results poco.
You would never dream of coding without unit tests. The looming presence of the database at the bottom of a call stack has long been the major obstacle to automated testing. Having a generated interface to all your queries dramatically simplifies the implementation of tests for your application code.
If you're not already using a DI container, use poor man's DI. Your app code attacks the generated interface. In your test code, you can simply implement the interface. The editor generates your method stubs and you can instantiate and return some pocos.
ADO provides 3 ways of executing a query: Execute, ExecuteScalar() and ExecuteNonQuery(). QueryFirst adds a fourth: GetOne(). If your query returns no results, you will only see ExecuteNonQuery(). If your query uses output parameters, you won't see GetOne().
For each of these four, we provide four overloads: with or without parameters, and with or without a DB connection.
The two common reasons for managing the connection yourself are as follows:
- You anticipate enormous result sets, and you want to iterate through your results once without bringing the whole set into memory.
- You want to manage manage transactions in your application code. Performance is generally not a good reason for managing the connection. Connection pooling means there is practically no cost to opening and closing a DB connection. Most times then, just use the connection-free overloads.
The overloads with no parameters are just syntax sugar, allowing you to supply parameter values via object initialization. So:
new MySuperQuery()
.Execute(custNum)
// is the same as
new MySuperQuery{CustNum = custNum}
.Execute()
Use whichever variant you prefer, but don't forget to provide values if your query requires them. Object initialization will be preferable if your query has lots of parameters.
A row of results becomes an instance of the results poco. Column values become properties. Database nulls are transposed to c# nulls and vise versa.