Exemplo n.º 1
0
        public IDependency BuildDependency(Type contract, IDependencyEngine engine)
        {
            var genericArgs = contract.GenericTypeArguments;

            Type pipelineType;

            if (ExistsBehaviour(genericArgs, engine))
            {
                pipelineType = typeof(CommandFullPipeline <>);
            }
            else if (ExistsPreOrPostProcessor(genericArgs, engine))
            {
                pipelineType = typeof(CommandSequentialPipeline <>);
            }
            else
            {
                pipelineType = typeof(CommandSimplePipeline <>);
            }

            var implementation = pipelineType.MakeGenericType(genericArgs);
            var lifetime       = engine.DefineLifetime(implementation);
            var resolver       = DependencyResolver.Build(lifetime, implementation);

            return(Dependency.Build(lifetime, new[] { contract }, resolver));
        }
Exemplo n.º 2
0
        public static IContainer Initialize()
        {
            Logger.Info(typeof(ContainerConfig), "Initializing container...");

            ContainerBuilder builder = new ContainerBuilder();

            ConfigFileConfigurationProvider configurationProvider = new ConfigFileConfigurationProvider();

            builder.RegisterInstance(configurationProvider.Service).As <IServiceConfiguration>();
            builder.RegisterInstance(configurationProvider.Repository).As <IApplicationRepositoryConfiguration>();
            builder.RegisterInstance(configurationProvider.Authentication).As <IAuthenticationConfiguration>();

            builder.RegisterType <FileSystemApplicationRepository>().As <IApplicationRepository>();

            builder.RegisterType <SystemDateTimeFactory>().As <IDateTimeFactory>();

            // register API controllers
            builder.RegisterApiControllers(typeof(PackagesApiController).Assembly);
            builder.RegisterControllers(typeof(HomeController).Assembly);

            // build container
            Container = Dependency.Build(builder);
            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(Container);
            DependencyResolver.SetResolver(new AutofacDependencyResolver(Container));

            Logger.Info(typeof(ContainerConfig), "Container initialized");
            return(Container);
        }
Exemplo n.º 3
0
        public DependencyCollection AddDependency(Type[] contracts, Type implementation, DependencyLifetime lifetime)
        {
            var resolver   = DependencyResolver.Build(lifetime, implementation, _engine);
            var dependency = Dependency.Build(lifetime, contracts, resolver);

            return(AddDependency(dependency));
        }
Exemplo n.º 4
0
        public IDependency BuildDependency(Type contract, IDependencyEngine engine)
        {
            var contractGenericArgs = contract.GenericTypeArguments;
            var notificationType    = contractGenericArgs[0];

            var processorType      = typeof(INotificationProcessor <>).MakeGenericType(contractGenericArgs);
            var dependencies       = engine.GetApplicable(processorType);
            var dependenciesLength = dependencies.Length;

            var pipelineType = dependenciesLength switch
            {
                0 => throw Error.DependencyNotRegistered(processorType),
                      1 => typeof(NotificationSimplePipeline <>),
                      _ => ParallelAttribute.IsDefined(notificationType)
                    ? typeof(NotificationParallelPipeline <>)
                    : typeof(NotificationSequentialPipeline <>)
            };

            var implementation = pipelineType.MakeGenericType(contractGenericArgs);
            var lifetime       = dependencies.DefineLifetime();
            var resolver       = DependencyResolver.Build(lifetime, implementation);

            return(Dependency.Build(lifetime, new[] { contract }, resolver));
        }
    }
Exemplo n.º 5
0
        public DependencyCollection AddDependency <TResult>(
            Type[] contracts,
            Func <IServiceProvider, TResult> builder,
            DependencyLifetime lifetime)
            where TResult : class
        {
            var implementation = typeof(TResult);

            foreach (var contract in contracts)
            {
                if (contract.IsAssignableFrom(implementation))
                {
                    continue;
                }

                var contractName = ReflectionUtils.GetName(contract);
                var resultName   = ReflectionUtils.GetName <TResult>();

                throw Error.InvalidOperation($"Type {resultName} is not assignable from {contractName}");
            }

            var resolver = new DelegateResolver <TResult>(builder);

            var dependency = Dependency.Build(lifetime, contracts, resolver);

            return(Add(dependency));
        }
Exemplo n.º 6
0
        public IDependency BuildDependency(Type contract, IDependencyEngine engine)
        {
            var implementation = _genericImplementation == null
                ? contract
                : _genericImplementation.MakeGenericType(contract.GenericTypeArguments);

            var resolver = DependencyResolver.Build(_lifetime, implementation);

            return(Dependency.Build(_lifetime, new[] { contract }, resolver));
        }
Exemplo n.º 7
0
        public void HasValidLifetime()
        {
            var lifetimes = (DependencyLifetime[])Enum.GetValues(typeof(DependencyLifetime));

            foreach (var lifetime in lifetimes)
            {
                var dependency = Dependency.Build(lifetime, Array.Empty <Type>(), new ActivatorResolver(typeof(Boo)));
                dependency.Lifetime.Should().Be(lifetime);
            }
        }
Exemplo n.º 8
0
        public void ThrowIfBuildWithInvalidLifetime()
        {
            const DependencyLifetime badLifetime = (DependencyLifetime)byte.MaxValue;

            var resolver = new Mock <DependencyResolver>(_implementation).Object;

            Assert
            .Throws <InvalidDataException>(() => Dependency
                                           .Build(badLifetime, new[] { _contract }, resolver));
        }
Exemplo n.º 9
0
        public DependencyCollection AddDependency <TContract>(Func <IDependencyScope, TContract> builder,
                                                              DependencyLifetime lifetime)
            where TContract : class
        {
            var contracts = new[] { Typeof <TContract> .Raw };
            var resolver  = new DelegateResolver <TContract>(builder);

            var dependency = Dependency.Build(lifetime, contracts, resolver);

            return(AddDependency(dependency));
        }
Exemplo n.º 10
0
        public DependencyCollection AddDependency(
            Type[] contracts,
            Func <IServiceProvider, object> builder,
            DependencyLifetime lifetime, Type implementation)
        {
            var resolver = new DelegateResolver(implementation, builder);

            var dependency = Dependency.Build(lifetime, contracts, resolver);

            return(Add(dependency));
        }
Exemplo n.º 11
0
        public IDependency BuildDependency(Type contract, IDependencyEngine engine)
        {
            var contracts = new[] { contract };

            if (_nullServicePredicate != null && _nullServicePredicate(engine))
            {
                return(new InstanceDependency(contracts, Activator.CreateInstance(_nullService)));
            }

            var lifetime = _lifetime ?? engine.DefineLifetime(_implementation);
            var resolver = DependencyResolver.Build(lifetime, _implementation);

            return(Dependency.Build(lifetime, contracts, resolver));
        }
Exemplo n.º 12
0
        public IDependency BuildDependency(Type contract, IDependencyEngine engine)
        {
            var writers = engine.GetApplicable(typeof(ILogWriter));

            var contracts = new[] { _providerType };

            if (writers.Length == 0)
            {
                return(new InstanceDependency(contracts, new NullProvider()));
            }

            var resolver = new CompiledResolver(typeof(LogProvider), engine);

            return(Dependency.Build(DependencyLifetime.Scoped, contracts, resolver));
        }
Exemplo n.º 13
0
        public IDependency BuildDependency(Type contract, IDependencyEngine engine)
        {
            var constructor = ReflectionUtils.GetConstructor(contract);
            var parameters  = constructor.GetParameters();

            var dependencies = new LocalList <IDependency>();

            foreach (var parameter in parameters)
            {
                var required   = !parameter.HasDefaultValue;
                var dependency = engine.GetDependency(parameter.ParameterType, required);
                dependencies.Add(dependency);
            }

            var lifetime = dependencies.DefineLifetime();
            var resolver = DependencyResolver.Build(lifetime, contract, engine);

            return(Dependency.Build(lifetime, new[] { contract }, resolver));
        }
Exemplo n.º 14
0
        public void Build(DependencyLifetime lifetime)
        {
            var resolver = MockResolver(_implementation, (_, scope) => null).Object;

            var dependency = Dependency.Build(lifetime, new[] { _contract }, resolver);

            switch (lifetime)
            {
            case DependencyLifetime.Scoped:
                dependency.Should().BeOfType <ScopedDependency>();
                break;

            case DependencyLifetime.Singleton:
                dependency.Should().BeOfType <SingletonDependency>();
                break;

            case DependencyLifetime.Transient:
                dependency.Should().BeOfType <TransientDependency>();
                break;
            }
        }
Exemplo n.º 15
0
        public IDependency BuildDependency(Type contract, IDependencyEngine engine)
        {
            var systemType   = contract.GenericTypeArguments[0];
            var dependencies = engine.GetApplicable(systemType);

            var contracts = new[] { contract };

            if (dependencies.Length == 0)
            {
                var nullHandlerType = typeof(SystemNullPipeline <>).MakeGenericType(systemType);
                var nullHandler     = Activator.CreateInstance(nullHandlerType);
                return(new InstanceDependency(contracts, nullHandler));
            }

            var handlerType = DefineHandlerType(dependencies);

            var implementation = handlerType.MakeGenericType(systemType);
            var lifetime       = dependencies.DefineLifetime();
            var resolver       = DependencyResolver.Build(lifetime, implementation);

            return(Dependency.Build(lifetime, contracts, resolver));
        }
Exemplo n.º 16
0
        public IDependency BuildDependency(Type contract, IDependencyEngine engine)
        {
            var elementType = contract.IsArray
                ? ReflectionUtils.GetArrayElementType(contract)
                : contract.GenericTypeArguments[0];

            var contracts    = new[] { elementType.MakeArrayType(), _enumerableType.MakeGenericType(elementType) };
            var dependencies = engine.GetApplicable(elementType);

            if (dependencies.Length == 0)
            {
                var emptyDependencyType = typeof(EmptyArrayDependency <>).MakeGenericType(elementType);
                return((IDependency)Activator.CreateInstance(emptyDependencyType));
            }

            var resolverType       = typeof(ArrayResolver <>).MakeGenericType(elementType);
            var resolverParameters = new object[] { dependencies };
            var resolver           = (DependencyResolver)Activator.CreateInstance(resolverType, resolverParameters);

            var lifetime = dependencies.DefineLifetime();

            return(Dependency.Build(lifetime, contracts, resolver));
        }