Skip to content

Commit f922b45

Browse files
committed
Add Inherits Lasyload
1 parent 155a723 commit f922b45

File tree

5 files changed

+105
-3
lines changed

5 files changed

+105
-3
lines changed

src/Mapster/Models/TypeTuple.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,50 @@ public TypeTuple(Type source, Type destination)
4040
Destination = destination;
4141
}
4242
}
43+
44+
public class InheritsTypeTuple : IEquatable<InheritsTypeTuple>
45+
{
46+
public bool Equals(InheritsTypeTuple other)
47+
{
48+
return Source == other.Source && Destination == other.Destination;
49+
}
50+
51+
public override bool Equals(object obj)
52+
{
53+
if (!(obj is InheritsTypeTuple))
54+
return false;
55+
return Equals((InheritsTypeTuple)obj);
56+
}
57+
58+
public override int GetHashCode()
59+
{
60+
return (Source.GetHashCode() << 16) ^ (Destination.GetHashCode() & 65535);
61+
}
62+
63+
public static bool operator ==(InheritsTypeTuple left, InheritsTypeTuple right)
64+
{
65+
return left.Equals(right);
66+
}
67+
68+
public static bool operator !=(InheritsTypeTuple left, InheritsTypeTuple right)
69+
{
70+
return !left.Equals(right);
71+
}
72+
73+
public Type Source { get; }
74+
public Type Destination { get; }
75+
public bool IsLoading { get; private set; }
76+
77+
public void IsUploaded()
78+
{
79+
IsLoading = true;
80+
}
81+
82+
public InheritsTypeTuple(Type source, Type destination)
83+
{
84+
Source = source;
85+
Destination = destination;
86+
IsLoading = false;
87+
}
88+
}
4389
}

src/Mapster/TypeAdapter.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
using System;
1+
using Mapster.Models;
2+
using System;
23
using System.Collections.Generic;
4+
using System.Data;
35
using System.Linq;
46
using System.Reflection;
5-
using Mapster.Models;
67

78
namespace Mapster
89
{
@@ -38,6 +39,14 @@ public static TDestination Adapt<TDestination>(this object? source)
3839
/// <returns>Adapted destination type.</returns>
3940
public static TDestination Adapt<TDestination>(this object? source, TypeAdapterConfig config)
4041
{
42+
if (config.RuleMap.TryGetValue(new TypeTuple(source.GetType(), typeof(TDestination)), out var rule))
43+
{
44+
if (rule.Settings.InheritsTypeTuples.Count > 0)
45+
{
46+
LoadInheritedRules(config, rule, rule.Settings.InheritsTypeTuples);
47+
}
48+
}
49+
4150
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
4251
if (source == null)
4352
return default!;
@@ -46,6 +55,22 @@ public static TDestination Adapt<TDestination>(this object? source, TypeAdapterC
4655
return fn(source);
4756
}
4857

58+
private static void LoadInheritedRules(TypeAdapterConfig config, TypeAdapterRule rule, IEnumerable<InheritsTypeTuple> inheritedTypes)
59+
{
60+
foreach (var typeTuple in inheritedTypes.Where(t => !t.IsLoading))
61+
{
62+
if (config.RuleMap.TryGetValue(new TypeTuple(typeTuple.Source, typeTuple.Destination), out var parentRule))
63+
{
64+
rule.LoadLasyInherits(parentRule);
65+
typeTuple.IsUploaded();
66+
}
67+
if (parentRule != null && parentRule.Settings.InheritsTypeTuples.Any())
68+
{
69+
LoadInheritedRules(config, rule, parentRule.Settings.InheritsTypeTuples);
70+
}
71+
}
72+
}
73+
4974
/// <summary>
5075
/// Adapt the source object to the destination type.
5176
/// </summary>
@@ -254,7 +279,7 @@ public static TDestination ValidateAndAdapt<TSource, TDestination>(this TSource
254279
}
255280
return source.Adapt<TDestination>(config);
256281
}
257-
}
282+
}
258283

259284
[System.Diagnostics.CodeAnalysis.SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "<Pending>")]
260285
public static class TypeAdapter<TSource, TDestination>

src/Mapster/TypeAdapterRule.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@ public class TypeAdapterRule
66
{
77
public Func<PreCompileArgument, int?> Priority { get; set; }
88
public TypeAdapterSettings Settings { get; set; }
9+
10+
public void LoadLasyInherits(TypeAdapterRule rule)
11+
{
12+
this.Settings.Apply(rule.Settings);
13+
}
914
}
1015
}

src/Mapster/TypeAdapterSetter.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,26 @@ public TypeAdapterSetter<TSource, TDestination> Inherits<TBaseSource, TBaseDesti
828828
return this;
829829
}
830830

831+
public TypeAdapterSetter<TSource, TDestination> InheritsLazy<TBaseSource, TBaseDestination>()
832+
{
833+
this.CheckCompiled();
834+
835+
Type baseSourceType = typeof(TBaseSource);
836+
Type baseDestinationType = typeof(TBaseDestination);
837+
838+
if (!baseSourceType.GetTypeInfo().IsAssignableFrom(typeof(TSource).GetTypeInfo()))
839+
throw new InvalidCastException("In order to use inherits, TSource must be inherited from TBaseSource.");
840+
841+
if (!baseDestinationType.GetTypeInfo().IsAssignableFrom(typeof(TDestination).GetTypeInfo()))
842+
throw new InvalidCastException("In order to use inherits, TDestination must be inherited from TBaseDestination.");
843+
844+
Settings.InheritsTypeTuples.Add(new InheritsTypeTuple(baseSourceType, baseDestinationType));
845+
846+
847+
return this;
848+
}
849+
850+
831851
public TypeAdapterSetter<TSource, TDestination> Fork(Action<TypeAdapterConfig> action)
832852
{
833853
this.CheckCompiled();

src/Mapster/TypeAdapterSettings.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ public List<InvokerModel> Resolvers
117117
{
118118
get => Get(nameof(Resolvers), () => new List<InvokerModel>());
119119
}
120+
121+
public HashSet<InheritsTypeTuple> InheritsTypeTuples
122+
{
123+
get => Get(nameof(InheritsTypeTuples), () => new HashSet<InheritsTypeTuple>());
124+
}
125+
120126
public List<object> ExtraSources
121127
{
122128
get => Get(nameof(ExtraSources), () => new List<object>());

0 commit comments

Comments
 (0)