1
+ using System ;
1
2
using System . Collections . Generic ;
2
3
using System . Linq ;
3
4
using Newtonsoft . Json ;
@@ -15,9 +16,19 @@ public Relations(Dictionary<RelationName, Children> container)
15
16
: base ( container . Select ( kv => kv ) . ToDictionary ( kv => kv . Key , kv => kv . Value ) )
16
17
{ }
17
18
18
- public void Add ( RelationName type , Children children ) => BackingDictionary . Add ( type , children ) ;
19
- public void Add ( RelationName type , RelationName child , params RelationName [ ] moreChildren ) =>
19
+ public void Add ( RelationName type , Children children )
20
+ {
21
+ if ( BackingDictionary . ContainsKey ( type ) )
22
+ throw new ArgumentException ( $ "{ type } is already mapped as parent, you have to map all it's children as a single entry") ;
23
+ BackingDictionary . Add ( type , children ) ;
24
+ }
25
+
26
+ public void Add ( RelationName type , RelationName child , params RelationName [ ] moreChildren )
27
+ {
28
+ if ( BackingDictionary . ContainsKey ( type ) )
29
+ throw new ArgumentException ( $ "{ type } is already mapped as parent, you have to map all it's children as a single entry") ;
20
30
BackingDictionary . Add ( type , new Children ( child , moreChildren ) ) ;
31
+ }
21
32
}
22
33
23
34
public class RelationsDescriptor : IsADictionaryDescriptorBase < RelationsDescriptor , IRelations , RelationName , Children >
@@ -27,8 +38,20 @@ internal RelationsDescriptor(IRelations relations) : base(relations) { }
27
38
28
39
public RelationsDescriptor Join ( RelationName parent , RelationName child , params RelationName [ ] moreChildren ) =>
29
40
Assign ( parent , new Children ( child , moreChildren ) ) ;
30
- public RelationsDescriptor Join < TParent > ( RelationName child , params RelationName [ ] moreChildren ) =>
31
- Assign ( typeof ( TParent ) , new Children ( child , moreChildren ) ) ;
32
- public RelationsDescriptor Join < TParent , TChild > ( ) => Assign ( typeof ( TParent ) , typeof ( TChild ) ) ;
41
+
42
+ public RelationsDescriptor Join < TParent > ( RelationName child , params RelationName [ ] moreChildren )
43
+ {
44
+ if ( this . PromisedValue . ContainsKey ( typeof ( TParent ) ) ) throw new ArgumentException ( Message ( typeof ( TParent ) ) ) ;
45
+ return Assign ( typeof ( TParent ) , new Children ( child , moreChildren ) ) ;
46
+ }
47
+
48
+ public RelationsDescriptor Join < TParent , TChild > ( )
49
+ {
50
+ if ( this . PromisedValue . ContainsKey ( typeof ( TParent ) ) ) throw new ArgumentException ( Message ( typeof ( TParent ) ) ) ;
51
+ return Assign ( typeof ( TParent ) , typeof ( TChild ) ) ;
52
+ }
53
+
54
+ private static string Message ( Type t ) =>
55
+ $ "{ t . Name } is already mapped. Use Join<TParent>(typeof(ChildA), typeof(ChildB), ..) to add multiple children in one go";
33
56
}
34
57
}
0 commit comments