Skip to content

Commit

Permalink
add builders
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardoporro committed Dec 6, 2024
1 parent 677111d commit 5017590
Show file tree
Hide file tree
Showing 42 changed files with 259 additions and 185 deletions.
2 changes: 0 additions & 2 deletions sample/Detached.Samples.RestApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ public void ConfigureServices(IServiceCollection services)

services.AddScoped<InvoiceService>();
services.AddScoped<InvoiceStore>();

services.Configure<MapperOptions>(m => { m.Type<User>().Entity(true); });
}

public class User { }
Expand Down
1 change: 1 addition & 0 deletions sample/Detached.Samples.RestApi/Stores/InvoiceStore.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Detached.Mappers.EntityFramework;
using Detached.Mappers.EntityFramework.Extensions;
using Detached.Samples.RestApi.Models;
using Detached.Samples.RestApi.Models.Inputs;
using Detached.Samples.RestApi.Models.Outputs;
Expand Down
10 changes: 5 additions & 5 deletions src/Detached.Mappers.EntityFramework/EntityMapperFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Detached.Mappers.EntityFramework.TypeMappers;
using Detached.Mappers.Options;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Concurrent;
using System.Reflection;

Expand Down Expand Up @@ -50,16 +49,17 @@ public EntityMapper CreateMapper(DbContext dbContext, object profileKey)

static void ApplyOnMapperCreating(DbContext dbContext, EntityMapperOptions options)
{
MethodInfo configureMapperMethodInfo = dbContext.GetType().GetMethod("OnMapperCreating");
var builder = new EntityMapperOptionsBuilder(options);
var configureMapperMethodInfo = dbContext.GetType().GetMethod("OnMapperCreating");
if (configureMapperMethodInfo != null)
{
var parameters = configureMapperMethodInfo.GetParameters();
if (parameters.Length != 1 && parameters[0].ParameterType != typeof(EntityMapperOptions))
if (parameters.Length != 1 && parameters[0].ParameterType != typeof(EntityMapperOptionsBuilder))
{
throw new ArgumentException($"OnMapperCreating method must have a single argument of type {nameof(EntityMapperOptions)}");
throw new ArgumentException($"OnMapperCreating method must have a single argument of type {nameof(EntityMapperOptionsBuilder)}");
}

configureMapperMethodInfo.Invoke(dbContext, new[] { options });
configureMapperMethodInfo.Invoke(dbContext, new[] { builder });
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
using System;

/* Unmerged change from project 'Detached.Mappers.EntityFramework (net9.0)'
Before:
using System;
After:
using Detached;
using Detached.Mappers;
using Detached.Mappers.EntityFramework;
using Detached.Mappers.EntityFramework;
using Detached.Mappers.EntityFramework.Options;
using System;
*/
using System;
using System.Diagnostics.CodeAnalysis;

namespace Detached.Mappers.EntityFramework.Options
namespace Detached.Mappers.EntityFramework
{
public struct EntityMapperKey
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Reflection;
using System.Threading.Tasks;

namespace Detached.Mappers.EntityFramework
namespace Detached.Mappers.EntityFramework.Extensions
{
public static class MappingDbContextExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Reflection;
using System.Threading.Tasks;

namespace Detached.Mappers.EntityFramework
namespace Detached.Mappers.EntityFramework.Extensions
{
public static class ProfileMappingDbContextExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Detached.Mappers.Options;
using Detached.PatchTypes;
using System;
using System.Collections.Concurrent;
using System.Text.Json;

Expand All @@ -22,21 +21,5 @@ public EntityMapperOptions()
public ConcurrentDictionary<object, MapperOptions> Profiles { get; } = new();

public JsonSerializerOptions JsonOptions { get; set; }

public EntityMapperOptions AddProfile(object profileKey, Action<MapperOptions> configure)
{
var mapperOptions = Profiles.GetOrAdd(profileKey, key => new MapperOptions());

configure?.Invoke(mapperOptions);

return this;
}

public EntityMapperOptions Default(Action<MapperOptions> configure)
{
configure?.Invoke(this);

return this;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Detached.Mappers.Options;

namespace Detached.Mappers.EntityFramework.Options
{
public class EntityMapperOptionsBuilder : MapperOptionsBuilder
{
public EntityMapperOptionsBuilder(EntityMapperOptions options)
: base(options)
{
}

public EntityMapperOptionsBuilder()
: base(new EntityMapperOptions())
{
}

public new EntityMapperOptions Options => (EntityMapperOptions)base.Options;

public EntityMapperOptionsBuilder AddProfile(object profileKey, Action<MapperOptionsBuilder> configure)
{
var options = Options.Profiles.GetOrAdd(profileKey, key => new MapperOptions());

var builder = new MapperOptionsBuilder(options);

configure?.Invoke(builder);

return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Detached.Mappers.TypeMappers;
using Detached.Mappers.TypePairs;
using Detached.Mappers.Types;
using System;
using System.Linq.Expressions;

namespace Detached.Mappers.EntityFramework.TypeMappers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static IType Abstract(this IType type, bool value = true)

public static ClassTypeBuilder<TType> Abstract<TType>(this ClassTypeBuilder<TType> typeBuilder, bool value = true)
{
typeBuilder.Type.Abstract(value);
typeBuilder.ClassType.Abstract(value);

return typeBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void Entity(this IType type, bool value = true)

public static ClassTypeBuilder<TType> Entity<TType>(this ClassTypeBuilder<TType> type, bool value = true)
{
type.Type.Entity(value);
type.ClassType.Entity(value);

return type;
}
Expand Down
8 changes: 1 addition & 7 deletions src/Detached.Mappers/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Detached.Mappers.TypeMappers;
using Detached.Mappers.TypePairs;
using Detached.Mappers.Types;
using System;
using System.Collections.Concurrent;
using System.Linq.Expressions;

Expand All @@ -16,18 +15,13 @@ public class Mapper
readonly ConcurrentDictionary<TypeMapperKey, ITypeMapper> _typeMappers = new ConcurrentDictionary<TypeMapperKey, ITypeMapper>();
readonly ConcurrentDictionary<TypeBinderKey, Expression> _typeBindings = new ConcurrentDictionary<TypeBinderKey, Expression>();

public Mapper(MapperOptions options = null)
public Mapper(MapperOptions? options = null)
{
Options = options ?? new MapperOptions();
}

public MapperOptions Options { get; }

public void Reset()
{
_typeMappers.Clear();
}

public virtual object Map(object source, Type sourceClrType, object target, Type targetClrType, IMapContext context = default)
{
if (context == null)
Expand Down
12 changes: 1 addition & 11 deletions src/Detached.Mappers/Options/MapperOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@
using Detached.Mappers.TypePairs.Conventions;
using Detached.Mappers.Types;
using Detached.Mappers.Types.Class;
using Detached.Mappers.Types.Class.Builder;
using Detached.Mappers.Types.Conventions;
using Detached.Mappers.Types.Dictionary;
using Detached.Mappers.Types.Json;
using Detached.PatchTypes;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Reflection;

Expand Down Expand Up @@ -148,13 +145,6 @@ public MapperOptions()

public virtual List<ITypeBinder> TypeBinders { get; }

public virtual ClassTypeBuilder<TType> Type<TType>()
{
IType type = GetTypeConfiguration(typeof(TType));

return new ClassTypeBuilder<TType>((ClassType)type, this);
}

public virtual IType GetType(Type clrType)
{
return _allTypes.GetOrAdd(clrType, clrType =>
Expand All @@ -167,7 +157,7 @@ public virtual IType GetType(Type clrType)
});
}

IType GetTypeConfiguration(Type clrType)
public IType GetTypeConfiguration(Type clrType)
{
return _configuredTypes.GetOrAdd(clrType, clrType =>
{
Expand Down
28 changes: 28 additions & 0 deletions src/Detached.Mappers/Options/MapperOptionsBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Detached.Mappers.Types;
using Detached.Mappers.Types.Class;
using Detached.Mappers.Types.Class.Builder;

namespace Detached.Mappers.Options
{
public class MapperOptionsBuilder
{
public MapperOptionsBuilder(MapperOptions options)
{
Options = options;
}

public MapperOptionsBuilder()
{
Options = new MapperOptions();
}

public MapperOptions Options { get; }

public virtual ClassTypeBuilder<TType> Type<TType>()
{
IType type = Options.GetTypeConfiguration(typeof(TType));

return new ClassTypeBuilder<TType>((ClassType)type, Options);
}
}
}
6 changes: 3 additions & 3 deletions src/Detached.Mappers/TypePairs/Builder/TypePairBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ public class TypePairBuilder<TSource, TTarget>
{
public TypePairBuilder(MapperOptions mapperOptions, TypePair typePairOptions)
{
MapperOptions = mapperOptions;
Options = mapperOptions;
TypePairOptions = typePairOptions;
}

public MapperOptions MapperOptions { get; }
public MapperOptions Options { get; }

public TypePair TypePairOptions { get; }

Expand All @@ -25,7 +25,7 @@ public TypePairMemberBuilder<TSource, TTarget> Member<TMember>(Expression<Func<T
throw new ArgumentException($"Member {memberName} does not exist.");
}

return new TypePairMemberBuilder<TSource, TTarget>(MapperOptions, TypePairOptions, memberOptions);
return new TypePairMemberBuilder<TSource, TTarget>(Options, TypePairOptions, memberOptions);
}
}
}
Loading

0 comments on commit 5017590

Please sign in to comment.