@@ -5,28 +5,29 @@ namespace Ninject.Web.AspNetCore
55{
66 public class BindingIndex
77 {
8- private readonly IDictionary < Type , Item > _bindingIndexMap = new Dictionary < Type , Item > ( ) ;
8+ private readonly IDictionary < ServiceTypeKey , Item > _bindingIndexMap = new Dictionary < ServiceTypeKey , Item > ( ) ;
9+ public const string DefaultIndexKey = "NonKeyed" ;
910
1011 public int Count { get ; private set ; }
1112
1213 public BindingIndex ( )
1314 {
1415 }
1516
16- public Item Next ( Type serviceType )
17+ public Item Next ( Type serviceType , object indexKey )
1718 {
18-
19- _bindingIndexMap . TryGetValue ( serviceType , out var previous ) ;
19+ var serviceTypeKey = new ServiceTypeKey ( serviceType , indexKey ) ;
20+ _bindingIndexMap . TryGetValue ( serviceTypeKey , out var previous ) ;
2021
21- var next = new Item ( this , serviceType , Count ++ , previous ? . TypeIndex + 1 ?? 0 ) ;
22- _bindingIndexMap [ serviceType ] = next ;
22+ var next = new Item ( this , serviceType , indexKey , Count ++ , previous ? . TypeIndex + 1 ?? 0 ) ;
23+ _bindingIndexMap [ serviceTypeKey ] = next ;
2324
2425 return next ;
2526 }
2627
27- private bool IsLatest ( Type serviceType , Item item )
28+ private bool IsLatest ( Type serviceType , object indexKey , Item item )
2829 {
29- return _bindingIndexMap [ serviceType ] == item ;
30+ return _bindingIndexMap [ new ServiceTypeKey ( serviceType , indexKey ) ] == item ;
3031 }
3132
3233 public class Item
@@ -36,16 +37,55 @@ public class Item
3637
3738 public int TotalIndex { get ; }
3839 public int TypeIndex { get ; }
40+ public object IndexKey { get ; }
3941
40- public bool IsLatest => _root . IsLatest ( _serviceType , this ) ;
42+ public bool IsLatest => _root . IsLatest ( _serviceType , IndexKey , this ) ;
4143 public int Precedence => _root . Count - TotalIndex ;
4244
43- public Item ( BindingIndex root , Type serviceType , int totalIndex , int typeIndex )
45+ public Item ( BindingIndex root , Type serviceType , object indexKey , int totalIndex , int typeIndex )
4446 {
4547 _root = root ;
4648 _serviceType = serviceType ;
4749 TotalIndex = totalIndex ;
4850 TypeIndex = typeIndex ;
51+ IndexKey = indexKey ;
52+ }
53+ }
54+
55+ /// <summary>
56+ /// We have to to separate the precedence by servicekey.
57+ /// This ensures that a binding with a different servicekey
58+ /// can't override a binding with a non-matching servicekey
59+ /// </summary>
60+ public class ServiceTypeKey : IEquatable < ServiceTypeKey >
61+ {
62+ public Type ServiceType { get ; }
63+ public object IndexKey { get ; }
64+
65+ public ServiceTypeKey ( Type serviceType , object indexKey )
66+ {
67+ ServiceType = serviceType ;
68+ IndexKey = indexKey ;
69+ }
70+
71+ public bool Equals ( ServiceTypeKey other )
72+ {
73+ if ( other is null ) return false ;
74+ if ( ReferenceEquals ( this , other ) ) return true ;
75+ return Equals ( ServiceType , other . ServiceType ) && Equals ( IndexKey , other . IndexKey ) ;
76+ }
77+
78+ public override bool Equals ( object obj )
79+ {
80+ if ( obj is null ) return false ;
81+ if ( ReferenceEquals ( this , obj ) ) return true ;
82+ if ( obj . GetType ( ) != GetType ( ) ) return false ;
83+ return Equals ( ( ServiceTypeKey ) obj ) ;
84+ }
85+
86+ public override int GetHashCode ( )
87+ {
88+ return HashCode . Combine ( ServiceType , IndexKey ) ;
4989 }
5090 }
5191 }
0 commit comments