|
| 1 | +# Open.Database.Extensions |
| 2 | + |
| 3 | +Useful set of utilities and abstractions for simplifying modern database operations and ensuring dependency injection compatibility. |
| 4 | + |
| 5 | +## Connection Factories |
| 6 | + |
| 7 | +Connection factories facilitate creation and disposal of connections without the concern of a connection reference or need for awareness of a connection string. A `SqlConnectionFactory` is provided and can be overriden to provide more specific dependency injection configurations. |
| 8 | + |
| 9 | +## Expressive Commands |
| 10 | + |
| 11 | +The provided expressive command classes allow for an expressive means to append parameters and execute the results without lenghty complicated setup. |
| 12 | + |
| 13 | +Extensions are provied to create commands from connection factories. |
| 14 | + |
| 15 | +##### Example |
| 16 | + |
| 17 | +```cs |
| 18 | +var result = connectionFactory |
| 19 | + .StoredProcedure("[procedure name]") |
| 20 | + .AddParam("a",1) |
| 21 | + .AddParam("b",true) |
| 22 | + .AddParam("c","hello") |
| 23 | + .ExecuteScalar(); |
| 24 | +``` |
| 25 | + |
| 26 | + |
| 27 | +## Asynchronous |
| 28 | + |
| 29 | +End-to-end asynchronous methods suffixed with `Async`. |
| 30 | + |
| 31 | +When using the SQL Client, asychronous methods are available as well as `.ToTargetBlockAsync<T>(target)` and `.AsSourceBlockAsync<T>()` Dataflow methods. |
| 32 | + |
| 33 | +## Extensions |
| 34 | + |
| 35 | +Instead of writing this: |
| 36 | +```cs |
| 37 | +var myResult = new List<T>(); |
| 38 | +using(var reader = await mySqlCommand.ExecuteReaderAsync(CommandBehavior.CloseConnection)) |
| 39 | +{ |
| 40 | + while(await reader.ReadAsync()) |
| 41 | + list.Add(transform(reader)); |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +Is now simplifed to this: |
| 46 | +```cs |
| 47 | +var myResult = await cmd.ToListAsync(transform); |
| 48 | +``` |
| 49 | + |
| 50 | +## Deferred Transformation |
| 51 | + |
| 52 | +In order to keep connection open time to a minimum, some methods cache data before closing the connection and then subsequently applying the transformations as needed. |
| 53 | + |
| 54 | +#### `Results<T>()` and `ResultsAsync<T>()` |
| 55 | + |
| 56 | +Queues all the data. Then using the provided type `T` entity, the data is coerced by which properties intersect with the ones available to the `IDataReader`. |
| 57 | + |
| 58 | +Optionally a field to column override map can be passed as a parameter. If a column is set as `null` then that field is ignored (not applied to the model). |
| 59 | + |
| 60 | +##### Examples |
| 61 | + |
| 62 | +If all the columns in the database map exactly to a field: (A column that has no associated field/property is ignored.) |
| 63 | +```cs |
| 64 | +var people = cmd.Results<Person>(); |
| 65 | +``` |
| 66 | + |
| 67 | +If the database fields don't map exactly: |
| 68 | + |
| 69 | +```cs |
| 70 | +var people = cmd.Results<Person>( |
| 71 | + (Field:"FirstName", Column:"first_name"), |
| 72 | + (Field:"LastName", Column:"last_name"))); |
| 73 | +``` |
| 74 | +or |
| 75 | +```cs |
| 76 | +var people = cmd.Results<Person>( |
| 77 | + ("FirstName", "first_name"), |
| 78 | + ("LastName", "last_name")); |
| 79 | +``` |
| 80 | +or |
| 81 | +```cs |
| 82 | +var people = cmd.Results<Person>(new Dictionary<string,string>{ |
| 83 | + {"FirstName", "first_name"}, |
| 84 | + {"LastName", "last_name"}); |
| 85 | +``` |
| 86 | + |
| 87 | +#### `Retrieve()` and `RetrieveAsync()` |
| 88 | + |
| 89 | +Queues all the data. Returns a `QueryResult<Queue<object[]>>` containing the requested data and column information. The `.AsDequeueingMappedEnumerable()` extension will iteratively convert the results to dictionaries for ease of access. |
| 90 | + |
| 91 | +#### `AsSourceBlockAsync<T>()` |
| 92 | + |
| 93 | +(Fully asynchronous.) Retuns a Dataflow source block. Then asynchronously buffers and transforms the results allowing for any possible Dataflow configuration. The source block is marked as complete when there are no more results. If the block is somehow marked as complete externally, the flow of data will stop and the connection will close. |
| 94 | + |
| 95 | +### `AsSourceBlockAsync<T>()` versus `ResultsAsync<T>` |
| 96 | + |
| 97 | +Depending on the level of asynchrony in your application, you may want to avoid too much buffering of data. |
| 98 | + |
| 99 | +`AsSourceBlockAsync<T>()` is fully asynchronous from end-to-end and can keep total buffering to a minimum by consuming (receiving) results as fast as possible, but may incur additional latency between reads. |
| 100 | + |
| 101 | +`ResultsAsync<T>()` is fully asynchronous from end-to-end but returns an `IEnumerable<T>` that although has fully buffered the all the data into memory, has deferred the transformation until enumerated. This way, the asynchronous data pipeline is fully complete before synchronously transforming the data. |
| 102 | + |
| 103 | +Both methods ultimately are using a `Queue<object[]>` or `ConcurrentQueue<object[]>` (Dataflow) to buffer the data, but `ResultsAsync<T>()` buffers the entire data set before dequeuing and transforming the results. |
0 commit comments