-
-
Save dcomartin/1c9d5488315c64be39c462d09a9d5d34 to your computer and use it in GitHub Desktop.
This file contains 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 System.Threading.Tasks; | |
using Microsoft.EntityFrameworkCore; | |
namespace Shipping | |
{ | |
public class ShippingDbContext : DbContext | |
{ | |
public DbSet<ShippingLabel> ShippingLabels { get; set; } | |
public DbSet<IdempotentConsumer> IdempotentConsumers { get; set; } | |
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |
{ | |
optionsBuilder.UseSqlServer("Server=localhost\\SQLExpress;Database=Demo;Trusted_Connection=Yes;"); | |
base.OnConfiguring(optionsBuilder); | |
} | |
protected override void OnModelCreating(ModelBuilder modelBuilder) | |
{ | |
modelBuilder.Entity<ShippingLabel>() | |
.ToTable("ShippingLabels") | |
.HasKey(x => x.OrderId); | |
modelBuilder.Entity<IdempotentConsumer>() | |
.ToTable("IdempotentConsumers") | |
.HasKey(x => new {x.MessageId, x.Consumer}); | |
base.OnModelCreating(modelBuilder); | |
} | |
public async Task IdempotentConsumer(long messageId, string consumer) | |
{ | |
await IdempotentConsumers.AddAsync(new IdempotentConsumer | |
{ | |
MessageId = messageId, | |
Consumer = consumer | |
}); | |
await SaveChangesAsync(); | |
} | |
public async Task<bool> HasBeenProcessed(long messageId, string consumer) | |
{ | |
return await IdempotentConsumers.AnyAsync(x => x.MessageId == messageId && x.Consumer == consumer); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment