Beispiel #1
0
        public static ServicesOptions Build(Action <ServicesOptions> optionBuilder)
        {
            var result = new ServicesOptions();

            optionBuilder?.Invoke(result);

            return(result);
        }
Beispiel #2
0
        public static IServiceCollection AddServices(this IServiceCollection services, ServicesOptions options)
        {
            options = options ?? new ServicesOptions();

            var profiler = new ServiceProfiler();

            foreach (var assembly in options.Assemblies)
            {
                profiler.Scan(assembly);
            }

            foreach (var service in profiler.Services)
            {
                Func <Type, Type, IServiceCollection> registerAction;

                switch (service.Lifetime)
                {
                case DI.ServiceDILifetime.Singleton:
                    registerAction = services.AddSingleton;
                    break;

                case DI.ServiceDILifetime.Scoped:
                    registerAction = services.AddScoped;
                    break;

                case DI.ServiceDILifetime.Instance:
                    registerAction = services.AddTransient;
                    break;

                default:
                    throw new ArgumentException("Unknown service lifetime: " + service.Lifetime);
                }

                foreach (var injectedAs in service.InjectAsType)
                {
                    registerAction(injectedAs, service.ImplementedType);
                }
            }

            return(services);
        }
Beispiel #3
0
 public static IServiceCollection AddServices(this IServiceCollection services, Action <ServicesOptions> optionsBuilder)
 {
     return(AddServices(services, ServicesOptions.Build(optionsBuilder)));
 }
Beispiel #4
0
 public static IServiceCollection AddServices(this IServiceCollection services)
 {
     return(AddServices(services, ServicesOptions.Build(null)));
 }