private void AddResourceDefinitions(Assembly assembly, ResourceDescriptor resourceDescriptor)
 {
     foreach (Type resourceDefinitionInterface in ResourceDefinitionInterfaces)
     {
         RegisterImplementations(assembly, resourceDefinitionInterface, resourceDescriptor);
     }
 }
 private void AddServices(Assembly assembly, ResourceDescriptor resourceDescriptor)
 {
     foreach (Type serviceInterface in ServiceInterfaces)
     {
         RegisterImplementations(assembly, serviceInterface, resourceDescriptor);
     }
 }
 private void AddRepositories(Assembly assembly, ResourceDescriptor resourceDescriptor)
 {
     foreach (var repositoryInterface in RepositoryInterfaces)
     {
         RegisterImplementations(assembly, repositoryInterface, resourceDescriptor);
     }
 }
示例#4
0
 /// <summary>
 /// Attempts to get a descriptor for the resource type.
 /// </summary>
 /// <returns>
 /// <c>true</c> if the type is a valid json:api type (must implement <see cref="IIdentifiable"/>); <c>false</c>, otherwise.
 /// </returns>
 internal static bool TryGetResourceDescriptor(Type type, out ResourceDescriptor descriptor)
 {
     if (TypeHelper.IsOrImplementsInterface(type, typeof(IIdentifiable)))
     {
         descriptor = new ResourceDescriptor(type, GetIdType(type));
         return(true);
     }
     descriptor = ResourceDescriptor.Empty;
     return(false);
 }
示例#5
0
        private void RegisterImplementations(Assembly assembly, Type interfaceType, ResourceDescriptor resourceDescriptor)
        {
            var genericArguments = interfaceType.GetTypeInfo().GenericTypeParameters.Length == 2 ? new[] { resourceDescriptor.ResourceType, resourceDescriptor.IdType } : new[] { resourceDescriptor.ResourceType };
            var result           = TypeLocator.GetGenericInterfaceImplementation(assembly, interfaceType, genericArguments);

            if (result != null)
            {
                var(implementation, registrationInterface) = result.Value;
                _services.AddScoped(registrationInterface, implementation);
            }
        }
        private IEnumerable <ResourceDescriptor> ScanForResourceDescriptors(Assembly assembly)
        {
            foreach (Type type in assembly.GetTypes())
            {
                ResourceDescriptor resourceDescriptor = _typeLocator.TryGetResourceDescriptor(type);

                if (resourceDescriptor != null)
                {
                    yield return(resourceDescriptor);
                }
            }
        }
        private void RegisterImplementations(Assembly assembly, Type interfaceType, ResourceDescriptor resourceDescriptor)
        {
            Type[] genericArguments = interfaceType.GetTypeInfo().GenericTypeParameters.Length == 2
                ? ArrayFactory.Create(resourceDescriptor.ResourceType, resourceDescriptor.IdType)
                : ArrayFactory.Create(resourceDescriptor.ResourceType);

            (Type implementation, Type registrationInterface)? result =
                _typeLocator.GetGenericInterfaceImplementation(assembly, interfaceType, genericArguments);

            if (result != null)
            {
                (Type implementation, Type registrationInterface) = result.Value;
                _services.AddScoped(registrationInterface, implementation);
            }
        }
示例#8
0
        private void AddResourceHookDefinitions(Assembly assembly, ResourceDescriptor identifiable)
        {
            try
            {
                var resourceDefinition = TypeLocator.GetDerivedGenericTypes(assembly, typeof(ResourceHooksDefinition <>), identifiable.ResourceType)
                                         .SingleOrDefault();

                if (resourceDefinition != null)
                {
                    _services.AddScoped(typeof(ResourceHooksDefinition <>).MakeGenericType(identifiable.ResourceType), resourceDefinition);
                }
            }
            catch (InvalidOperationException e)
            {
                throw new InvalidConfigurationException($"Cannot define multiple ResourceHooksDefinition<> implementations for '{identifiable.ResourceType}'", e);
            }
        }
        private static ResourceDescriptor TryGetResourceTypeFromServiceImplementation(Type serviceType)
        {
            foreach (Type @interface in serviceType.GetInterfaces())
            {
                Type firstGenericArgument = @interface.IsGenericType ? @interface.GenericTypeArguments.First() : null;

                if (firstGenericArgument != null)
                {
                    ResourceDescriptor resourceDescriptor = TypeLocator.TryGetResourceDescriptor(firstGenericArgument);

                    if (resourceDescriptor != null)
                    {
                        return(resourceDescriptor);
                    }
                }
            }

            return(null);
        }
        private static void RegisterForConstructedType(IServiceCollection services, Type implementationType, IEnumerable <Type> openGenericInterfaces)
        {
            bool seenCompatibleInterface          = false;
            ResourceDescriptor resourceDescriptor = TryGetResourceTypeFromServiceImplementation(implementationType);

            if (resourceDescriptor != null)
            {
                foreach (Type openGenericInterface in openGenericInterfaces)
                {
                    // A shorthand interface is one where the ID type is omitted.
                    // e.g. IResourceService<TResource> is the shorthand for IResourceService<TResource, TId>
                    bool isShorthandInterface = openGenericInterface.GetTypeInfo().GenericTypeParameters.Length == 1;

                    if (isShorthandInterface && resourceDescriptor.IdType != typeof(int))
                    {
                        // We can't create a shorthand for ID types other than int.
                        continue;
                    }

                    Type constructedType = isShorthandInterface
                        ? openGenericInterface.MakeGenericType(resourceDescriptor.ResourceType)
                        : openGenericInterface.MakeGenericType(resourceDescriptor.ResourceType, resourceDescriptor.IdType);

                    if (constructedType.IsAssignableFrom(implementationType))
                    {
                        services.AddScoped(constructedType, implementationType);
                        seenCompatibleInterface = true;
                    }
                }
            }

            if (!seenCompatibleInterface)
            {
                throw new InvalidConfigurationException($"{implementationType} does not implement any of the expected JsonApiDotNetCore interfaces.");
            }
        }
 private void AddResource(ResourceDescriptor resourceDescriptor)
 {
     _resourceGraphBuilder.Add(resourceDescriptor.ResourceType, resourceDescriptor.IdType);
 }
示例#12
0
        private void AddResource(Assembly assembly, ResourceDescriptor resourceDescriptor)
        {
            RegisterResourceDefinition(assembly, resourceDescriptor);

            _resourceGraphBuilder.Add(resourceDescriptor.ResourceType, resourceDescriptor.IdType);
        }
示例#13
0
        private void RegisterServiceImplementations(Assembly assembly, Type interfaceType, ResourceDescriptor resourceDescriptor)
        {
            if (resourceDescriptor.IdType == typeof(Guid) && interfaceType.GetTypeInfo().GenericTypeParameters.Length == 1)
            {
                return;
            }
            var genericArguments = interfaceType.GetTypeInfo().GenericTypeParameters.Length == 2 ? new[] { resourceDescriptor.ResourceType, resourceDescriptor.IdType } : new[] { resourceDescriptor.ResourceType };
            var service          = TypeLocator.GetGenericInterfaceImplementation(assembly, interfaceType, genericArguments);

            if (service.implementation != null)
            {
                _services.AddScoped(service.registrationInterface, service.implementation);
            }
        }