Skip to content

Commit

Permalink
Use persistence context for lookups where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
sven-n committed Jul 6, 2024
1 parent 170fa52 commit 96b1464
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 6 deletions.
28 changes: 28 additions & 0 deletions src/Persistence/EntityFramework/EntityFrameworkContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,34 @@ public async ValueTask<IEnumerable> GetAsync(Type type)
return await this.GetRepository(type).GetAllAsync().ConfigureAwait(false);
}

/// <inheritdoc/>
public bool IsSupporting(Type type)
{
var currentSearchType = type;
do
{
if (currentSearchType is null)
{
break;
}

if (this.Context.Model.FindLeastDerivedEntityTypes(currentSearchType).FirstOrDefault() is not null)
{
return true;
}

if (currentSearchType.Name != currentSearchType.BaseType?.Name)
{
break;
}

currentSearchType = currentSearchType.BaseType;
}
while (currentSearchType != typeof(object));

return false;
}

/// <inheritdoc/>
public void Dispose()
{
Expand Down
10 changes: 10 additions & 0 deletions src/Persistence/IContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,14 @@ ValueTask<IEnumerable<T>> GetAsync<T>()
/// <param name="type">The type.</param>
/// <returns>All objects of the specified type.</returns>
ValueTask<IEnumerable> GetAsync(Type type);

/// <summary>
/// Determines whether the specified type is supported by this instance.
/// This is usually the case for a type of the object tree of the owner.
/// </summary>
/// <param name="type">The type.</param>
/// <returns>
/// <c>true</c> if the specified type is supported; otherwise, <c>false</c>.
/// </returns>
bool IsSupporting(Type type);
}
6 changes: 6 additions & 0 deletions src/Persistence/InMemory/InMemoryContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,10 @@ public ValueTask<IEnumerable> GetAsync(Type type)
{
return this.Provider.GetRepository(type).GetAllAsync();
}

/// <inheritdoc/>
public bool IsSupporting(Type type)
{
return true;
}
}
4 changes: 3 additions & 1 deletion src/Web/AdminPanel/Components/Form/ItemTable.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ private void OnToggleCollapseClick()

private async Task OnAddClickAsync()
{
var modal = this._modal.Show<ModalObjectSelection<TItem>>($"Select {typeof(TItem).Name}");
var parameters = new ModalParameters();
parameters.Add(nameof(ModalCreateNew<TItem>.PersistenceContext), this.PersistenceContext);
var modal = this._modal.Show<ModalObjectSelection<TItem>>($"Select {typeof(TItem).Name}", parameters);
var result = await modal.Result.ConfigureAwait(false);
if (!result.Cancelled && result.Data is TItem item)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Web/AdminPanel/Components/Form/LookupField.razor
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<FieldLabel Text="@Label" ValueExpression="@this.ValueExpression" />
<BlazoredTypeahead TItem="TObject" TValue="TObject"
@bind-Value="@this.CurrentValue"
SearchMethod="@(text => this.EffectiveLookupController.GetSuggestionsAsync<TObject>(text, null))"
SearchMethod="@(text => this.EffectiveLookupController.GetSuggestionsAsync<TObject>(text, this.PersistenceContext))"
EnableDropDown="true"
MaximumSuggestions="20"
placeholder="Search ..."
Expand Down
6 changes: 4 additions & 2 deletions src/Web/AdminPanel/Components/Form/ModalObjectSelection.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
@inject IModalService _modalService;

<EditForm Model="@this">
<CascadingValue Value="@this.PersistenceContext">
<LookupField TObject="TItem" @bind-Value="@Item" Label="@typeof(TItem).Name"/>
</CascadingValue>

<button type="submit" @onclick="@SubmitAsync" class="btn-primary">OK</button>
<button @onclick="@CancelAsync" class="btn-secondary">Cancel</button>
<button type="submit" @onclick="@SubmitAsync" class="btn-primary">OK</button>
<button @onclick="@CancelAsync" class="btn-secondary">Cancel</button>
</EditForm>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace MUnique.OpenMU.Web.AdminPanel.Components.Form;
using Blazored.Modal;
using Blazored.Modal.Services;
using Microsoft.AspNetCore.Components;
using MUnique.OpenMU.Persistence;
using MUnique.OpenMU.Web.AdminPanel.Services;

/// <summary>
Expand All @@ -30,6 +31,12 @@ public partial class ModalObjectSelection<TItem>
[CascadingParameter]
public BlazoredModalInstance BlazoredModal { get; set; } = null!;

/// <summary>
/// Gets or sets the persistence context which should be used. It's required for lookups.
/// </summary>
[Parameter]
public IContext PersistenceContext { get; set; } = null!;

private Task SubmitAsync()
{
return this.BlazoredModal.CloseAsync(ModalResult.Ok(this.Item));
Expand Down
2 changes: 1 addition & 1 deletion src/Web/AdminPanel/Components/ItemEdit/ItemEdit.razor
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
<LookupField
Label="@socket.Caption"
@bind-Value="@socket.Option"
ExplicitLookupController="new EnumerableLookupController(this._viewModel.PossibleSocketOptions)"></LookupField>
ExplicitLookupController=@(new EnumerableLookupController(this._viewModel.PossibleSocketOptions))></LookupField>
@if (socket.Option is { } socketOption)
{
<NumberField
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public async Task<IEnumerable<T>> GetSuggestionsAsync<T>(string? text, IContext?

var owner = await this._gameConfigurationSource.GetOwnerAsync();
IEnumerable<T> values;
if (this._gameConfigurationSource.IsSupporting(typeof(T)))
if (this._gameConfigurationSource.IsSupporting(typeof(T))
&& persistenceContext?.IsSupporting(typeof(T)) is not true)
{
values = this._gameConfigurationSource.GetAll<T>();
}
Expand Down

0 comments on commit 96b1464

Please sign in to comment.