Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonardo Porro committed Jan 26, 2024
1 parent 6ab8752 commit 5e31b10
Show file tree
Hide file tree
Showing 22 changed files with 294 additions and 89 deletions.
7 changes: 0 additions & 7 deletions Detached.Mappers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Detached.Mappers.HotChocola
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "contrib", "contrib", "{5409CE02-02AD-448B-85A5-EFEABA6C52C8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Detached.Mappers.Annotation.Tests", "test\Detached.Mappers.Annotation.Tests\Detached.Mappers.Annotation.Tests.csproj", "{713EB397-9271-45F6-811C-CD84CD6BB194}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -94,10 +92,6 @@ Global
{B6162ECF-00B0-4506-B3E8-2A4B46517ECE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B6162ECF-00B0-4506-B3E8-2A4B46517ECE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B6162ECF-00B0-4506-B3E8-2A4B46517ECE}.Release|Any CPU.Build.0 = Release|Any CPU
{713EB397-9271-45F6-811C-CD84CD6BB194}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{713EB397-9271-45F6-811C-CD84CD6BB194}.Debug|Any CPU.Build.0 = Debug|Any CPU
{713EB397-9271-45F6-811C-CD84CD6BB194}.Release|Any CPU.ActiveCfg = Release|Any CPU
{713EB397-9271-45F6-811C-CD84CD6BB194}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -116,7 +110,6 @@ Global
{42962E94-05D1-4D67-8BB2-572B04CBC3F7} = {C9521C36-FBA4-4B3E-AE14-A1735E9D1E71}
{B6162ECF-00B0-4506-B3E8-2A4B46517ECE} = {42962E94-05D1-4D67-8BB2-572B04CBC3F7}
{5409CE02-02AD-448B-85A5-EFEABA6C52C8} = {745CB1E1-F50B-4307-8BD6-1ADDC837102B}
{713EB397-9271-45F6-811C-CD84CD6BB194} = {C9521C36-FBA4-4B3E-AE14-A1735E9D1E71}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7FC47439-B36B-45BB-BEEC-A6D595358C63}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Detached.Mappers.EntityFramework.Configuration
{
public interface IEntityMapperConfiguration
{
void Apply(EntityMapperOptionsBuilder builder);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Detached.Mappers.Types;
using Detached.Mappers.Types.Class;
using Microsoft.EntityFrameworkCore.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Detached.Mappers.EntityFramework.Conventions
Expand Down Expand Up @@ -63,15 +65,16 @@ void SetDiscriminator(ClassType classType, IEntityType entityType)
IProperty discriminator = entityType.FindDiscriminatorProperty();
if (discriminator != null && (entityType.BaseType == null || entityType.IsAbstract()))
{
classType.SetDiscriminatorName(discriminator.Name);

var values = new Dictionary<object, Type>();
foreach (var inheritedType in entityType.Model.GetEntityTypes())
{
if (IsBaseType(inheritedType, entityType))
{
classType.GetDiscriminatorValues()[inheritedType.GetDiscriminatorValue()] = inheritedType.ClrType;
values[inheritedType.GetDiscriminatorValue()] = inheritedType.ClrType;
}
}

classType.SetDiscriminator(discriminator.Name, values);
}
}

Expand Down

This file was deleted.

17 changes: 11 additions & 6 deletions src/Detached.Mappers.EntityFramework/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using System;
using System.Collections.Generic;

namespace Detached.Mappers.EntityFramework
{
Expand All @@ -14,28 +15,32 @@ public static DbContextOptionsBuilder UseMapping(this DbContextOptionsBuilder db

configure?.Invoke(builder);

AddMappingExtension(dbContextBuilder, builder.Options);
UseMapping(dbContextBuilder, builder.Options);

return dbContextBuilder;
}

public static DbContextOptionsBuilder<TDbContext> UseMapping<TDbContext>(this DbContextOptionsBuilder<TDbContext> dbContextBuilder, Action<EntityMapperOptionsBuilder> configure = null)
where TDbContext : DbContext
public static DbContextOptionsBuilder UseMapping(this DbContextOptionsBuilder dbContextBuilder, IEnumerable<IEntityMapperConfiguration> configs)
{
var builder = new EntityMapperOptionsBuilder();

configure?.Invoke(builder);
foreach (var config in configs)
{
config.Apply(builder);
}

AddMappingExtension(dbContextBuilder, builder.Options);
UseMapping(dbContextBuilder, builder.Options);

return dbContextBuilder;
}

static void AddMappingExtension(DbContextOptionsBuilder dbContextBuilder, EntityMapperOptions options)
public static DbContextOptionsBuilder UseMapping(this DbContextOptionsBuilder dbContextBuilder, EntityMapperOptions options)
{
var builder = ((IDbContextOptionsBuilderInfrastructure)dbContextBuilder);

builder.AddOrUpdateExtension(new EntityMapperDbContextOptionsExtension(dbContextBuilder.Options.ContextType, options));

return dbContextBuilder;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ public static ClassTypeBuilder<TType> Abstract<TType>(this ClassTypeBuilder<TTyp

return typeBuilder;
}

public static bool IsAbstract(this IType type)
{
return type.Annotations.Abstract().Value();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,26 @@ public static bool IsInherited(this IType type)
return type.Annotations.DiscriminatorName().Value() != null;
}

public static string GetDiscriminatorName(this IType type)
public static IType SetDiscriminator(this IType type, string propertyName, Dictionary<object, Type> values)
{
return type.Annotations.DiscriminatorName().Value();
}
var propertyNameAnnotation = type.Annotations.DiscriminatorName();
var valuesAnnotation = type.Annotations.DiscriminatorValues();

public static IType SetDiscriminatorName(this IType type, string name)
{
type.Annotations.DiscriminatorName().Set(name);
propertyNameAnnotation.Set(propertyName);
valuesAnnotation.Set(values);

return type;
}

public static Dictionary<object, Type> GetDiscriminatorValues(this IType type)
public static bool GetDiscriminator(this IType type, out string propertyName, out Dictionary<object, Type> values)
{
var annotation = type.Annotations.DiscriminatorValues();
var propertyNameAnnotation = type.Annotations.DiscriminatorName();
var valuesAnnotation = type.Annotations.DiscriminatorValues();

if (!annotation.IsDefined())
{
annotation.Set(new());
}
propertyName = propertyNameAnnotation.Value();
values = valuesAnnotation.Value();

return annotation.Value();
return propertyNameAnnotation.IsDefined() && valuesAnnotation.IsDefined();
}
}
}
4 changes: 2 additions & 2 deletions src/Detached.Mappers/MapperOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ public virtual bool IsPrimitive(Type type)
public virtual bool ShouldMap(IType sourceType, IType targetType)
{
return sourceType != targetType
|| sourceType.Annotations.Abstract().Value()
|| targetType.Annotations.Abstract().Value()
|| sourceType.IsAbstract()
|| targetType.IsAbstract()
|| (targetType.IsComplex() || targetType.IsCollection() && GetType(targetType.ItemClrType).IsComplex());
}

Expand Down
19 changes: 8 additions & 11 deletions src/Detached.Mappers/TypeBinders/Binders/InheritedTypeBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ public bool CanBind(Mapper mapper, TypePair typePair)
{
return typePair.SourceType.IsComplex()
&& typePair.TargetType.IsComplex()
&& typePair.SourceType.GetDiscriminatorName() != null
&& typePair.TargetType.GetDiscriminatorName() != null;
&& typePair.SourceType.GetDiscriminator(out _, out _)
&& typePair.TargetType.GetDiscriminator(out _, out _);
}

public Expression Bind(Mapper mapper, TypePair typePair, Expression sourceExpr)
{
var options = mapper.Options;

var sourceName = typePair.SourceType.GetDiscriminatorName();
var targetName = typePair.TargetType.GetDiscriminatorName();
typePair.SourceType.GetDiscriminator(out var sourceName, out var sourceValues);
typePair.TargetType.GetDiscriminator(out var targetName, out var targetValues);

if (sourceName != targetName)
{
throw new MapperException($"Discriminator members '{typePair.SourceType}.{sourceName}' and '{typePair.TargetType}.{targetName}' doesn't match.");
throw new MapperException($"Discriminator members '{typePair.SourceType}.{sourceName}' and '{typePair.TargetType}.{targetName}' doesn't match.");
}

var member = typePair.GetMember(sourceName);
Expand All @@ -35,17 +35,14 @@ public Expression Bind(Mapper mapper, TypePair typePair, Expression sourceExpr)
throw new MapperException($"Discriminator member '{sourceName}' must be mapped for both {typePair.SourceType} and {typePair.TargetType}");
}

var sourceValues = typePair.SourceType.GetDiscriminatorValues();
var targetValues = typePair.TargetType.GetDiscriminatorValues();

var propertyExpr = member.SourceMember.BuildGetExpression(sourceExpr, null);

Expression resultExpr = Constant(null, typePair.TargetType.ClrType);

foreach (var entry in sourceValues)
{
var sourceValue = entry.Key;

if (!targetValues.TryGetValue(sourceValue, out Type targetClrType))
{
throw new MapperException($"Value '{sourceValue}' for discriminator '{sourceName}' doesn't have an concrete type for '{typePair.TargetType.ClrType}'");
Expand All @@ -57,7 +54,7 @@ public Expression Bind(Mapper mapper, TypePair typePair, Expression sourceExpr)
var binder = mapper.GetTypeBinder(concreteTypePair);

var conditionExpr = Equal(propertyExpr, Constant(sourceValue, sourceValue.GetType()));

var bindExpr = binder.Bind(mapper, concreteTypePair, Convert(sourceExpr, sourceType.ClrType));

resultExpr = Condition(conditionExpr, Convert(bindExpr, typePair.TargetType.ClrType), resultExpr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class EntityCollectionTypeMapperFactory : ITypeMapperFactory
public bool CanCreate(Mapper mapper, TypePair typePair)
{
if (typePair.SourceType.IsCollection()
&& !typePair.SourceType.Annotations.Abstract().Value()
&& !typePair.SourceType.IsAbstract()
&& typePair.TargetType.IsCollection()
&& !typePair.TargetType.Annotations.Abstract().Value())
&& !typePair.TargetType.IsAbstract())
{
IType sourceItemType = mapper.Options.GetType(typePair.TargetType.ItemClrType);
IType targetItemType = mapper.Options.GetType(typePair.TargetType.ItemClrType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ public class AbstractTypeMapperFactory : ITypeMapperFactory
{
public bool CanCreate(Mapper mapper, TypePair typePair)
{
return typePair.SourceType.Annotations.Abstract().Value() || typePair.TargetType.Annotations.Abstract().Value();
return typePair.SourceType.IsAbstract() || typePair.TargetType.IsAbstract();
}

public ITypeMapper Create(Mapper mapper, TypePair typePair)
{
Type mapperType = typeof(AbstractTypeMapper<,>).MakeGenericType(typePair.SourceType.ClrType, typePair.TargetType.ClrType);

Type concreteTargetType = typePair.TargetType.Annotations.Abstract().Value() && !typePair.TargetType.IsInherited() && typePair.TargetType.ClrType != typeof(object)
Type concreteTargetType = typePair.TargetType.IsAbstract() && !typePair.TargetType.IsInherited() && typePair.TargetType.ClrType != typeof(object)
? GetConcreteType(mapper.Options, typePair.TargetType.ClrType)
: typePair.TargetType.ClrType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public class InheritedTypeMapperFactory : ITypeMapperFactory
{
public bool CanCreate(Mapper mapper, TypePair typePair)
{
return ((typePair.SourceType.IsComplex() || typePair.SourceType.IsEntity()) && !typePair.SourceType.Annotations.Abstract().Value())
return ((typePair.SourceType.IsComplex() || typePair.SourceType.IsEntity()) && !typePair.SourceType.IsAbstract())
&& (typePair.TargetType.IsComplex() || typePair.TargetType.IsEntity())
&& typePair.TargetType.IsInherited();
}

public ITypeMapper Create(Mapper mapper, TypePair typePair)
{
string targetMemberName = typePair.TargetType.GetDiscriminatorName();
typePair.TargetType.GetDiscriminator(out var targetMemberName, out var targetValues);

if (!typePair.Members.TryGetValue(targetMemberName, out TypePairMember member) || member.IsIgnored() || member.SourceMember == null)
{
Expand All @@ -34,7 +34,7 @@ public ITypeMapper Create(Mapper mapper, TypePair typePair)
ITypeMember discriminatorMember = typePair.SourceType.GetMember(sourceMemberName);
if (discriminatorMember == null)
{
throw new MapperException($"Discriminator member {typePair.TargetType.GetDiscriminatorName()} does not exist in type {typePair.TargetType.ClrType}");
throw new MapperException($"Discriminator member {targetMemberName} does not exist in type {typePair.TargetType.ClrType}");
}

var getDiscriminator =
Expand All @@ -48,7 +48,7 @@ public ITypeMapper Create(Mapper mapper, TypePair typePair)
Type tableType = typeof(Dictionary<,>).MakeGenericType(discriminatorMember.ClrType, typeof(ITypeMapper));
IDictionary table = (IDictionary)Activator.CreateInstance(tableType);

foreach (var entry in typePair.TargetType.GetDiscriminatorValues())
foreach (var entry in targetValues)
{
IType sourceDiscriminatorType = typePair.SourceType;
IType targetDiscriminatorType = mapper.Options.GetType(entry.Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public ClassTypeBuilder<TType> IncludePrimitives()
public ClassTypeDiscriminatorBuilder<TType, TMember> Discriminator<TMember>(Expression<Func<TType, TMember>> selector)
{
ClassTypeMember memberOptions = GetMember(selector);
Type.SetDiscriminatorName(memberOptions.Name);
Type.Annotations.DiscriminatorName().Set(memberOptions.Name);

return new ClassTypeDiscriminatorBuilder<TType, TMember>(Type);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace Detached.Mappers.Types.Class.Builder
{
Expand All @@ -13,7 +14,16 @@ public ClassTypeDiscriminatorBuilder(ClassType typeOptions)

public ClassTypeDiscriminatorBuilder<TType, TMember> HasValue(TMember value, Type instantiationType)
{
TypeOptions.GetDiscriminatorValues()[value] = instantiationType;
var annotation = TypeOptions.Annotations.DiscriminatorValues();

if (annotation.IsDefined())
{
annotation.Value()[value] = instantiationType;
}
else
{
annotation.Set(new Dictionary<object, Type> { { value, instantiationType } });
}

return this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Detached.Mappers/Types/Class/ClassTypeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public virtual IType Create(MapperOptions options, Type clrType)
protected virtual void CreateConstructor(ClassType classType)
{
ConstructorInfo constructorInfo = Array.Find(classType.ClrType.GetConstructors(), c => c.GetParameters().Length == 0);
if (!classType.Annotations.Abstract().Value() && constructorInfo != null)
if (!classType.IsAbstract() && constructorInfo != null)
{
classType.Constructor = Lambda(New(constructorInfo));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Detached.Mappers/Types/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static bool IsNullable(this IType type)

public static bool IsConcrete(this IType type)
{
return !(type.Annotations.Abstract().Value() || type.IsInherited());
return !(type.IsAbstract() || type.IsInherited());
}
}
}

This file was deleted.

Loading

0 comments on commit 5e31b10

Please sign in to comment.