Skip to content

Commit d8365e1

Browse files
committed
fix style
1 parent 56ff07d commit d8365e1

File tree

1 file changed

+34
-42
lines changed

1 file changed

+34
-42
lines changed

examples/Linq2db.QuickStart/Program.cs

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using Linq2db.Ydb;
2-
3-
namespace Linq2db.QuickStart;
4-
5-
using Microsoft.Extensions.Logging;
6-
using Polly;
72
using LinqToDB;
83
using LinqToDB.Async;
94
using LinqToDB.Data;
105
using LinqToDB.Mapping;
6+
using Microsoft.Extensions.Logging;
7+
using Polly;
8+
9+
namespace Linq2db.QuickStart;
1110

1211
internal static class Program
1312
{
@@ -24,9 +23,9 @@ public static async Task Main(string[] args)
2423
[Table("series")]
2524
public sealed class Series
2625
{
27-
[PrimaryKey, Column("series_id")] public ulong SeriesId { get; set; }
26+
[PrimaryKey, Column("series_id")] public ulong SeriesId { get; init; }
2827

29-
[Column("title"), NotNull] public string Title { get; set; } = null!;
28+
[Column("title"), NotNull] public string Title { get; init; } = null!;
3029

3130
[Column("series_info")] public string? SeriesInfo { get; set; }
3231

@@ -37,11 +36,11 @@ public sealed class Series
3736
[Table("seasons")]
3837
public sealed class Season
3938
{
40-
[PrimaryKey, Column("series_id")] public ulong SeriesId { get; set; }
39+
[PrimaryKey, Column("series_id")] public ulong SeriesId { get; init; }
4140

42-
[PrimaryKey, Column("season_id")] public ulong SeasonId { get; set; }
41+
[PrimaryKey, Column("season_id")] public ulong SeasonId { get; init; }
4342

44-
[Column("title"), NotNull] public string Title { get; set; } = null!;
43+
[Column("title"), NotNull] public string Title { get; init; } = null!;
4544

4645
[Column("first_aired"), DataType(DataType.Date)]
4746
public DateTime FirstAired { get; set; }
@@ -118,16 +117,9 @@ public static Settings Load()
118117

119118
#endregion
120119

121-
internal class AppContext
120+
internal class AppContext(ILogger<AppContext> logger)
122121
{
123-
private readonly ILogger<AppContext> _logger;
124-
private readonly Settings _settings;
125-
126-
public AppContext(ILogger<AppContext> logger)
127-
{
128-
_logger = logger;
129-
_settings = SettingsLoader.Load();
130-
}
122+
private readonly Settings _settings = SettingsLoader.Load();
131123

132124
DataOptions BuildOptions(string? overrideConnectionString = null)
133125
{
@@ -137,7 +129,7 @@ DataOptions BuildOptions(string? overrideConnectionString = null)
137129

138130
public async Task Run()
139131
{
140-
_logger.LogInformation("Start app example");
132+
logger.LogInformation("Start app example");
141133

142134
await InitTables();
143135
await LoadData();
@@ -149,7 +141,7 @@ public async Task Run()
149141
await TlsConnectionExample();
150142
await ConnectionWithLoggerFactory();
151143

152-
_logger.LogInformation("Finish app example");
144+
logger.LogInformation("Finish app example");
153145
}
154146

155147
private async Task InitTables()
@@ -162,7 +154,7 @@ private async Task InitTables()
162154
}
163155
catch
164156
{
165-
_logger.LogDebug("series exists");
157+
logger.LogDebug("series exists");
166158
}
167159

168160
try
@@ -171,7 +163,7 @@ private async Task InitTables()
171163
}
172164
catch
173165
{
174-
_logger.LogDebug("seasons exists");
166+
logger.LogDebug("seasons exists");
175167
}
176168

177169
try
@@ -180,10 +172,10 @@ private async Task InitTables()
180172
}
181173
catch
182174
{
183-
_logger.LogDebug("episodes exists");
175+
logger.LogDebug("episodes exists");
184176
}
185177

186-
_logger.LogInformation("Created tables");
178+
logger.LogInformation("Created tables");
187179
}
188180

189181
private async Task LoadData()
@@ -325,7 +317,7 @@ private async Task LoadData()
325317

326318
await db.BulkCopyAsync(eps);
327319

328-
_logger.LogInformation("Loaded data");
320+
logger.LogInformation("Loaded data");
329321
}
330322

331323
private async Task SelectWithParameters()
@@ -345,17 +337,17 @@ private async Task SelectWithParameters()
345337
.Select(e => new { e.SeriesId, e.SeasonId, e.EpisodeId, e.AirDate, e.Title })
346338
.ToListAsync();
347339

348-
_logger.LogInformation("Selected rows:");
340+
logger.LogInformation("Selected rows:");
349341
foreach (var r in rows)
350-
_logger.LogInformation(
342+
logger.LogInformation(
351343
"series_id: {series_id}, season_id: {season_id}, episode_id: {episode_id}, air_date: {air_date}, title: {title}",
352344
r.SeriesId, r.SeasonId, r.EpisodeId, r.AirDate, r.Title);
353345
}
354346

355347
private async Task RetryPolicy()
356348
{
357349
var policy = Policy
358-
.Handle<Exception>(_ => true)
350+
.Handle<Exception>()
359351
.WaitAndRetryAsync(10, _ => TimeSpan.FromSeconds(1));
360352

361353
await policy.ExecuteAsync(async () =>
@@ -377,15 +369,15 @@ await policy.ExecuteAsync(async () =>
377369
.ThenBy(x => x.SeasonId);
378370

379371
foreach (var x in stats)
380-
_logger.LogInformation("series_id: {series_id}, season_id: {season_id}, cnt: {cnt}",
372+
logger.LogInformation("series_id: {series_id}, season_id: {season_id}, cnt: {cnt}",
381373
x.SeriesId, x.SeasonId, x.Cnt);
382374
});
383375
}
384376

385377
private async Task InteractiveTransaction()
386378
{
387379
await using var db = new MyYdb(BuildOptions());
388-
using var tr = await db.BeginTransactionAsync();
380+
await using var tr = await db.BeginTransactionAsync();
389381

390382
await db.InsertAsync(new Episode
391383
{
@@ -404,32 +396,32 @@ await db.InsertAsync(new Episode
404396
});
405397

406398
await tr.CommitAsync();
407-
_logger.LogInformation("Commit transaction");
399+
logger.LogInformation("Commit transaction");
408400

409401
string title21 = await db.Episodes
410402
.Where(e => e.SeriesId == 2 && e.SeasonId == 5 && e.EpisodeId == 21)
411403
.Select(e => e.Title)
412404
.SingleAsync();
413-
_logger.LogInformation("New episode title: {title}", title21);
405+
logger.LogInformation("New episode title: {title}", title21);
414406

415407
string title22 = await db.Episodes
416408
.Where(e => e.SeriesId == 2 && e.SeasonId == 5 && e.EpisodeId == 22)
417409
.Select(e => e.Title)
418410
.SingleAsync();
419-
_logger.LogInformation("New episode title: {title}", title22);
411+
logger.LogInformation("New episode title: {title}", title22);
420412

421413
string title13 = await db.Episodes
422414
.Where(e => e.SeriesId == 2 && e.SeasonId == 5 && e.EpisodeId == 13)
423415
.Select(e => e.Title)
424416
.SingleAsync();
425-
_logger.LogInformation("Updated episode title: {title}", title13);
417+
logger.LogInformation("Updated episode title: {title}", title13);
426418
}
427419

428420
private async Task TlsConnectionExample()
429421
{
430422
if (!_settings.UseTls)
431423
{
432-
_logger.LogInformation("Tls example was ignored");
424+
logger.LogInformation("Tls example was ignored");
433425
return;
434426
}
435427

@@ -446,7 +438,7 @@ private async Task TlsConnectionExample()
446438
.ToListAsync();
447439

448440
foreach (var r in rows)
449-
_logger.LogInformation(
441+
logger.LogInformation(
450442
"season_title: {SeasonTitle}, series_title: {SeriesTitle}, series_id: {SeriesId}, season_id: {SeasonId}",
451443
r.SeasonTitle, r.SeriesTitle, r.SeriesId, r.SeasonId);
452444
}
@@ -459,20 +451,20 @@ private async Task ConnectionWithLoggerFactory()
459451
switch (ti.TraceInfoStep)
460452
{
461453
case TraceInfoStep.BeforeExecute:
462-
_logger.LogInformation("BeforeExecute: {sql}", ti.SqlText);
454+
logger.LogInformation("BeforeExecute: {sql}", ti.SqlText);
463455
break;
464456
case TraceInfoStep.AfterExecute:
465-
_logger.LogInformation("AfterExecute: {time} {records} recs", ti.ExecutionTime, ti.RecordsAffected);
457+
logger.LogInformation("AfterExecute: {time} {records} recs", ti.ExecutionTime, ti.RecordsAffected);
466458
break;
467459
case TraceInfoStep.Error:
468-
_logger.LogError(ti.Exception, "SQL error");
460+
logger.LogError(ti.Exception, "SQL error");
469461
break;
470462
}
471463
});
472464

473465
await using var db = new MyYdb(options);
474466

475-
_logger.LogInformation("Dropping tables of examples");
467+
logger.LogInformation("Dropping tables of examples");
476468
try
477469
{
478470
await db.DropTableAsync<Episode>();
@@ -500,6 +492,6 @@ private async Task ConnectionWithLoggerFactory()
500492
/* ignore */
501493
}
502494

503-
_logger.LogInformation("Dropped tables of examples");
495+
logger.LogInformation("Dropped tables of examples");
504496
}
505497
}

0 commit comments

Comments
 (0)