Skip to content

Commit 03a24fb

Browse files
committed
Add DbFactory to GenerateCrudServices
1 parent 4146d43 commit 03a24fb

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

MyApp/_pages/autoquery/autogen.md

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,28 @@ As you'll need to use 2 terminal windows, I'd recommend opening the project with
3535
code NorthwindAuto
3636
:::
3737

38+
### Register DB Connection
39+
3840
The important parts of this project is the registering the OrmLite DB Connection, the above configuration and the local **northwind.sqlite** database, i.e:
3941

4042
```csharp
4143
container.AddSingleton<IDbConnectionFactory>(c =>
4244
new OrmLiteConnectionFactory(MapProjectPath("~/northwind.sqlite"), SqliteDialect.Provider));
45+
```
46+
47+
When using [Endpoint Routing](/endpoint-routing) the DB Factory also needs to be initialized on `GenerateCrudServices`, e.g:
48+
49+
```csharp
50+
var dbFactory = new OrmLiteConnectionFactory(
51+
context.Configuration.GetConnectionString("DefaultConnection"),
52+
SqliteDialect.Provider);
53+
container.AddSingleton<IDbConnectionFactory>(c => dbFactory);
4354

4455
Plugins.Add(new AutoQueryFeature {
4556
MaxLimit = 1000,
46-
GenerateCrudServices = new GenerateCrudServices {}
57+
GenerateCrudServices = new GenerateCrudServices {
58+
DbFactory = dbFactory,
59+
}
4760
});
4861
```
4962

@@ -181,6 +194,7 @@ This is what the magical `AutoRegister` flag does for us:
181194
```csharp
182195
Plugins.Add(new AutoQueryFeature {
183196
GenerateCrudServices = new GenerateCrudServices {
197+
DbFactory = dbFactory,
184198
AutoRegister = true,
185199
//....
186200
}
@@ -216,15 +230,25 @@ x mix autocrudgen sqlite northwind.sqlite
216230
Which will mix in the [autocrudgen](https://gist.github.com/gistlyn/464a80c15cb3af4f41db7810082dc00c) gist to enable AutoQuery and tell it to Auto Generate AutoQuery and CRUD Services for all tables in the registered RDBMS (default schema):
217231

218232
```csharp
219-
public class ConfigureAutoQuery : IConfigureAppHost
233+
public class ConfigureDb : IHostingStartup
220234
{
221-
public void Configure(IAppHost appHost)
235+
public void Configure(IWebHostBuilder builder)
222236
{
223-
appHost.Plugins.Add(new AutoQueryFeature {
224-
MaxLimit = 1000,
225-
GenerateCrudServices = new GenerateCrudServices {
226-
AutoRegister = true
227-
}
237+
builder.ConfigureServices((context,services) =>
238+
{
239+
var dbFactory = new OrmLiteConnectionFactory(
240+
context.Configuration.GetConnectionString("DefaultConnection"),
241+
SqliteDialect.Provider);
242+
243+
services.AddSingleton<IDbConnectionFactory>(dbFactory);
244+
245+
services.AddPlugin(new AutoQueryFeature {
246+
MaxLimit = 1000,
247+
GenerateCrudServices = new GenerateCrudServices {
248+
DbFactory = dbFactory,
249+
AutoRegister = true
250+
}
251+
});
228252
});
229253
}
230254
}
@@ -237,8 +261,7 @@ The [sqlite](https://gist.github.com/gistlyn/768d7b330b8c977f43310b954ceea668) g
237261
public void Configure(IServiceCollection services)
238262
{
239263
services.AddSingleton<IDbConnectionFactory>(new OrmLiteConnectionFactory(
240-
Configuration.GetConnectionString("DefaultConnection")
241-
?? "northwind.sqlite",
264+
Configuration.GetConnectionString("DefaultConnection"),
242265
SqliteDialect.Provider));
243266
}
244267
```
@@ -420,6 +443,7 @@ With this you could have a single API Gateway Servicifying access to multiple Sy
420443
```csharp
421444
Plugins.Add(new AutoQueryFeature {
422445
GenerateCrudServices = new GenerateCrudServices {
446+
DbFactory = dbFactory,
423447
CreateServices = {
424448
new CreateCrudServices(),
425449
new CreateCrudServices { Schema = "AltSchema" },
@@ -474,6 +498,7 @@ Plugins.Add(new AutoQueryFeature {
474498
MaxLimit = 100,
475499
GenerateCrudServices = new GenerateCrudServices
476500
{
501+
DbFactory = dbFactory,
477502
ServiceFilter = (op,req) =>
478503
{
479504
// Require all Write Access to Tables to be limited to Authenticated Users
@@ -638,6 +663,7 @@ You can change each of these default conventions with the new `GenerateOperation
638663
Plugins.Add(new AutoQueryFeature {
639664
MaxLimit = 1000,
640665
GenerateCrudServices = new GenerateCrudServices {
666+
DbFactory = dbFactory,
641667
AutoRegister = true,
642668
GenerateOperationsFilter = ctx => {
643669
if (ctx.TableName == "applications")

0 commit comments

Comments
 (0)