SmartDIContainer.cs
/// <summary>
/// Adds a list of types that the type can be resolved as. Includes creators and storage rules.
/// </summary>
/// <param name="classT">The base type for the class rule.</param>
/// <param name="creatorsAndRules">The list of class creators and rules. The creators can be null.</param>
Adds a list of types that the type can be resolved as. Includes creators and storage rules.
Adds a list of types that the type can be resolved as . Includes creators and storage rules .
public void RegisterTypeContracts
      (
         Type                                             classT,
         IDictionary<Type, IProvideCreatorAndStorageRule> creatorsAndRules
      )
      {
         if (!classT.IsPublic || !classT.IsClass || classT.IsGenericType || classT.IsSealed || classT.IsAbstract ||
             classT.IsInterface)
         {
            ThrowArgumentException(nameof(RegisterTypeContracts),
                                   "Type ->" + classT + "<- must be a standard public type");
            return;
         }
         if (creatorsAndRules.IsEmpty())
         {
            creatorsAndRules = new ConcurrentDictionary<Type, IProvideCreatorAndStorageRule>();
            creatorsAndRules.Add(classT, new CreatorAndStorageRule());
         }
         else
         {
            foreach (var creatorAndRule in creatorsAndRules)
            {
               if (!classT.IsTypeOrAssignableFromType(creatorAndRule.Key))
               {
                  ThrowArgumentException(nameof(RegisterTypeContracts),
                                         "Type ->" + classT + "<- cannot be cast as ->" +
                                         creatorAndRule.Key);
                  return;
               }
            }
         }
         var creatorsAndRulesTypes = creatorsAndRules.Keys;
         var competingContracts = _registeredTypeContracts
                                 .Where(rc => (rc.Key != classT) && creatorsAndRulesTypes
                                                                   .Intersect(rc.Value.CreatorsAndStorageRules.Keys)
                                                                   .Any())
                                 .ToList();
         if (competingContracts.Any())
         {
            if (ThrowOnMultipleRegisteredTypesForOneResolvedType)
            {
               ThrowArgumentException(nameof(RegisterTypeContracts),
                                      "Cannot register a second contract when the property ->" +
                                      nameof(ThrowOnMultipleRegisteredTypesForOneResolvedType) +
                                      "< is true.  The competing contract is of type ->"       +
                                      competingContracts                                       +
                                      "<-");
               return;
            }
         }
         if (!_registeredTypeContracts.ContainsKey(classT))
         {
            _registeredTypeContracts.Add(classT,
                                         new TimeStampedCreatorAndStorageRules
                                         { WhenAdded = DateTime.Now, CreatorsAndStorageRules = creatorsAndRules });
         }
         else
         {
            _registeredTypeContracts[classT] = SeekExistingContract(classT, creatorsAndRules);
         }
      }
public void RegisterTypeContracts ( Type classT , IDictionary < Type , IProvideCreatorAndStorageRule > creatorsAndRules ) { if ( ! classT . IsPublic || ! classT . IsClass || classT . IsGenericType || classT . IsSealed || classT . IsAbstract || classT . IsInterface ) { ThrowArgumentException ( nameof ( RegisterTypeContracts ) , " " + classT + " " ) ; return ; } if ( creatorsAndRules . IsEmpty ( ) ) { creatorsAndRules = new ConcurrentDictionary < Type , IProvideCreatorAndStorageRule > ( ) ; creatorsAndRules . Add ( classT , new CreatorAndStorageRule ( ) ) ; } else { foreach ( var creatorAndRule in creatorsAndRules ) { if ( ! classT . IsTypeOrAssignableFromType ( creatorAndRule . Key ) ) { ThrowArgumentException ( nameof ( RegisterTypeContracts ) , " " + classT + " " + creatorAndRule . Key ) ; return ; } } } var creatorsAndRulesTypes = creatorsAndRules . Keys ; var competingContracts = _registeredTypeContracts . Where ( rc => ( rc . Key != classT ) && creatorsAndRulesTypes . Intersect ( rc . Value . CreatorsAndStorageRules . Keys ) . Any ( ) ) . ToList ( ) ; if ( competingContracts . Any ( ) ) { if ( ThrowOnMultipleRegisteredTypesForOneResolvedType ) { ThrowArgumentException ( nameof ( RegisterTypeContracts ) , " " + nameof ( ThrowOnMultipleRegisteredTypesForOneResolvedType ) + " " + competingContracts + " " ) ; return ; } } if ( ! _registeredTypeContracts . ContainsKey ( classT ) ) { _registeredTypeContracts . Add ( classT , new TimeStampedCreatorAndStorageRules { WhenAdded = DateTime . Now , CreatorsAndStorageRules = creatorsAndRules } ) ; } else { _registeredTypeContracts [ classT ] = SeekExistingContract ( classT , creatorsAndRules ) ; } }
Adds a list of types that the type can be resolved as.
Adds a list of types that the type can be resolved as .
// ClassT must be a standard public type -- otherwise, can't be instantiated // ClassT may or may not be included in the resolvable types. // *However*, if there are no resolvable types (no 'creators and rules'), then we force ClassT to be resolvable by adding it here manually. //  Otherwise, this is a meaningless registration that will always fail. // Add the new rule as the only member - order "0" // Interrogate the creatorsAdRules to make sure that all requested types can indeed be cast from classT. // Are there other registrants that already return/resolve the any of the same classes as we do in our creators // and rules? If so, is it their master type the same as us (probably not unless we have duplicated this // registration exactly)? // If there is more than one master type, we should consider throwing, as this is extremely confusing for resolution. // All is good, so register // If the contract does not exist, add it and block-copy in the entire list of creators and rules // Return the value to the array
classT
Type
The base type for the class rule.
The base type for the class rule .
creatorsAndRules
IDictionary<Type, IProvideCreatorAndStorageRule>
The list of class creators and rules. The creators can be null.
The list of class creators and rules . The creators can be null .