private void AddRepositories(Assembly assembly, ResourceDescriptor resourceDescriptor)
 {
     foreach (var serviceInterface in RepositoryInterfaces)
     {
         RegisterServiceImplementations(assembly, serviceInterface, resourceDescriptor);
     }
 }
예제 #2
0
 /// <summary>
 /// Attempts to get a descriptor of the resource type.
 /// </summary>
 /// <returns>
 /// True if the type is a valid json:api type (must implement <see cref="IIdentifiable"/>), false otherwise.
 /// </returns>
 internal static bool TryGetResourceDescriptor(Type type, out ResourceDescriptor descriptor)
 {
     if (type.Implements <IIdentifiable>())
     {
         descriptor = new ResourceDescriptor(type, GetIdType(type));
         return(true);
     }
     descriptor = ResourceDescriptor.Empty;
     return(false);
 }
예제 #3
0
        /// <summary>
        /// Attempts to get a descriptor of the resource type.
        /// </summary>
        /// <returns>
        /// True if the type is a valid json:api type (must implement <see cref="IIdentifiable"/>), false otherwise.
        /// </returns>
        internal static bool TryGetResourceDescriptor(Type type, out ResourceDescriptor descriptor)
        {
            var possible = GetIdType(type);

            if (possible.isJsonApiResource)
            {
                descriptor = new ResourceDescriptor(type, possible.idType);
                return(true);
            }

            descriptor = ResourceDescriptor.Empty;
            return(false);
        }
        private void RegisterResourceDefinition(Assembly assembly, ResourceDescriptor identifiable)
        {
            try
            {
                var resourceDefinition = TypeLocator.GetDerivedGenericTypes(assembly, typeof(ResourceDefinition <>), identifiable.ResourceType)
                                         .SingleOrDefault();

                if (resourceDefinition != null)
                {
                    _services.AddScoped(typeof(ResourceDefinition <>).MakeGenericType(identifiable.ResourceType), resourceDefinition);
                }
            }
            catch (InvalidOperationException e)
            {
                throw new JsonApiSetupException($"Cannot define multiple ResourceDefinition<> implementations for '{identifiable.ResourceType}'", e);
            }
        }
        private void RegisterResourceDefinition(Assembly assembly, ResourceDescriptor identifiable)
        {
            try
            {
                var resourceDefinition = TypeLocator.GetDerivedGenericTypes(assembly, typeof(ResourceDefinition <>), identifiable.ResourceType)
                                         .SingleOrDefault();

                if (resourceDefinition != null)
                {
                    _services.AddScoped(typeof(ResourceDefinition <>).MakeGenericType(identifiable.ResourceType), resourceDefinition);
                }
            }
            catch (InvalidOperationException e)
            {
                // TODO: need a better way to communicate failure since this is unlikely to occur during a web request
                throw new JsonApiException(500,
                                           $"Cannot define multiple ResourceDefinition<> implementations for '{identifiable.ResourceType}'", e);
            }
        }
        private void AddResourceToGraph(ResourceDescriptor identifiable, IResourceNameFormatter resourceNameFormatter = null)
        {
            var resourceName = FormatResourceName(identifiable.ResourceType, resourceNameFormatter);

            _graphBuilder.AddResource(identifiable.ResourceType, identifiable.IdType, resourceName);
        }
        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?.Name == "CustomArticleService" && genericArguments[0].Name != "Article")
            //{
            //    service = TypeLocator.GetGenericInterfaceImplementation(assembly, interfaceType, genericArguments);
            //}
            if (service.implementation != null)
            {
                _services.AddScoped(service.registrationInterface, service.implementation);
            }
        }
 private void AddResourceToGraph(ResourceDescriptor identifiable)
 {
     _resourceGraphBuilder.AddResource(identifiable.ResourceType, identifiable.IdType);
 }
 private void AddResource(Assembly assembly, ResourceDescriptor resourceDescriptor)
 {
     RegisterResourceDefinition(assembly, resourceDescriptor);
     AddResourceToGraph(resourceDescriptor);
 }
예제 #10
0
        private void AddResource(Assembly assembly, ResourceDescriptor resourceDescriptor)
        {
            RegisterResourceDefinition(assembly, resourceDescriptor);

            _resourceGraphBuilder.AddResource(resourceDescriptor.ResourceType, resourceDescriptor.IdType);
        }
예제 #11
0
        private void RegisterServiceImplementations(Assembly assembly, Type interfaceType, ResourceDescriptor resourceDescriptor)
        {
            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);
            }
        }