Skip to content

Commit e40e097

Browse files
committed
Micro optimization
1 parent 7f460a8 commit e40e097

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

src/EFCore.AuditBase/EFCore.AuditBase.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
<PackageReadmeFile>Readme.md</PackageReadmeFile>
99
<Authors>Pandatech</Authors>
1010
<Copyright>MIT</Copyright>
11-
<Version>3.0.7</Version>
11+
<Version>3.0.8</Version>
1212
<PackageId>Pandatech.EFCore.AuditBase</PackageId>
1313
<Title>Pandatech EFCore AuditBase</Title>
1414
<PackageTags>Pandatech, library, audit, optimistic lock, tracking, efcore, soft delete, versioning</PackageTags>
1515
<Description>Pandatech.EFCore.AuditBase provides a robust auditing solution for EF Core applications, ensuring traceability and integrity of entity modifications. It seamlessly integrates auditing capabilities into your EF Core entities, enforcing best practices for entity state changes, deletion handling, and versioning to support concurrency control. Ideal for applications requiring a reliable audit trail and compliance with data handling standards.</Description>
1616
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-efcore-audit-base</RepositoryUrl>
17-
<PackageReleaseNotes>Fix IgnoreInterceptor view as shadow prop</PackageReleaseNotes>
17+
<PackageReleaseNotes>Micro optimization</PackageReleaseNotes>
1818
</PropertyGroup>
1919

2020
<ItemGroup>

src/EFCore.AuditBase/Interceptors/AuditPropertyValidationInterceptor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ private static void ValidateAuditMethodUsage(DbContext context)
3535
.Where(e => e.State == EntityState.Modified)
3636
.ToList();
3737

38+
if (entries.Count is 0)
39+
{
40+
return;
41+
}
42+
3843
var ignoreInterceptor = entries.Any(x => x.Entity.IgnoreInterceptor);
3944

4045
if (ignoreInterceptor)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
public class InMemoryContext(DbContextOptions<InMemoryContext> options) : DbContext(options)
4+
{
5+
public DbSet<Product> Products => Set<Product>();
6+
}

test/EFCore.AuditBase.Demo/EFCore.AuditBase.Demo.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.3" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.3" />
1213
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.0" />
1314
</ItemGroup>
1415

test/EFCore.AuditBase.Demo/Program.cs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
using EFCore.AuditBase;
2+
using EFCore.AuditBase.Extensions;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.EntityFrameworkCore;
5+
16
var builder = WebApplication.CreateBuilder(args);
27

8+
builder.Services.AddDbContext<InMemoryContext>(options =>
9+
options.UseInMemoryDatabase("DemoDb")
10+
.UseAuditBaseValidatorInterceptor());
311

412
builder.Services.AddEndpointsApiExplorer();
513
builder.Services.AddSwaggerGen();
@@ -10,5 +18,47 @@
1018
app.UseSwagger();
1119
app.UseSwaggerUI();
1220

21+
app.MapGet("/products",
22+
async ([FromServices] InMemoryContext db) =>
23+
{
24+
var product = new Product
25+
{
26+
CreatedAt = DateTime.Now,
27+
CreatedByUserId = 23,
28+
Name = "asd",
29+
Price = 23
30+
};
31+
32+
db.Add(product);
33+
await db.SaveChangesAsync();
34+
35+
product.Name = "asd2";
36+
product.Price = 24;
37+
38+
product.MarkAsUpdated(24, DateTime.Now);
39+
await db.SaveChangesAsync();
40+
41+
var product2 = new Product
42+
{
43+
CreatedAt = product.CreatedAt,
44+
CreatedByUserId = product.CreatedByUserId,
45+
Name = product.Name,
46+
Price = product.Price,
47+
};
48+
product2.SyncAuditBase(product);
49+
50+
db.Add(product2);
51+
52+
await db.SaveChangesAsync();
53+
54+
return TypedResults.Ok(product);
55+
});
56+
57+
app.Run();
1358

14-
app.Run();
59+
public class Product : AuditEntityBase
60+
{
61+
public int Id { get; set; }
62+
public required string Name { get; set; }
63+
public decimal Price { get; set; }
64+
}

0 commit comments

Comments
 (0)