Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Developed by Softeq Development Corporation
// http://www.softeq.com

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;

namespace Softeq.NetKit.Integrations.EventLog
{
public class HoopIntegrationEventLogContext : IntegrationEventLogContextBase<HoopIntegrationEventLogContext>
{
public HoopIntegrationEventLogContext(
DbContextOptions<HoopIntegrationEventLogContext> dbContextOptions,
IOptions<IntegrationEventLogContextOptions> integrationEventLogContextOptions)
: base(dbContextOptions, integrationEventLogContextOptions)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Developed by Softeq Development Corporation
// http://www.softeq.com

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Options;

namespace Softeq.NetKit.Integrations.EventLog
{
/// <summary>
/// This factory is needed to create db migrations in class library via Package Manager Console
/// https://docs.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli#from-a-design-time-factory
/// To use this factory
/// 1. Set current project as startup project
/// 2. Edit project file: change the project TargetFramework to, for example, <TargetFramework>netcoreapp3.1</TargetFramework>
/// 2.1. Ensure project successfully builds. If not - change target framework to for failed projects to fix
/// 3. Open Package Manager Console. Set current project as the default project
/// 4. Create migration by running command: add-migration migration_name -context IntegrationEventLogContext
/// </summary>
public class HoopIntegrationEventLogContextFactory : IDesignTimeDbContextFactory<HoopIntegrationEventLogContext>
{
public HoopIntegrationEventLogContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<HoopIntegrationEventLogContext>();
var integrationEventLogContextOptions = Options.Create(
new IntegrationEventLogContextOptions
{
Schema = "dbo",
TableName = "HoopIntegrationEventLogs"
});
optionsBuilder.UseSqlServer("data source=.\\SQLEXPRESS");
return new HoopIntegrationEventLogContext(optionsBuilder.Options, integrationEventLogContextOptions);
}
}
}
20 changes: 6 additions & 14 deletions Softeq.NetKit.Integrations.EventLog/IntegrationEventLogContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,17 @@
// http://www.softeq.com

using Microsoft.EntityFrameworkCore;
using Softeq.NetKit.Integrations.EventLog.Extensions;
using Softeq.NetKit.Integrations.EventLog.Mappings;
using Microsoft.Extensions.Options;

namespace Softeq.NetKit.Integrations.EventLog
{
public class IntegrationEventLogContext : DbContext
public class IntegrationEventLogContext : IntegrationEventLogContextBase<IntegrationEventLogContext>
{
public IntegrationEventLogContext(DbContextOptions<IntegrationEventLogContext> options) : base(options)
public IntegrationEventLogContext(
DbContextOptions<IntegrationEventLogContext> options,
IOptions<IntegrationEventLogContextOptions> integrationEventLogContextOptions)
: base(options, integrationEventLogContextOptions)
{
}

public DbSet<IntegrationEventLog> IntegrationEventLogs { get; set; }

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);

builder.HasDefaultSchema("dbo");
builder.AddEntityConfigurationsFromAssembly<IEntityMappingConfiguration>(GetType().Assembly);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Developed by Softeq Development Corporation
// http://www.softeq.com

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Softeq.NetKit.Integrations.EventLog.Extensions;
using Softeq.NetKit.Integrations.EventLog.Mappings;

namespace Softeq.NetKit.Integrations.EventLog
{
public abstract class IntegrationEventLogContextBase<TContext> : DbContext
where TContext : DbContext
{
private readonly IOptions<IntegrationEventLogContextOptions> _integrationEventLogOptions;

protected IntegrationEventLogContextBase(
DbContextOptions<TContext> options,
IOptions<IntegrationEventLogContextOptions> integrationEventLogOptions)
: base(options)
{
_integrationEventLogOptions = integrationEventLogOptions;
}
Comment on lines +4 to +22

public DbSet<IntegrationEventLog> IntegrationEventLogs { get; set; }

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);

builder.HasDefaultSchema(_integrationEventLogOptions.Value.Schema);
builder.AddEntityConfigurationsFromAssembly<IEntityMappingConfiguration>(GetType().Assembly);
builder.Entity<IntegrationEventLog>().ToTable(_integrationEventLogOptions.Value.TableName, _integrationEventLogOptions.Value.Schema);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Options;

namespace Softeq.NetKit.Integrations.EventLog
{
Expand All @@ -21,8 +22,14 @@ public class IntegrationEventLogContextFactory : IDesignTimeDbContextFactory<Int
public IntegrationEventLogContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<IntegrationEventLogContext>();
var integrationEventLogContextOptions = Options.Create(
new IntegrationEventLogContextOptions
{
Schema = "dbo",
TableName = "IntegrationEventLogs"
});
optionsBuilder.UseSqlServer("data source=.\\SQLEXPRESS");
return new IntegrationEventLogContext(optionsBuilder.Options);
return new IntegrationEventLogContext(optionsBuilder.Options, integrationEventLogContextOptions);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Developed by Softeq Development Corporation
// http://www.softeq.com

namespace Softeq.NetKit.Integrations.EventLog
{
public class IntegrationEventLogContextOptions
{
public string Schema { get; set; }

public string TableName { get; set; }
}
Comment on lines +6 to +11
}
65 changes: 33 additions & 32 deletions Softeq.NetKit.Integrations.EventLog/IntegrationEventLogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

using EnsureThat;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Softeq.NetKit.Components.EventBus.Events;
using Softeq.NetKit.Integrations.EventLog.Abstract;
using Softeq.NetKit.Integrations.EventLog.Dtos;
using Softeq.NetKit.Integrations.EventLog.Exceptions;
using System;
using System.Collections.Generic;
Expand All @@ -15,29 +17,34 @@
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Softeq.NetKit.Integrations.EventLog.Dtos;
using SortOrder = Softeq.NetKit.Integrations.EventLog.Dtos.SortOrder;

namespace Softeq.NetKit.Integrations.EventLog
{
public class IntegrationEventLogService : IIntegrationEventLogService
public class IntegrationEventLogService<TContext> : IIntegrationEventLogService
where TContext : DbContext
{
Comment on lines +24 to 26
private readonly Func<IntegrationEventLogContext> _dbContextFactory;

protected IntegrationEventLogContext DbContext => _dbContextFactory.Invoke();
private readonly TContext _dbContext;
private readonly IOptions<IntegrationEventLogContextOptions> _integrationEventLogContextOptions;

public IntegrationEventLogService(Func<IntegrationEventLogContext> dbContextFactory)
public IntegrationEventLogService(
TContext dbContext,
IOptions<IntegrationEventLogContextOptions> options)
{
_dbContextFactory = Ensure.Any.IsNotNull(dbContextFactory, nameof(dbContextFactory));
_dbContext = Ensure.Any.IsNotNull(dbContext, nameof(dbContext));
_integrationEventLogContextOptions = Ensure.Any.IsNotNull(options, nameof(options));
}

private DbSet<IntegrationEventLog> IntegrationEventLogs => _dbContext.Set<IntegrationEventLog>();

private string FullTableName => $"{_integrationEventLogContextOptions.Value.Schema}.{_integrationEventLogContextOptions.Value.TableName}";
Comment on lines +38 to +40

/// <inheritdoc />
public async Task<IntegrationEventLog> GetAsync(Guid eventId)
{
Ensure.Guid.IsNotEmpty(eventId, nameof(eventId));

var eventLog = await DbContext
.IntegrationEventLogs
var eventLog = await IntegrationEventLogs
.FirstOrDefaultAsync(log => log.EventId == eventId);
if (eventLog == null)
{
Expand All @@ -51,7 +58,7 @@ public Task<List<IntegrationEventLog>> GetAsync(Expression<Func<IntegrationEvent
{
Ensure.Any.IsNotNull(condition, nameof(condition));

return DbContext.IntegrationEventLogs.Where(condition).ToListAsync();
return IntegrationEventLogs.Where(condition).ToListAsync();
}

/// <inheritdoc />
Expand All @@ -75,7 +82,7 @@ public Task<List<IntegrationEventContentDto>> GetEventContentListAsync(
// Raw SQL is used instead of EF Core LINQ to avoid deserialization issues for old events
var sql = $@"
SELECT [EventId], [EventTypeName], [Content], [Created]
FROM [IntegrationEventLogs]
FROM {FullTableName}
WHERE [EventState] IN ({eventStateInClause}) AND [Created] <= @createdUntil
ORDER BY [Created] {orderClause}
OFFSET @skipCount ROWS
Expand Down Expand Up @@ -121,7 +128,7 @@ public Task<bool> AnyAsync(Expression<Func<IntegrationEventLog, bool>> condition
{
Ensure.Any.IsNotNull(condition, nameof(condition));

return DbContext.IntegrationEventLogs.AnyAsync(condition);
return IntegrationEventLogs.AnyAsync(condition);
}

/// <inheritdoc />
Expand All @@ -130,8 +137,8 @@ public async Task<IntegrationEventLog> CreateAsync(IntegrationEvent @event)
Ensure.Any.IsNotNull(@event, nameof(@event));

var eventLog = new IntegrationEventLog(@event);
DbContext.IntegrationEventLogs.Add(eventLog);
await DbContext.SaveChangesAsync();
IntegrationEventLogs.Add(eventLog);
await _dbContext.SaveChangesAsync();

return eventLog;
}
Expand All @@ -142,8 +149,7 @@ public async Task<IntegrationEventLog> MarkAsPublishedAsync(Guid eventId, string
Ensure.Guid.IsNotEmpty(eventId, nameof(eventId));
Ensure.String.IsNotNullOrEmpty(publisherId, nameof(publisherId));

var eventLog = await DbContext
.IntegrationEventLogs
var eventLog = await IntegrationEventLogs
.FirstOrDefaultAsync(log => log.EventId == eventId);
if (eventLog == null)
{
Expand All @@ -160,8 +166,7 @@ public async Task<IList<IntegrationEventLog>> MarkAsPublishedAsync(IList<Guid> e
Ensure.Collection.HasItems(eventIds, nameof(eventIds));
Ensure.String.IsNotNullOrEmpty(publisherId, nameof(publisherId));

var eventLogs = await DbContext
.IntegrationEventLogs
var eventLogs = await IntegrationEventLogs
.Where(log => eventIds.Contains(log.EventId))
.ToListAsync();
foreach (var eventLog in eventLogs)
Expand All @@ -177,8 +182,7 @@ public async Task<IntegrationEventLog> MarkAsPublishAcknowledgmentTimeoutAsync(G
{
Ensure.Guid.IsNotEmpty(eventId, nameof(eventId));

var eventLog = await DbContext
.IntegrationEventLogs
var eventLog = await IntegrationEventLogs
.FirstOrDefaultAsync(log => log.EventId == eventId);
if (eventLog == null)
{
Expand All @@ -194,8 +198,7 @@ public async Task<IList<IntegrationEventLog>> MarkAsPublishAcknowledgmentTimeout
{
Ensure.Collection.HasItems(eventIds, nameof(eventIds));

var eventLogs = await DbContext
.IntegrationEventLogs
var eventLogs = await IntegrationEventLogs
.Where(log => eventIds.Contains(log.EventId))
.ToListAsync();
foreach (var eventLog in eventLogs)
Expand All @@ -211,8 +214,7 @@ public async Task<IntegrationEventLog> MarkAsCompletedAsync(Guid eventId)
{
Ensure.Guid.IsNotEmpty(eventId, nameof(eventId));

var eventLog = await DbContext
.IntegrationEventLogs
var eventLog = await IntegrationEventLogs
.FirstOrDefaultAsync(log => log.EventId == eventId);
if (eventLog == null)
{
Expand All @@ -228,8 +230,7 @@ public async Task<IList<IntegrationEventLog>> MarkAsCompletedAsync(IList<Guid> e
{
Ensure.Collection.HasItems(eventIds, nameof(eventIds));

var eventLogs = await DbContext
.IntegrationEventLogs
var eventLogs = await IntegrationEventLogs
.Where(log => eventIds.Contains(log.EventId))
.ToListAsync();
foreach (var eventLog in eventLogs)
Expand All @@ -250,7 +251,7 @@ public async Task DeleteAsync(IReadOnlyCollection<Guid> eventIds)

// Raw SQL is used instead of EF Core LINQ to avoid deserialization issues for old events
var sql = $@"
DELETE FROM [IntegrationEventLogs]
DELETE FROM {FullTableName}
WHERE [EventId] IN ({eventIdInClause})";

var parameters = eventIds
Expand All @@ -270,22 +271,22 @@ await ExecuteWithConnectionAsync(sql, parameters, async command =>

private async Task UpdateAsync(IntegrationEventLog eventLog)
{
DbContext.IntegrationEventLogs.Update(eventLog);
await DbContext.SaveChangesAsync();
IntegrationEventLogs.Update(eventLog);
await _dbContext.SaveChangesAsync();
}

private async Task UpdateAsync(IList<IntegrationEventLog> eventLogs)
{
DbContext.IntegrationEventLogs.UpdateRange(eventLogs);
await DbContext.SaveChangesAsync();
IntegrationEventLogs.UpdateRange(eventLogs);
await _dbContext.SaveChangesAsync();
}

private async Task<TResult> ExecuteWithConnectionAsync<TResult>(
string sql,
List<DbParameter> parameters,
Func<DbCommand, Task<TResult>> executor)
{
var connection = DbContext.Database.GetDbConnection();
var connection = _dbContext.Database.GetDbConnection();
var connectionInitiallyOpen = connection.State == ConnectionState.Open;

try
Expand Down
Loading