Esempio n. 1
0
 public static DependencyCollection AddHttpHandler <THandler>(this DependencyCollection dependencies,
                                                              DependencyLifetime lifetime = DependencyLifetime.Singleton)
     where THandler : class, IHttpRequestHandler
 {
     dependencies.AddDependency(Typeof <IHttpRequestHandler> .Raw, typeof(THandler), lifetime);
     return(dependencies);
 }
Esempio n. 2
0
        public static DependencyCollection AddJsonConverter <TConverter>(this DependencyCollection dependencies)
            where TConverter : class, IJsonConverter
        {
            dependencies.AddDependency(ConverterContracts, typeof(TConverter), DependencyLifetime.Singleton);

            return(dependencies);
        }
Esempio n. 3
0
 public static DependencyCollection AddSingleton(
     this DependencyCollection dependencies,
     Type contract,
     Type implementation)
 {
     return(dependencies.AddDependency(contract, implementation, DependencyLifetime.Singleton));
 }
Esempio n. 4
0
 public static DependencyCollection AddTransient(
     this DependencyCollection dependencies,
     Type contract,
     Type implementation)
 {
     return(dependencies.AddDependency(contract, implementation, DependencyLifetime.Transient));
 }
Esempio n. 5
0
        public static DependencyCollection AddScoped <TContract, TImplementation>(this DependencyCollection dependencies)
            where TImplementation : TContract
        {
            var contract       = Typeof <TContract> .Raw;
            var implementation = typeof(TImplementation);

            return(dependencies.AddDependency(contract, implementation, DependencyLifetime.Scoped));
        }
Esempio n. 6
0
        public static DependencyCollection AddHttpHandler(this DependencyCollection dependencies,
                                                          Func <IServiceProvider, IHttpRequestHandler> builder,
                                                          DependencyLifetime lifetime = DependencyLifetime.Singleton)
        {
            var contracts = new[] { typeof(IHttpRequestHandler) };

            dependencies.AddDependency(contracts, builder, lifetime);
            return(dependencies);
        }
Esempio n. 7
0
        public static DependencyCollection AddTransient <TContract>(
            this DependencyCollection dependencies,
            Func <IServiceProvider, TContract> builder)
            where TContract : class
        {
            var contracts = new[] { typeof(TContract) };

            return(dependencies.AddDependency(contracts, builder, DependencyLifetime.Transient));
        }
Esempio n. 8
0
        public static DependencyCollection AddMetricsCounter <TSender>(this DependencyCollection dependencies,
                                                                       Func <IServiceProvider, ICounter <TSender> > builder)
        {
            var contracts = new[] { typeof(ICounter), typeof(ICounter <TSender>) };

            dependencies.AddDependency(contracts, builder, DependencyLifetime.Singleton);

            return(dependencies);
        }
Esempio n. 9
0
        public static DependencyCollection AddLogWriter <TWriter>(this DependencyCollection dependencies)
            where TWriter : ILogWriter
        {
            var implementation = Typeof <TWriter> .Raw;
            var contracts      = new[] { Typeof <ILogWriter> .Raw, implementation };

            dependencies.AddDependency(contracts, implementation, DependencyLifetime.Singleton);

            return(dependencies);
        }
Esempio n. 10
0
        public static DependencyCollection AddJsonSettings(this DependencyCollection dependencies,
                                                           Func <IServiceProvider, string> fileNameBuilder, bool required = false)
        {
            dependencies.AddDependency(
                SettingsSourceContract,
                provider => new JsonFileSource(fileNameBuilder(provider), required),
                DependencyLifetime.Singleton);

            return(dependencies);
        }
Esempio n. 11
0
        public static DependencyCollection AddAssets(
            this DependencyCollection dependencies,
            Func <IServiceProvider, IEntitySource <Asset> > sourceBuilder)
        {
            if (sourceBuilder == null)
            {
                throw Error.Null(nameof(sourceBuilder));
            }

            dependencies.AddDependency(AssetSourceContracts, sourceBuilder, DependencyLifetime.Singleton);

            return(dependencies);
        }
Esempio n. 12
0
        public static DependencyCollection AddSingleton(this DependencyCollection dependencies, Type implementation)
        {
            if (implementation.IsGenericTypeDefinition)
            {
                var factory = new GenericFactory(implementation, implementation, DependencyLifetime.Singleton);
                dependencies.AddFactory(factory);
            }
            else
            {
                dependencies.AddDependency(implementation, implementation, DependencyLifetime.Singleton);
            }

            return(dependencies);
        }
Esempio n. 13
0
        public static DependencyCollection AddSystem <TSystem>(
            this DependencyCollection dependencies,
            DependencyLifetime lifetime = DependencyLifetime.Singleton)
        {
            var implementation = typeof(TSystem);

            if (!ECSUtils.TryGetImplementedSystemInterfaces(implementation, out var contracts))
            {
                throw Error.InvalidOperation(
                          $"Type '{ReflectionUtils.GetName<TSystem>()}' isn't implemented system interfaces");
            }

            dependencies.AddDependency(contracts, implementation, lifetime);
            return(dependencies);
        }
Esempio n. 14
0
        public static DependencyCollection AddAssets(this DependencyCollection dependencies, string filePath)
        {
            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw Error.Null(nameof(filePath));
            }

            dependencies.AddDependency(AssetSourceContracts,
                                       provider => new JsonFileSource <Asset>(
                                           provider.GetRequired <IConvertersCollection>(),
                                           provider.GetRequired <SourceDescriptions>(),
                                           filePath),
                                       DependencyLifetime.Singleton);

            return(dependencies);
        }
Esempio n. 15
0
        public static DependencyCollection AddAssets(this DependencyCollection dependencies, Stream stream)
        {
            if (stream == null)
            {
                throw Error.Null(nameof(stream));
            }

            dependencies.AddDependency(AssetSourceContracts,
                                       provider => new JsonStreamSource <Asset>(
                                           provider.GetRequired <IConvertersCollection>(),
                                           provider.GetRequired <SourceDescriptions>(),
                                           stream),
                                       DependencyLifetime.Singleton);

            return(dependencies);
        }
Esempio n. 16
0
        private static bool AddDependencies(
            DependencyCollection dependencies,
            Type implementation,
            Type[] genericInterfaces,
            DependencyLifetime lifetime)
        {
            var contracts = ReflectionUtils.GetGenericInterfaceImplementations(implementation, genericInterfaces);

            if (contracts.Length == 0)
            {
                return(false);
            }

            contracts.Add(implementation);
            dependencies.AddDependency(contracts.ToArray(), implementation, lifetime);
            return(true);
        }
Esempio n. 17
0
        public static DependencyCollection AddTransient <TImplementation>(this DependencyCollection dependencies)
        {
            var implementation = Typeof <TImplementation> .Raw;

            return(dependencies.AddDependency(implementation, implementation, DependencyLifetime.Transient));
        }
Esempio n. 18
0
        public static DependencyCollection AddSingleton <TImplementation>(this DependencyCollection dependencies)
        {
            var implementation = Typeof <TImplementation> .Raw;

            return(dependencies.AddDependency(implementation, implementation, DependencyLifetime.Singleton));
        }
Esempio n. 19
0
 public static DependencyCollection AddScoped(
     this DependencyCollection dependencies,
     Type implementation)
 {
     return(dependencies.AddDependency(implementation, implementation, DependencyLifetime.Scoped));
 }