1+ using System ;
12using System . Collections . Generic ;
23using System . Linq ;
34using Newtonsoft . Json ;
@@ -15,9 +16,19 @@ public Relations(Dictionary<RelationName, Children> container)
1516 : base ( container . Select ( kv => kv ) . ToDictionary ( kv => kv . Key , kv => kv . Value ) )
1617 { }
1718
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") ;
2030 BackingDictionary . Add ( type , new Children ( child , moreChildren ) ) ;
31+ }
2132 }
2233
2334 public class RelationsDescriptor : IsADictionaryDescriptorBase < RelationsDescriptor , IRelations , RelationName , Children >
@@ -27,8 +38,20 @@ internal RelationsDescriptor(IRelations relations) : base(relations) { }
2738
2839 public RelationsDescriptor Join ( RelationName parent , RelationName child , params RelationName [ ] moreChildren ) =>
2940 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";
3356 }
3457}
0 commit comments