Skip to content

Commit

Permalink
Merge pull request #74 from Etherna/improve/MODM-172-support-structs
Browse files Browse the repository at this point in the history
Add support to structs on model maps
  • Loading branch information
tmm360 authored Aug 24, 2024
2 parents 9b352de + 7ac6fc0 commit a810382
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
namespace Etherna.MongODM.Core.Serialization.Mapping
{
internal sealed class CustomSerializerMap<TModel> : MapBase, ICustomSerializerMapBuilder<TModel>
where TModel : class
{
// Constructor.
public CustomSerializerMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
namespace Etherna.MongODM.Core.Serialization.Mapping
{
public interface ICustomSerializerMapBuilder<TModel>
where TModel : class
{
}
}
6 changes: 2 additions & 4 deletions src/MongODM.Core/Serialization/Mapping/IMapRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public interface IMapRegistry : IDbContextInitializable, IFreezableConfig
/// <param name="customSerializer">Custom serializer</param>
/// <returns>The new schema</returns>
ICustomSerializerMapBuilder<TModel> AddCustomSerializerMap<TModel>(
IBsonSerializer<TModel> customSerializer)
where TModel : class;
IBsonSerializer<TModel> customSerializer);

/// <summary>
/// Register a new schema based on model map
Expand All @@ -56,8 +55,7 @@ ICustomSerializerMapBuilder<TModel> AddCustomSerializerMap<TModel>(
IModelMapBuilder<TModel> AddModelMap<TModel>(
string activeModelMapSchemaId,
Action<BsonClassMap<TModel>>? activeModelMapSchemaInitializer = null,
IBsonSerializer<TModel>? customSerializer = null)
where TModel : class;
IBsonSerializer<TModel>? customSerializer = null);

/// <summary>
/// Get active class map from schemas, or create a default classMap for model type
Expand Down
5 changes: 2 additions & 3 deletions src/MongODM.Core/Serialization/Mapping/MapRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void Initialize(IDbContext dbContext, ILogger logger)

// Methods.
public ICustomSerializerMapBuilder<TModel> AddCustomSerializerMap<TModel>(
IBsonSerializer<TModel> customSerializer) where TModel : class =>
IBsonSerializer<TModel> customSerializer) =>
ExecuteConfigAction(() =>
{
ArgumentNullException.ThrowIfNull(customSerializer, nameof(customSerializer));
Expand All @@ -74,8 +74,7 @@ public ICustomSerializerMapBuilder<TModel> AddCustomSerializerMap<TModel>(
public IModelMapBuilder<TModel> AddModelMap<TModel>(
string activeModelMapSchemaId,
Action<BsonClassMap<TModel>>? activeModelMapSchemaInitializer = null,
IBsonSerializer<TModel>? customSerializer = null)
where TModel : class =>
IBsonSerializer<TModel>? customSerializer = null) =>
ExecuteConfigAction(() =>
{
// Register and add schema configuration.
Expand Down
6 changes: 3 additions & 3 deletions src/MongODM.Core/Serialization/Mapping/ModelMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ protected ModelMap(
{
DbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));

// Verify if have to use proxy model.
if (modelType != typeof(object) &&
// Verify if uses proxy model.
if (modelType.IsClass &&
modelType != typeof(object) &&
!modelType.IsAbstract &&
!dbContext.ProxyGenerator.IsProxyType(modelType))
{
Expand Down Expand Up @@ -202,7 +203,6 @@ private MemberMap BuildMemberMap(

[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
internal sealed class ModelMap<TModel> : ModelMap, IModelMapBuilder<TModel>
where TModel : class
{
// Constructor.
public ModelMap(IDbContext dbContext)
Expand Down
10 changes: 4 additions & 6 deletions src/MongODM.Core/Serialization/Serializers/ModelMapSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ namespace Etherna.MongODM.Core.Serialization.Serializers
{
public class ModelMapSerializer<TModel> :
SerializerBase<TModel>,
IBsonSerializer<TModel>,
IBsonDocumentSerializer,
IBsonIdProvider,
IModelMapsHandlingSerializer
where TModel : class
{
// Fields.
private IDiscriminatorConvention _discriminatorConvention = default!;
Expand Down Expand Up @@ -72,7 +70,7 @@ public override TModel Deserialize(BsonDeserializationContext context, BsonDeser
if (context.Reader.CurrentBsonType == BsonType.Null)
{
context.Reader.ReadNull();
return null!;
return default!;
}

// Find pre-deserialization informations.
Expand Down Expand Up @@ -139,15 +137,15 @@ public override TModel Deserialize(BsonDeserializationContext context, BsonDeser
* In this case, don't add any not-proxy models in cache.
*/
if (!dbContext.SerializerModifierAccessor.IsNoCacheEnabled &&
GetDocumentId(model, out var id, out _, out _) && id != null &&
dbContext.ProxyGenerator.IsProxyType(model.GetType()))
dbContext.ProxyGenerator.IsProxyType(model!.GetType()) &&
GetDocumentId(model, out var id, out _, out _) && id != null)
{
if (dbContext.DbCache.LoadedModels.ContainsKey(id))
{
var fullModel = model;
model = (TModel)dbContext.DbCache.LoadedModels[id];

if (((IReferenceable)model).IsSummary)
if (((IReferenceable)model!).IsSummary)
((IReferenceable)model).MergeFullModel(fullModel);
}
else
Expand Down

0 comments on commit a810382

Please sign in to comment.