You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+58-56Lines changed: 58 additions & 56 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,22 @@
1
1
# Shuttle.Core.Data
2
2
3
-
Provides a thin abstraction over ADO.NET.
3
+
```
4
+
PM> Install-Package Shuttle.Core.Data
5
+
```
4
6
5
-
The following may be found on the [documentation site](http://shuttle.github.io/shuttle-core/overview-data/):
7
+
Provides a thin abstraction over ADO.NET.
6
8
7
9
# Overview
8
10
9
-
The `Shuttle.Core.Data` package provides a thin abstraction over ADO.NET by making use of the `DbProviderFactories`. Even though it provides object/relational mapping mechasnims it is in no way an ORM.
11
+
The `Shuttle.Core.Data` package provides a thin abstraction over ADO.NET by making use of the `DbProviderFactories` (see `Shuttle.Core.Data.SqlClient` for .Net Core Provider Factory adapter). Even though it provides object/relational mapping mechanisms it is in no way an ORM.
10
12
11
13
# IDatabaseContextFactory
12
14
13
15
As per usual, in order to access a database, we need a database connection. A database connection is represented by a `IDatabaseContext` instance that may be obtained by using an instance of an `IDatabaseContextFactory` implementation.
14
16
15
-
The `DatabaseContextFactory`implmentation makes use of an `IDbConnectionFactory` implementation, that creates a `System.Data.IDbConnection` by using the provider name and connection string, an `IDbCommandFactory` that creates a `System.Data.IDbCommand` by using `IDbConnection` instance. The `DatabaseContextFactory` also requires an instance of a `IDatabaseContextCache` that stores connections and is assigned to the `DatabaseContext` in order to obtain the active connection.
17
+
The `DatabaseContextFactory`implementation makes use of an `IDbConnectionFactory` implementation, that creates a `System.Data.IDbConnection` by using the provider name and connection string, an `IDbCommandFactory` that creates a `System.Data.IDbCommand` by using `IDbConnection` instance. The `DatabaseContextFactory` also requires an instance of a `IDatabaseContextCache` that stores connections and is assigned to the `DatabaseContext` in order to obtain the active connection.
16
18
17
-
~~~c#
19
+
```c#
18
20
varfactory=DatabaseContextFactory.Default();
19
21
20
22
using (varcontext=factory.Create("connectionStringName"))
@@ -33,19 +35,19 @@ using (var context = factory.Create(existingIDbConnection))
33
35
{
34
36
// database interaction
35
37
}
36
-
~~~
38
+
```
37
39
38
40
# IConfiguredDatabaseContextFactory
39
41
40
42
You can pre-configure your database context factory using this interface. If you typically connect to only one data source this may be helpful:
@@ -55,49 +57,49 @@ The following section each describe the methods available in the `IDatabaseGatew
55
57
56
58
## GetReaderUsing
57
59
58
-
~~~c#
60
+
```c#
59
61
IDataReaderGetReaderUsing(IQueryquery);
60
-
~~~
62
+
```
61
63
62
64
Returns an `IDataReader` instance for the given `select` statement:
63
65
64
-
~~~c#
66
+
```c#
65
67
varfactory=DatabaseContextFactory.Default();
66
68
vargateway=newDatabaseGateway();
67
69
68
70
using (varcontext=factory.Create("connectionStringName"))
69
71
{
70
72
varreader=gateway.GetReaderUsing(RawQuery.Create("select Id, Username from dbo.Member"));
71
73
}
72
-
~~~
74
+
```
73
75
74
76
## ExecuteUsing
75
77
76
-
~~~c#
78
+
```c#
77
79
intExecuteUsing(IQueryquery);
78
-
~~~
80
+
```
79
81
80
82
Executes the given query and returns the number of rows affected:
81
83
82
-
~~~c#
84
+
```c#
83
85
varfactory=DatabaseContextFactory.Default();
84
86
vargateway=newDatabaseGateway();
85
87
86
88
using (varcontext=factory.Create("connectionStringName"))
87
89
{
88
90
gateway.ExecuteUsing(RawQuery.Create("delete from dbo.Member where Username = 'mr.resistor'"));
89
91
}
90
-
~~~
92
+
```
91
93
92
94
## GetScalarUsing<T>
93
95
94
-
~~~c#
96
+
```c#
95
97
TGetScalarUsing<T>(IQueryquery);
96
-
~~~
98
+
```
97
99
98
100
Get the scalar value returned by the `select` query. The query shoud return only one value (scalar):
99
101
100
-
~~~c#
102
+
```c#
101
103
varfactory=DatabaseContextFactory.Default();
102
104
vargateway=newDatabaseGateway();
103
105
@@ -111,54 +113,54 @@ using (var context = factory.Create("connectionStringName"))
111
113
RawQuery.Create("select Id from dbo.Member where Username = 'mr.resistor'")
112
114
);
113
115
}
114
-
~~~
116
+
```
115
117
116
118
## GetDataTableFor
117
119
118
-
~~~c#
120
+
```c#
119
121
DataTableGetDataTableFor(IQueryquery);
120
-
~~~
122
+
```
121
123
122
124
Returns a `DataTable` containing the rows returned for the given `select` statement.
123
125
124
-
~~~c#
126
+
```c#
125
127
varfactory=DatabaseContextFactory.Default();
126
128
vargateway=newDatabaseGateway();
127
129
128
130
using (varcontext=factory.Create("connectionStringName"))
129
131
{
130
132
vartable=gateway.GetDataTableFor(RawQuery.Create("select Id, Username from dbo.Member"));
131
133
}
132
-
~~~
134
+
```
133
135
134
136
## GetRowsUsing
135
137
136
-
~~~c#
138
+
```c#
137
139
IEnumerable<DataRow>GetRowsUsing(IQueryquery);
138
-
~~~
140
+
```
139
141
140
142
Returns an enumerable containing the `DataRow` instances returned for a `select` query:
141
143
142
-
~~~c#
144
+
```c#
143
145
varfactory=DatabaseContextFactory.Default();
144
146
vargateway=newDatabaseGateway();
145
147
146
148
using (varcontext=factory.Create("connectionStringName"))
147
149
{
148
150
varrows=gateway.GetRowsUsing(RawQuery.Create("select Id, Username from dbo.Member"));
149
151
}
150
-
~~~
152
+
```
151
153
152
154
153
155
## GetSingleRowUsing
154
156
155
-
~~~c#
157
+
```c#
156
158
DataRowGetSingleRowUsing(IQueryquery);
157
-
~~~
159
+
```
158
160
159
161
Returns a single `DataRow` containing the values returned for a `select` statement that returns exactly one row:
160
162
161
-
~~~c#
163
+
```c#
162
164
varfactory=DatabaseContextFactory.Default();
163
165
vargateway=newDatabaseGateway();
164
166
@@ -168,7 +170,7 @@ using (var context = factory.Create("connectionStringName"))
168
170
RawQuery.Create("select Id, Username, EMail, DateActivated from dbo.Member where Id = 10")
169
171
);
170
172
}
171
-
~~~
173
+
```
172
174
173
175
# IDataRepository<T>
174
176
@@ -178,51 +180,51 @@ The following methods can be used to interact with your object type.
178
180
179
181
## FetchAllUsing
180
182
181
-
~~~c#
183
+
```c#
182
184
IEnumerable<T>FetchAllUsing(IQueryquery);
183
-
~~~
185
+
```
184
186
185
187
Uses the `select` clause represented by the `IQuery` instance to create a list of objects of type `T`. The `select` clause will need to select all the required columns and will, typically, return more than one instance.
186
188
187
189
## FetchItemUsing
188
190
189
-
~~~c#
191
+
```c#
190
192
TFetchItemUsing(IQueryquery);
191
-
~~~
193
+
```
192
194
193
195
Returns a single object instance of type `T` that is hydrated using the data returned from the `select` clause represented by the `IQuery` instance.
This is similar to the `FetchAllUsing` method but instead returns a list of `MappedRow<T>` instances. Uses the `select` clause represented by the `IQuery` instance to create a list of `MappedRow` instances of type `T`. The `select` clause will need to select all the required columns and will, typically, return more than one instance.
202
204
203
205
## FetchMappedRowUsing
204
206
205
-
~~~c#
207
+
```c#
206
208
MappedRow<T>FetchMappedRowUsing(IQueryquery);
207
-
~~~
209
+
```
208
210
209
211
Similar to the `FetchItemUsing` method but instead return a `MappedRow<T>` instance that is hydrated using the data returned from the `select` clause represented by the `IQuery` instance.
210
212
211
213
## Contains
212
214
213
-
~~~c#
215
+
```c#
214
216
boolContains(IQueryquery);
215
-
~~~
217
+
```
216
218
217
219
Returns `true` is the `IQuery` instance `select` clause returns an `int` scalar that equals `1`; else returns `false`.
218
220
219
221
# IQuery
220
222
221
223
An `IQuery` represent a database query that can be executed against the relevant database type. There is only one method that needs to be implemented:
222
224
223
-
~~~c#
225
+
```c#
224
226
voidPrepare(IDbCommandcommand);
225
-
~~~
227
+
```
226
228
227
229
This should ensure that the given `IDbCommand` is configured for execution by setting the relvant command attributes and parameters.
228
230
@@ -236,27 +238,27 @@ There are two implementations of this interface.
236
238
237
239
The `RawQuery` enables you to create any query using the native language structure:
238
240
239
-
~~~c#
241
+
```c#
240
242
varquery=RawQuery.Create("select UserName from dbo.Member where Id = @Id")
Typically you would not want to create a `MappedColumn` each time you need it and these are also quite fixed. A column mapping can, therefore, by defined statically:
@@ -356,7 +358,7 @@ An `IAssembler` implementation is used to create multiple mappings with as few c
356
358
357
359
Using a `MappedData` instance we can keep adding the `MappedRow` instances to the `MappedData` and then have the assembler return the three `Order` aggregates:
358
360
359
-
~~~c#
361
+
```c#
360
362
publicclassOrderAssembler : IAssembler<Order>
361
363
{
362
364
publicIEnumerable<Order> Assemble(MappedDatadata)
@@ -381,4 +383,4 @@ public class OrderAssembler : IAssembler<Order>
0 commit comments