示例#1
0
        private static T GetOrCreate <T>(
            string key,
            IDataLoaderRegistry registry,
            Action <IDataLoaderRegistry> register)
            where T : IDataLoader
        {
            if (registry == null)
            {
                throw new InvalidOperationException(TypeResources
                                                    .DataLoaderResolverContextExtensions_RegistryIsNull);
            }

            if (!registry.TryGet(key, out T dataLoader))
            {
                register(registry);

                if (!registry.TryGet(key, out dataLoader))
                {
                    throw new InvalidOperationException(TypeResources
                                                        .DataLoaderResolverContextExtensions_UnableToRegister);
                }
            }

            return(dataLoader);
        }
        public static IDataLoader <TKey, TValue[]> GroupDataLoader <TKey, TValue>(
            this IResolverContext context,
            FetchGroup <TKey, TValue> fetch,
            string?key = null)
            where TKey : notnull
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (fetch is null)
            {
                throw new ArgumentNullException(nameof(fetch));
            }

            IServiceProvider    services = context.Services;
            IDataLoaderRegistry reg      = services.GetRequiredService <IDataLoaderRegistry>();
            Func <FetchGroupedDataLoader <TKey, TValue> > createDataLoader =
                () => new FetchGroupedDataLoader <TKey, TValue>(
                    services.GetRequiredService <IBatchScheduler>(),
                    fetch);

            return(key is null
                ? reg.GetOrRegister(createDataLoader)
                : reg.GetOrRegister(key, createDataLoader));
        }
        public static IDataLoader <TKey, TValue> CacheDataLoader <TKey, TValue>(
            this IResolverContext context,
            FetchCacheCt <TKey, TValue> fetch,
            string?key    = null,
            int cacheSize = DataLoaderDefaults.CacheSize)
            where TKey : notnull
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (fetch is null)
            {
                throw new ArgumentNullException(nameof(fetch));
            }

            IServiceProvider    services = context.Services;
            IDataLoaderRegistry reg      = services.GetRequiredService <IDataLoaderRegistry>();
            Func <FetchCacheDataLoader <TKey, TValue> > createDataLoader =
                () => new FetchCacheDataLoader <TKey, TValue>(
                    fetch,
                    cacheSize);

            return(key is null
                ? reg.GetOrRegister(createDataLoader)
                : reg.GetOrRegister(key, createDataLoader));
        }
        public static IDataLoader <TKey, TValue> BatchDataLoader <TKey, TValue>(
            this IResolverContext context,
            FetchBatch <TKey, TValue> fetch,
            string?key = null)
            where TKey : notnull
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (fetch is null)
            {
                throw new ArgumentNullException(nameof(fetch));
            }

            IServiceProvider    services = context.Services;
            IDataLoaderRegistry reg      = services.GetRequiredService <IDataLoaderRegistry>();

            FetchBatchDataLoader <TKey, TValue> Loader()
            => new(
                fetch,
                services.GetRequiredService <IBatchScheduler>(),
                services.GetRequiredService <DataLoaderOptions>());

            return(key is null
                ? reg.GetOrRegister(Loader)
                : reg.GetOrRegister(key, Loader));
        }
        public static bool Register <TValue>(
            this IDataLoaderRegistry registry,
            string key,
            FetchOnceFactory <TValue> factory)
        {
            if (string.IsNullOrEmpty(key))
            {
                // TODO : Resources
                throw new ArgumentException(
                          "The DataLoader key cannot be null or empty.",
                          nameof(key));
            }

            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            return(registry.Register(key, services =>
            {
                FetchOnce <TValue> fetch = factory(services);
                return new FetchSingleDataLoader <string, TValue>(
                    k => fetch(), DataLoaderDefaults.MinCacheSize);
            }));
        }
示例#6
0
        private static T GetOrCreate <T>(
            string key,
            IDataLoaderRegistry registry,
            Action <IDataLoaderRegistry> register)
            where T : IDataLoader
        {
            if (registry == null)
            {
                // TODO : resources
                throw new InvalidOperationException(
                          "No DataLoader registry was registerd with your " +
                          "dependency injection.");
            }

            if (!registry.TryGet(key, out T dataLoader))
            {
                register(registry);

                if (!registry.TryGet(key, out dataLoader))
                {
                    // TODO : resources
                    throw new InvalidOperationException(
                              "Unable to register a DataLoader with your " +
                              "DataLoader registry.");
                }
            }

            return(dataLoader);
        }
        public static bool Register <T>(
            this IDataLoaderRegistry registry)
            where T : class, IDataLoader
        {
            Func <IServiceProvider, T> createInstance =
                ActivatorHelper.CreateInstanceFactory <T>();

            return(registry.Register(typeof(T).FullName, createInstance));
        }
示例#8
0
        public static bool Register <T>(
            this IDataLoaderRegistry registry,
            string key)
            where T : class, IDataLoader
        {
            Func <IServiceProvider, T> createInstance =
                ActivatorHelper.CompileFactory <T>();

            return(registry.Register(key, createInstance));
        }
        public static T DataLoader <T>(this IResolverContext context)
            where T : notnull, IDataLoader
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            IServiceProvider    services = context.Services;
            IDataLoaderRegistry reg      = services.GetRequiredService <IDataLoaderRegistry>();

            return(reg.GetOrRegister(() => CreateDataLoader <T>(services)));
        }
示例#10
0
        public static bool Register <TKey, TValue>(
            this IDataLoaderRegistry registry,
            string key,
            FetchGroup <TKey, TValue> fetch)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException(
                          TypeResources.DataLoaderRegistry_KeyNullOrEmpty,
                          nameof(key));
            }

            if (fetch == null)
            {
                throw new ArgumentNullException(nameof(fetch));
            }

            return(registry.Register(key, services =>
                                     new FetchGroupedDataLoader <TKey, TValue>(fetch)));
        }
        public static bool Register <TKey, TValue>(
            this IDataLoaderRegistry registry,
            string key,
            FetchGrouped <TKey, TValue> fetch)
        {
            if (string.IsNullOrEmpty(key))
            {
                // TODO : Resources
                throw new ArgumentException(
                          "The DataLoader key cannot be null or empty.",
                          nameof(key));
            }

            if (fetch == null)
            {
                throw new ArgumentNullException(nameof(fetch));
            }

            return(registry.Register(key, services =>
                                     new FetchGroupedDataLoader <TKey, TValue>(fetch)));
        }
示例#12
0
        public static bool Register <TKey, TValue>(
            this IDataLoaderRegistry registry,
            string key,
            FetchCacheFactory <TKey, TValue> factory)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException(
                          TypeResources.DataLoaderRegistry_KeyNullOrEmpty,
                          nameof(key));
            }

            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            return(registry.Register(key, services =>
                                     new FetchSingleDataLoader <TKey, TValue>(
                                         factory(services))));
        }
示例#13
0
        private static bool TryGetDataLoader <T>(
            IResolverContext context,
            string key,
            out T dataLoader,
            out IDataLoaderRegistry registry)
            where T : IDataLoader
        {
            registry = null;

            foreach (IDataLoaderRegistry current in
                     context.Service <IEnumerable <IDataLoaderRegistry> >())
            {
                registry = current;

                if (current.TryGet(key, out dataLoader))
                {
                    return(true);
                }
            }

            dataLoader = default;
            return(false);
        }
示例#14
0
        public static bool Register <TValue>(
            this IDataLoaderRegistry registry,
            string key,
            FetchOnce <TValue> fetch)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException(
                          TypeResources.DataLoaderRegistry_KeyNullOrEmpty,
                          nameof(key));
            }

            if (fetch == null)
            {
                throw new ArgumentNullException(nameof(fetch));
            }

            return(registry.Register(key, services =>
            {
                return new FetchSingleDataLoader <string, TValue>(
                    k => fetch(), DataLoaderDefaults.MinCacheSize);
            }));
        }