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);
            }));
        }
Пример #2
0
        private static Func <Task <TValue> > DataLoader <TValue>(
            this IResolverContext context,
            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));
            }

            if (!TryGetDataLoader(context, key,
                                  out IDataLoader <string, TValue> dataLoader,
                                  out IDataLoaderRegistry registry))
            {
                dataLoader = GetOrCreate <IDataLoader <string, TValue> >(
                    key, registry, r => r.Register(key, factory));
            }

            return(() => dataLoader.LoadAsync("none"));
        }
Пример #3
0
        public static Task <TValue> FetchOnceAsync <TValue>(
            this IResolverContext context,
            string key,
            FetchOnce <TValue> fetch)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException(
                          TypeResources.DataLoaderRegistry_KeyNullOrEmpty,
                          nameof(key));
            }

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

            return(FetchOnceFactory(context, key, services => fetch)());
        }
Пример #4
0
        private static Func <Task <TValue> > FetchOnceFactory <TValue>(
            this IResolverContext context,
            string key,
            FetchOnceFactory <TValue> factory)
        {
            if (!TryGetDataLoader(context, key,
                                  out IDataLoader <string, TValue> dataLoader,
                                  out IDataLoaderRegistry registry))
            {
                dataLoader = GetOrCreate <IDataLoader <string, TValue> >(
                    key, registry, r => r.Register(key, factory));
            }

            return(() => dataLoader.LoadAsync("none"));
        }