private void ThrowExceptionIfRegistryNotValid(IResolverRegistry registry, string interfaceName)
 {
     if (registry == null ||
         (string.IsNullOrWhiteSpace(registry.Interface) || !registry.Interface.Equals(interfaceName)) ||
         string.IsNullOrWhiteSpace(registry.Class))
     {
         throw new ApplicationException($"Registration for {interfaceName} is not setup correctly.");
     }
 }
        public object ExecuteModule(Type interfaceType, IResolver resolver, object existingObject)
        {
            // If we already have an object, just return that object (this module is for creating a new one from the Factory)
            if (existingObject != null)
            {
                return(existingObject);
            }

            if (resolver == null)
            {
                throw new ArgumentNullException(nameof(resolver), $"{nameof(FactoryModule)} requires a non-null {nameof(IResolver)}");
            }
            else if (interfaceType == null)
            {
                throw new ArgumentNullException(nameof(interfaceType), $"{nameof(FactoryModule)} requires a non-null {nameof(interfaceType)}");
            }

            FactoryRegistry registry = RegistrationParser.GetRegistrationRegistry <FactoryRegistry>(interfaceType.Name);

            if (string.IsNullOrWhiteSpace(registry.Factory))
            {
                throw new InvalidOperationException($"No factory setup was registered to the Interface \"{interfaceType.Name}\" within the Registration.");
            }

            // Generate class from Factory
            IResolverRegistry factoryRegistry = RegistrationParser.GetRegistrationRegistry(registry.Factory);

            foreach (Type factoryInterfaceType in RegistrationParser.GetInterfaceTypesInAssemblies())
            {
                if (!factoryInterfaceType.Name.Equals(factoryRegistry.Interface))
                {
                    continue;
                }

                object     factory       = resolver.Resolve(factoryInterfaceType);
                MethodInfo factoryMethod = factory.GetType()
                                           .GetMethod(registry.FactoryMethod);

                try
                {
                    ParameterInfo parameter = factoryMethod.GetParameters()[0];
                    if (parameter.ParameterType != typeof(IResolver))
                    {
                        throw new NotSupportedException($"{nameof(FactoryModule)} does not support calling factory methods that require unique parameters beyond just the {nameof(IResolver)}.");
                    }

                    return(factoryMethod.Invoke(factory, new[] { resolver }));
                }
                catch (IndexOutOfRangeException) // No parameters thus the GetParameters()[0} would throw this
                {
                    return(factoryMethod.Invoke(factory, null));
                }
            }

            throw new TypeAccessException($"Interface \"{interfaceType.Name}\" was marked to use a Factory but the Factory could not be mapped to. Searched for \"{factoryRegistry.Class}\"");
        }
        public Type GetRegistryClassType(IResolverRegistry registry)
        {
            foreach (Type classType in _parser.GetClassTypesInAssemblies())
            {
                if (classType.FullName.Equals(registry.Class))
                {
                    return(classType);
                }
            }

            throw new TypeAccessException($"Failed to map the provided Interface Type \"{registry.Interface}\" to a respective Class Type in the Registration. Is the Registration properly setup?");
        }
예제 #4
0
        public InstantiatedObject MapInterfaceTypeToInstantiatedObject(Type interfaceType, object[] constructorData, IResolver resolver, Func <object, IResolverModule, Type, IResolver, object> moduleLogic, Func <IList <IResolverModule>, IList <IResolverModule> > moduleFilter)
        {
            object            newInstance = MapInterfaceTypeToClassInstance(interfaceType, constructorData, resolver, moduleLogic, moduleFilter);
            IResolverRegistry registry    = _parser.GetRegistrationRegistry(interfaceType.Name);

            return(new InstantiatedObject()
            {
                InterfaceType = interfaceType,
                ClassType = newInstance.GetType(),
                TheObject = newInstance,
                AllowMultiple = registry.Multiple
            });
        }
예제 #5
0
 public SchemaConfiguration(
     Action <IServiceProvider> registerServiceProvider,
     ITypeRegistry typeRegistry,
     IResolverRegistry resolverRegistry,
     IDirectiveRegistry directiveRegistry)
 {
     _registerServiceProvider = registerServiceProvider
                                ?? throw new ArgumentNullException(
                                          nameof(registerServiceProvider));
     _typeRegistry = typeRegistry
                     ?? throw new ArgumentNullException(nameof(typeRegistry));
     _resolverRegistry = resolverRegistry
                         ?? throw new ArgumentNullException(nameof(resolverRegistry));
     _directiveRegistry = directiveRegistry
                          ?? throw new ArgumentNullException(nameof(directiveRegistry));
 }
예제 #6
0
 private void CompleteResolver(
     IResolverRegistry resolverRegistry,
     Action <SchemaError> reportError,
     INamedType parentType)
 {
     if (parentType is ObjectType ot && Resolver == null)
     {
         Resolver = resolverRegistry.GetResolver(parentType.Name, Name);
         if (Resolver == null)
         {
             reportError(new SchemaError(
                             $"The field `{parentType.Name}.{Name}` " +
                             "has no resolver.", parentType));
         }
     }
 }
예제 #7
0
        public Type MapInterfaceTypeToClassType(Type interfaceType)
        {
            if (interfaceType == null)
            {
                throw new ArgumentNullException(nameof(interfaceType), $"Cannot map a NULL Interface Type to a Class Type.");
            }

            if (!interfaceType.IsInterface)
            {
                throw new InvalidOperationException($"Interface Type \"{interfaceType.FullName}\" did not resolve to an Interface.");
            }

            IResolverRegistry registry = _parser.GetRegistrationRegistry(interfaceType.Name);

            return(_registryParser.GetRegistryClassType(registry));
        }
        public IResolverRegistry GetRegistrationRegistry(string interfaceName)
        {
            IResolverRegistry interfaceRegistry = null;

            foreach (JToken token in GetRegistration())
            {
                IResolverRegistry registry = token.ToObject <BaseRegistry>();
                if (registry.Interface.Equals(interfaceName))
                {
                    interfaceRegistry = registry;
                    break;
                }
            }

            ThrowExceptionIfRegistryNotValid(interfaceRegistry, interfaceName);

            return(interfaceRegistry);
        }
        public TResolverRegistry GetRegistrationRegistry <TResolverRegistry>(string interfaceName) where TResolverRegistry : BaseRegistry
        {
            IResolverRegistry interfaceRegistry = null;

            foreach (JToken token in GetRegistration())
            {
                // NOTE: ToObject requires a Class Type. Potentially will need custom mapping logic for any custom IResolverRegistry
                IResolverRegistry registry = token.ToObject <TResolverRegistry>();
                if (registry.Interface.Equals(interfaceName))
                {
                    interfaceRegistry = registry;
                    break;
                }
            }

            ThrowExceptionIfRegistryNotValid(interfaceRegistry, interfaceName);

            return((TResolverRegistry)interfaceRegistry);
        }
예제 #10
0
 private FieldResolverDelegate CreateResolver(
     IResolverRegistry resolverRegistry)
 {
     return(Resolver ?? resolverRegistry.GetResolver(_typeName, Name));
 }