2024-08-04 18:44:33 -05:00
|
|
|
|
using LANCommander.Server.Data;
|
2024-11-25 22:29:56 -06:00
|
|
|
|
using LANCommander.Server.Data.Enums;
|
2024-08-04 18:44:33 -05:00
|
|
|
|
using LANCommander.Server.Data.Models;
|
2024-10-07 18:04:26 -05:00
|
|
|
|
using LANCommander.Server.Services.Models;
|
2023-08-25 19:25:28 -05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-10-07 18:04:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-01-09 01:05:40 -06:00
|
|
|
|
using System.Linq.Expressions;
|
2025-02-01 02:21:34 -06:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using AutoMapper.QueryableExtensions;
|
2025-02-09 18:53:40 -06:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2024-10-30 17:44:10 -05:00
|
|
|
|
using ZiggyCreatures.Caching.Fusion;
|
2023-01-09 01:05:40 -06:00
|
|
|
|
|
2024-08-04 18:44:33 -05:00
|
|
|
|
namespace LANCommander.Server.Services
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
public abstract class BaseDatabaseService<T>(
|
|
|
|
|
|
ILogger logger,
|
|
|
|
|
|
IFusionCache cache,
|
|
|
|
|
|
IMapper mapper,
|
2025-02-09 18:53:40 -06:00
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
2025-02-01 02:21:34 -06:00
|
|
|
|
IDbContextFactory<DatabaseContext> dbContextFactory) : BaseService(logger), IBaseDatabaseService<T> where T : class, IBaseModel
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
protected readonly List<Func<IQueryable<T>, IQueryable<T>>> _modifiers = new();
|
2023-01-09 01:05:40 -06:00
|
|
|
|
|
2025-01-25 12:46:24 -06:00
|
|
|
|
public IBaseDatabaseService<T> AsNoTracking()
|
|
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
return Query((queryable) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
return queryable.AsNoTracking();
|
|
|
|
|
|
});
|
2025-02-16 13:24:23 -06:00
|
|
|
|
}
|
2025-01-25 12:46:24 -06:00
|
|
|
|
|
2025-02-16 13:24:23 -06:00
|
|
|
|
public IBaseDatabaseService<T> AsSplitQuery()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Query((queryable) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
return queryable.AsSplitQuery();
|
|
|
|
|
|
});
|
2025-01-25 12:46:24 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-25 22:29:56 -06:00
|
|
|
|
public IBaseDatabaseService<T> Query(Func<IQueryable<T>, IQueryable<T>> modifier)
|
2024-11-11 18:18:59 -06:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
_modifiers.Add(modifier);
|
2024-11-25 22:29:56 -06:00
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IBaseDatabaseService<T> Include(params Expression<Func<T, object>>[] expressions)
|
|
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
return Query((queryable) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var expression in expressions)
|
|
|
|
|
|
{
|
|
|
|
|
|
queryable = queryable.Include(expression);
|
|
|
|
|
|
}
|
2024-11-11 18:18:59 -06:00
|
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
|
return queryable;
|
|
|
|
|
|
});
|
2024-11-11 18:18:59 -06:00
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
|
public IBaseDatabaseService<T> SortBy(Expression<Func<T, object>> expression, SortDirection direction = SortDirection.Ascending)
|
2024-11-25 22:29:56 -06:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
switch (direction)
|
|
|
|
|
|
{
|
|
|
|
|
|
case SortDirection.Descending:
|
|
|
|
|
|
return Query((queryable) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
return queryable.OrderByDescending(expression);
|
|
|
|
|
|
});
|
|
|
|
|
|
case SortDirection.Ascending:
|
|
|
|
|
|
default:
|
|
|
|
|
|
return Query((queryable) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
return queryable.OrderBy(expression);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2024-11-25 22:29:56 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public virtual async Task<ICollection<T>> GetAsync()
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
2025-01-24 21:31:42 -06:00
|
|
|
|
return await GetAsync(x => true);
|
2023-01-09 01:05:40 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public virtual async Task<ICollection<U>> GetAsync<U>()
|
2024-11-11 18:18:59 -06:00
|
|
|
|
{
|
2024-11-12 18:32:46 -06:00
|
|
|
|
return await GetAsync<U>(x => true);
|
2024-11-11 18:18:59 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public virtual async Task<T> GetAsync(Guid id)
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
return await FirstOrDefaultAsync(x => x.Id == id);
|
2023-01-09 01:05:40 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public virtual async Task<U> GetAsync<U>(Guid id)
|
2024-11-11 18:18:59 -06:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
return await FirstOrDefaultAsync<U>(x => x.Id == id);
|
2024-11-11 18:18:59 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public virtual async Task<ICollection<T>> GetAsync(Expression<Func<T, bool>> predicate)
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
using var context = await dbContextFactory.CreateDbContextAsync();
|
2025-02-02 18:50:22 -06:00
|
|
|
|
|
|
|
|
|
|
var queryable = context.Set<T>().AsQueryable();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var modifier in _modifiers)
|
|
|
|
|
|
queryable = modifier.Invoke(queryable);
|
2025-02-01 02:21:34 -06:00
|
|
|
|
|
2025-02-02 18:50:22 -06:00
|
|
|
|
return await queryable.Where(predicate).ToListAsync();
|
2025-02-01 02:21:34 -06:00
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
Reset();
|
|
|
|
|
|
}
|
2024-10-21 20:06:52 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public virtual async Task<ICollection<U>> GetAsync<U>(Expression<Func<T, bool>> predicate)
|
2024-11-11 18:18:59 -06:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
using var context = await dbContextFactory.CreateDbContextAsync();
|
2025-02-02 18:50:22 -06:00
|
|
|
|
|
|
|
|
|
|
var queryable = context.Set<T>().AsQueryable();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var modifier in _modifiers)
|
|
|
|
|
|
queryable = modifier.Invoke(queryable);
|
|
|
|
|
|
|
|
|
|
|
|
return await queryable.Where(predicate).ProjectTo<U>(mapper.ConfigurationProvider).ToListAsync();
|
2025-02-01 02:21:34 -06:00
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
Reset();
|
|
|
|
|
|
}
|
2024-11-11 18:18:59 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public virtual async Task<T> FirstAsync(Expression<Func<T, bool>> predicate)
|
2024-10-21 20:06:52 -05:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
using var context = await dbContextFactory.CreateDbContextAsync();
|
2025-02-02 18:50:22 -06:00
|
|
|
|
|
|
|
|
|
|
var queryable = context.Set<T>().AsQueryable();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var modifier in _modifiers)
|
|
|
|
|
|
queryable = modifier.Invoke(queryable);
|
|
|
|
|
|
|
|
|
|
|
|
return await queryable.FirstAsync(predicate);
|
2025-02-01 02:21:34 -06:00
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
Reset();
|
|
|
|
|
|
}
|
2024-11-11 18:18:59 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
|
public virtual async Task<U> FirstAsync<U>(Expression<Func<T, bool>> predicate)
|
2024-10-21 20:06:52 -05:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
using var context = await dbContextFactory.CreateDbContextAsync();
|
2025-02-02 18:50:22 -06:00
|
|
|
|
|
|
|
|
|
|
var queryable = context.Set<T>().AsQueryable();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var modifier in _modifiers)
|
|
|
|
|
|
queryable = modifier.Invoke(queryable);
|
2024-11-11 18:18:59 -06:00
|
|
|
|
|
2025-02-02 18:50:22 -06:00
|
|
|
|
return await queryable.Where(predicate).ProjectTo<U>(mapper.ConfigurationProvider).FirstAsync();
|
2025-02-01 02:21:34 -06:00
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
Reset();
|
|
|
|
|
|
}
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public virtual async Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate)
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
using var context = await dbContextFactory.CreateDbContextAsync();
|
2025-02-02 18:50:22 -06:00
|
|
|
|
|
|
|
|
|
|
var queryable = context.Set<T>().AsQueryable();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var modifier in _modifiers)
|
|
|
|
|
|
queryable = modifier.Invoke(queryable);
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
|
2025-02-02 18:50:22 -06:00
|
|
|
|
return await queryable.FirstOrDefaultAsync(predicate);
|
2025-02-01 02:21:34 -06:00
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
Reset();
|
|
|
|
|
|
}
|
2024-11-11 18:18:59 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
|
public virtual async Task<U> FirstOrDefaultAsync<U>(Expression<Func<T, bool>> predicate)
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
using var context = await dbContextFactory.CreateDbContextAsync();
|
2025-02-02 18:50:22 -06:00
|
|
|
|
|
|
|
|
|
|
var queryable = context.Set<T>().AsQueryable();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var modifier in _modifiers)
|
|
|
|
|
|
queryable = modifier.Invoke(queryable);
|
2024-11-11 18:18:59 -06:00
|
|
|
|
|
2025-02-02 18:50:22 -06:00
|
|
|
|
return await queryable.Where(predicate).ProjectTo<U>(mapper.ConfigurationProvider).FirstOrDefaultAsync();
|
2025-02-01 02:21:34 -06:00
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
Reset();
|
|
|
|
|
|
}
|
2023-01-09 01:05:40 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public virtual async Task<bool> ExistsAsync(Guid id)
|
2023-01-09 01:35:08 -06:00
|
|
|
|
{
|
2025-01-16 03:08:43 -06:00
|
|
|
|
return (await FirstOrDefaultAsync(x => x.Id == id)) != null;
|
2023-01-09 01:35:08 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-22 02:04:04 -06:00
|
|
|
|
public virtual async Task<bool> ExistsAsync(Expression<Func<T, bool>> predicate)
|
|
|
|
|
|
{
|
2025-02-21 22:48:29 -06:00
|
|
|
|
return (await GetAsync(predicate)).Any();
|
2024-11-22 02:04:04 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-23 08:49:43 -06:00
|
|
|
|
public abstract Task<T> AddAsync(T entity);
|
|
|
|
|
|
|
|
|
|
|
|
protected async Task<T> AddAsync(T addedEntity, Action<UpdateEntityContext<T>> additionalMapping = null)
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
using var context = await dbContextFactory.CreateDbContextAsync();
|
2025-02-23 08:49:43 -06:00
|
|
|
|
|
2025-02-09 19:58:25 -06:00
|
|
|
|
var currentUser = await GetCurrentUserAsync(context);
|
2025-02-01 02:21:34 -06:00
|
|
|
|
|
2025-02-23 08:49:43 -06:00
|
|
|
|
var newEntity = Activator.CreateInstance<T>();
|
|
|
|
|
|
|
|
|
|
|
|
context.Entry(newEntity).CurrentValues.SetValues(addedEntity);
|
2025-02-09 18:53:40 -06:00
|
|
|
|
|
2025-02-23 08:49:43 -06:00
|
|
|
|
newEntity = (await context.AddAsync(newEntity)).Entity;
|
|
|
|
|
|
newEntity.CreatedOn = DateTime.UtcNow;
|
2025-02-26 00:59:50 -06:00
|
|
|
|
newEntity.CreatedById = currentUser?.Id;
|
2025-02-01 02:21:34 -06:00
|
|
|
|
|
|
|
|
|
|
await context.SaveChangesAsync();
|
2025-02-23 08:49:43 -06:00
|
|
|
|
|
|
|
|
|
|
addedEntity.Id = newEntity.Id;
|
|
|
|
|
|
|
|
|
|
|
|
return await UpdateAsync(addedEntity, additionalMapping);
|
2025-02-01 02:21:34 -06:00
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
Reset();
|
|
|
|
|
|
}
|
2023-01-09 01:05:40 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-10 17:17:54 -06:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Adds an entity to the database if it does exist as dictated by the predicate
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="predicate">Qualifier expressoin</param>
|
|
|
|
|
|
/// <param name="entity">Entity to add</param>
|
|
|
|
|
|
/// <returns>Newly created or existing entity</returns>
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public virtual async Task<ExistingEntityResult<T>> AddMissingAsync(Expression<Func<T, bool>> predicate, T entity)
|
2023-01-10 17:17:54 -06:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
var existing = await FirstOrDefaultAsync(predicate);
|
2023-01-10 17:17:54 -06:00
|
|
|
|
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
if (existing == null)
|
|
|
|
|
|
{
|
2025-02-09 01:37:31 -06:00
|
|
|
|
await cache.ExpireAsync($"{typeof(T).FullName}");
|
2023-01-10 17:17:54 -06:00
|
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
|
entity = await AddAsync(entity);
|
2023-01-10 17:17:54 -06:00
|
|
|
|
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
return new ExistingEntityResult<T>
|
|
|
|
|
|
{
|
|
|
|
|
|
Value = entity,
|
|
|
|
|
|
Existing = false
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return new ExistingEntityResult<T>
|
2023-01-10 17:17:54 -06:00
|
|
|
|
{
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
Value = existing,
|
|
|
|
|
|
Existing = true
|
|
|
|
|
|
};
|
2023-01-10 17:17:54 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-02-02 14:58:07 -06:00
|
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
|
public abstract Task<T> UpdateAsync(T entity);
|
2023-01-09 01:05:40 -06:00
|
|
|
|
|
2025-02-03 01:42:14 -06:00
|
|
|
|
protected async Task<T> UpdateAsync(T updatedEntity, Action<UpdateEntityContext<T>> additionalMapping = null)
|
2025-02-02 14:58:07 -06:00
|
|
|
|
{
|
|
|
|
|
|
using var context = await dbContextFactory.CreateDbContextAsync();
|
2025-02-09 19:58:25 -06:00
|
|
|
|
|
|
|
|
|
|
if (updatedEntity.CreatedById != null && updatedEntity.CreatedBy == null)
|
|
|
|
|
|
updatedEntity.CreatedById = null;
|
2025-02-02 14:58:07 -06:00
|
|
|
|
|
2025-02-04 20:58:08 -06:00
|
|
|
|
var existingEntity = await context.Set<T>().FirstOrDefaultAsync(e => e.Id == updatedEntity.Id);
|
2025-02-02 14:58:07 -06:00
|
|
|
|
|
2025-02-03 01:42:14 -06:00
|
|
|
|
context.Entry(existingEntity).CurrentValues.SetValues(updatedEntity);
|
2025-02-02 14:58:07 -06:00
|
|
|
|
|
|
|
|
|
|
if (additionalMapping != null)
|
|
|
|
|
|
{
|
2025-02-03 01:42:14 -06:00
|
|
|
|
var updateContext = new UpdateEntityContext<T>(context, existingEntity, updatedEntity);
|
2025-02-02 14:58:07 -06:00
|
|
|
|
|
|
|
|
|
|
additionalMapping?.Invoke(updateContext);
|
|
|
|
|
|
}
|
2025-02-09 18:53:40 -06:00
|
|
|
|
|
2025-02-09 19:58:25 -06:00
|
|
|
|
var currentUser = await GetCurrentUserAsync(context);
|
|
|
|
|
|
|
2025-02-09 18:53:40 -06:00
|
|
|
|
existingEntity.UpdatedOn = DateTime.UtcNow;
|
2025-02-09 19:58:25 -06:00
|
|
|
|
existingEntity.UpdatedById = currentUser.Id;
|
2025-02-02 14:58:07 -06:00
|
|
|
|
|
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
|
|
2025-02-03 01:42:14 -06:00
|
|
|
|
return updatedEntity;
|
2025-02-02 14:58:07 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public virtual async Task DeleteAsync(T entity)
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-02-09 01:37:31 -06:00
|
|
|
|
await cache.ExpireAsync($"{typeof(T).FullName}");
|
2025-02-01 02:21:34 -06:00
|
|
|
|
|
|
|
|
|
|
using var context = await dbContextFactory.CreateDbContextAsync();
|
|
|
|
|
|
|
|
|
|
|
|
context.Set<T>().Remove(entity);
|
|
|
|
|
|
|
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
Reset();
|
|
|
|
|
|
}
|
2023-01-09 01:05:40 -06:00
|
|
|
|
}
|
2025-02-09 18:53:40 -06:00
|
|
|
|
|
|
|
|
|
|
private async Task<User?> GetCurrentUserAsync(DatabaseContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
var httpContext = httpContextAccessor?.HttpContext;
|
|
|
|
|
|
if (httpContext != null && httpContext.User != null && httpContext.User.Identity != null && httpContext.User.Identity.IsAuthenticated)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await GetUserAsync(httpContext.User.Identity?.Name, context);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static async Task<User?> GetUserAsync(string? username, DatabaseContext context) =>
|
2025-02-09 19:58:25 -06:00
|
|
|
|
await context.Users.AsNoTracking().FirstOrDefaultAsync(u => u.UserName == username);
|
2024-11-25 22:29:56 -06:00
|
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
|
protected void Reset()
|
2024-11-25 22:29:56 -06:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
_modifiers.Clear();
|
2024-11-25 22:29:56 -06:00
|
|
|
|
}
|
2023-01-09 01:05:40 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|