Skip to content

Instantly share code, notes, and snippets.

View changhuixu's full-sized avatar
💭
Everyone has a happy ending. If you're not happy, it's not the end.

Changhui Xu changhuixu

💭
Everyone has a happy ending. If you're not happy, it's not the end.
View GitHub Profile
[TestMethod]
public void TestWithNullLogger()
{
var svc = new CalculationService(new NullLogger<CalculationService>());
var result = svc.AddTwoPositiveNumbers(1, 2);
Assert.AreEqual(3, result);
}
[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.
[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);
[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(
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;
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();
}
});
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();
}
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();
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)
{
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")