Esempio n. 1
0
        public static bool Register <T>(
            this IDataLoaderRegistry registry)
            where T : class, IDataLoader
        {
            Func <IServiceProvider, T> createInstance =
                ActivatorHelper.CompileFactory <T>();

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

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

            if (TryGetDataLoader(context, key,
                                 out T dataLoader,
                                 out IDataLoaderRegistry registry))
            {
                return(dataLoader);
            }

            return(GetOrCreate <T>
                   (
                       key,
                       registry,
                       r => r.Register(key, services =>
            {
                var instance = (T)services.GetService(typeof(T));
                return instance is null
                        ? ActivatorHelper.CompileFactory <T>().Invoke(services)
                        : instance;
            })
                   ));
        }
Esempio n. 3
0
 public object?CreateInstance(Type type) =>
 ActivatorHelper.CompileFactory(type)(_services);
Esempio n. 4
0
 public T CreateInstance <T>() =>
 ActivatorHelper.CompileFactory <T>()(_services);