This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestMethod] | |
public void TestWithNullLogger() | |
{ | |
var svc = new CalculationService(new NullLogger<CalculationService>()); | |
var result = svc.AddTwoPositiveNumbers(1, 2); | |
Assert.AreEqual(3, result); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestMethod] | |
public void TestWithConsoleLogger() | |
{ | |
using var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()); | |
var logger = loggerFactory.CreateLogger<CalculationService>(); | |
var svc = new CalculationService(logger); | |
var result = svc.AddTwoPositiveNumbers(1, 2); | |
Assert.AreEqual(3, result); | |
// If you are using .NET Core 2.1. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestMethod] | |
public void TestWithDependencyInjectionLogger() | |
{ | |
var services = new ServiceCollection() | |
.AddLogging(config => config.AddConsole()) // can add any logger(s) | |
.BuildServiceProvider(); | |
using (var loggerFactory = services.GetRequiredService<ILoggerFactory>()) | |
{ | |
var svc = new CalculationService(loggerFactory.CreateLogger<CalculationService>()); | |
var result = svc.AddTwoPositiveNumbers(1, 2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestMethod] | |
public void TestWithMockLogger() | |
{ | |
var loggerMock = new Mock<ILogger<CalculationService>>(); | |
var svc = new CalculationService(loggerMock.Object); | |
var result = svc.AddTwoPositiveNumbers(1, 2); | |
Assert.AreEqual(3, result); | |
loggerMock.Verify( | |
m => m.Log( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BankAccount | |
{ | |
public int Id { get; set; } | |
public decimal Balance { get; set; } | |
public void Credit(decimal amount) | |
{ | |
Console.WriteLine($"Balance before credit:{Balance,5}"); | |
Console.WriteLine($"Amount to add :{amount,5}"); | |
Balance += amount; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var threads = new Thread[2]; | |
threads[0] = new Thread(async () => | |
{ | |
using (var dbContext = new MyDbContext()) | |
{ | |
var account = await dbContext.BankAccounts.FindAsync(1); | |
account.Credit(100); | |
await dbContext.SaveChangesAsync(); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class BankAccountEntityTypeConfigurationSqlite : IEntityTypeConfiguration<BankAccount> | |
{ | |
public void Configure(EntityTypeBuilder<BankAccount> builder) | |
{ | |
builder.ToTable("BankAccounts"); | |
builder.HasKey(x => x.Id); | |
builder.Property(x => x.Id).HasColumnName("Id").ValueGeneratedOnAdd(); | |
builder.Property(x => x.Balance).HasColumnName("Balance").HasConversion<double>() | |
.IsConcurrencyToken(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
info: Microsoft.EntityFrameworkCore.Database.Command[20101] | |
Executed DbCommand (1ms) [Parameters=[@p1='?', @p0='?', @p2='?'], CommandType='Text', CommandTimeout='30'] | |
UPDATE "BankAccounts" SET "Balance" = @p0 | |
WHERE "Id" = @p1 AND "Balance" = @p2; | |
SELECT changes(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (var dbContext = new MyDbContext()) | |
{ | |
var account = await dbContext.BankAccounts.FindAsync(1); | |
account.Credit(100); | |
try | |
{ | |
await dbContext.SaveChangesAsync(); // Attempt to save changes to the database | |
} | |
catch (DbUpdateConcurrencyException e) | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class BankAccountEntityTypeConfigurationSqlite : IEntityTypeConfiguration<BankAccount> | |
{ | |
public void Configure(EntityTypeBuilder<BankAccount> builder) | |
{ | |
builder.ToTable("BankAccounts"); | |
builder.HasKey(x => x.Id); | |
builder.Property(x => x.Id).HasColumnName("Id").ValueGeneratedOnAdd(); | |
builder.Property(x => x.Balance).HasColumnName("Balance").HasConversion<double>(); | |
builder.Property(x => x.Timestamp).HasColumnName("Timestamp") | |
.HasColumnType("BLOB") |