Skip to content
Closed
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
9 changes: 5 additions & 4 deletions src/SIL.Harmony/Adapters/CustomAdapterProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public CustomAdapterProvider(ObjectTypeListBuilder objectTypeListBuilder)
_objectTypeListBuilder = objectTypeListBuilder;
JsonTypes.AddDerivedType(typeof(IObjectBase), typeof(TCustomAdapter), TCustomAdapter.TypeName);
}

public CustomAdapterProvider<TCommonInterface, TCustomAdapter> AddWithCustomPolymorphicMapping<T>(string typeName,
Action<EntityTypeBuilder<T>>? configureEntry = null
) where T : class, TCommonInterface
Expand All @@ -28,7 +28,8 @@ public CustomAdapterProvider<TCommonInterface, TCustomAdapter> AddWithCustomPoly
}

public CustomAdapterProvider<TCommonInterface, TCustomAdapter> Add<T>(
Action<EntityTypeBuilder<T>>? configureEntry = null
Action<EntityTypeBuilder<T>>? configureEntry = null,
Action<JsonTypeInfo>? jsonTypeModifier = null
) where T : class, TCommonInterface
{
_objectTypeListBuilder.CheckFrozen();
Expand All @@ -39,7 +40,7 @@ public CustomAdapterProvider<TCommonInterface, TCustomAdapter> Add<T>(
var entity = builder.Entity<T>();
configureEntry?.Invoke(entity);
return entity;
})
}, jsonTypeModifier)
);
return this;
}
Expand Down Expand Up @@ -69,4 +70,4 @@ public interface ICustomAdapter<TSelf, TCommonInterface> : IObjectBase, IPolyTyp
public static abstract TSelf Create(TCommonInterface obj);
static string IPolyType.TypeName => TSelf.AdapterTypeName;
public static abstract string AdapterTypeName { get; }
}
}
5 changes: 3 additions & 2 deletions src/SIL.Harmony/Adapters/IObjectAdapterProvider.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System.Text.Json.Serialization.Metadata;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace SIL.Harmony.Adapters;

internal record AdapterRegistration(Type ObjectDbType, Func<ModelBuilder, EntityTypeBuilder> EntityBuilder);
internal record AdapterRegistration(Type ObjectDbType, Func<ModelBuilder, EntityTypeBuilder> EntityBuilder, Action<JsonTypeInfo>? JsonTypeModifier = null);

internal interface IObjectAdapterProvider
{
IEnumerable<AdapterRegistration> GetRegistrations();
IObjectBase Adapt(object obj);
bool CanAdapt(object obj);
}
}
13 changes: 12 additions & 1 deletion src/SIL.Harmony/CrdtConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ private void JsonTypeModifier(JsonTypeInfo typeInfo)
typeInfo.PolymorphismOptions!.DerivedTypes.Add(type);
}
}

var registration = ObjectTypeListBuilder.RegistrationsByType.GetValueOrDefault(typeInfo.Type);
if (registration?.JsonTypeModifier is not null && registration.ObjectDbType == typeInfo.Type)
{
registration.JsonTypeModifier(typeInfo);
}
}

public bool RemoteResourcesEnabled { get; private set; }
Expand Down Expand Up @@ -131,6 +137,7 @@ public void Freeze()
{
if (_frozen) return;
_frozen = true;
_frozenRegistrationsByType = RegistrationsByType;
foreach (var registration in AdapterProviders.SelectMany(a => a.GetRegistrations()))
{
ModelConfigurations.Add((builder, config) =>
Expand All @@ -156,6 +163,10 @@ internal void CheckFrozen()

internal List<IObjectAdapterProvider> AdapterProviders { get; } = [];

private IReadOnlyDictionary<Type, AdapterRegistration>? _frozenRegistrationsByType;
internal IReadOnlyDictionary<Type, AdapterRegistration> RegistrationsByType
=> _frozenRegistrationsByType ?? AdapterProviders.SelectMany(p => p.GetRegistrations()).ToDictionary(r => r.ObjectDbType);

public DefaultAdapterProvider DefaultAdapter()
{
CheckFrozen();
Expand All @@ -164,7 +175,7 @@ public DefaultAdapterProvider DefaultAdapter()
AdapterProviders.Add(adapter);
return adapter;
}

/// <summary>
/// add a custom adapter for a common interface
/// this is required as CRDT objects must express their references and have an Id property
Expand Down
Loading