Ejemplo n.º 1
0
        public void Register(string key, Func <IUnitOfWork> instanceCreator, Lifestyle lifestyle = null)
        {
            lifestyle = lifestyle ?? Lifestyle.Transient;
            var producer = lifestyle.CreateProducer(instanceCreator, _container);

            _producers.Add(key, producer);
        }
Ejemplo n.º 2
0
        public void Register(TKey key, Func <TService> instanceCreator, Lifestyle lifestyle)
        {
            Condition.Requires(instanceCreator, nameof(instanceCreator)).IsNotNull();
            Condition.Requires(lifestyle, nameof(lifestyle)).IsNotNull();
            var producer = lifestyle.CreateProducer(instanceCreator, container);

            this.producers.Add(key, producer);
        }
Ejemplo n.º 3
0
        public void Register <TImplementation>(TKey key, Lifestyle lifestyle)
            where TImplementation : class, TService
        {
            Condition.Requires(lifestyle, nameof(lifestyle)).IsNotNull();
            var producer = lifestyle.CreateProducer <TService, TImplementation>(container);

            this.producers.Add(key, producer);
        }
Ejemplo n.º 4
0
    public void Register <TImplementation>(string name, Lifestyle lifestyle = null)
        where TImplementation : class, Basic
    {
        lifestyle = lifestyle ?? Lifestyle.Transient;
        var producer = lifestyle.CreateProducer <Basic, TImplementation>(container);

        this.producers.Add(name, producer);
    }
Ejemplo n.º 5
0
        public static void RegisterInstanceCreator <TService, TImpl>(
            this Container container, Lifestyle lifestyle = null)
            where TService : class
            where TImpl : class, TService
        {
            lifestyle = lifestyle ?? container.Options.DefaultLifestyle;
            var producer = lifestyle.CreateProducer <TService, TImpl>(container);

            container.RegisterInstance <Func <TService> >(producer.GetInstance);
        }
        private InstanceProducer CreateNewExternalProducer(Type implementationType)
        {
            if (!Types.IsConcreteConstructableType(implementationType))
            {
                // This method will throw an (expressive) exception since implementationType is not concrete.
                this.container.GetRegistration(implementationType, throwOnFailure: true);
            }

            Lifestyle lifestyle = this.container.SelectionBasedLifestyle;

            // This producer will be automatically registered as external producer.
            return(lifestyle.CreateProducer(typeof(TService), implementationType, this.container));
        }
Ejemplo n.º 7
0
        public void CreateProducerTService_SuppliedWithOpenGenericImplementationType_ThrowsExpectedException()
        {
            // Arrange
            Container container = new Container();
            Lifestyle lifestyle = Lifestyle.Transient;

            // Act
            Action action = () => lifestyle.CreateProducer <object>(typeof(NullCommandHandler <>), container);

            // Assert
            AssertThat.ThrowsWithExceptionMessageContains <ArgumentException>(
                "The supplied type NullCommandHandler<T> is an open-generic type.",
                action);
            AssertThat.ThrowsWithParamName("implementationType", action);
        }
Ejemplo n.º 8
0
        private InstanceProducer CreateNewExternalProducer(ContainerControlledItem item)
        {
            if (!Types.IsConcreteConstructableType(item.ImplementationType))
            {
                throw new ActivationException(
                          StringResources.UnregisteredAbstractionFoundInCollection(
                              serviceType: typeof(TService),
                              registeredType: item.RegisteredImplementationType,
                              foundAbstractType: item.ImplementationType));
            }

            Lifestyle lifestyle = this.container.SelectionBasedLifestyle;

            // This producer will be automatically registered as external producer.
            return(lifestyle.CreateProducer(typeof(TService), item.ImplementationType, this.container));
        }
Ejemplo n.º 9
0
        public void CreateProducer_SuppliedWithOpenGenericServiceType_ThrowsExpectedException()
        {
            // Arrange
            Container container = new Container();
            Lifestyle lifestyle = Lifestyle.Transient;

            // Act
            Action action = () => lifestyle.CreateProducer(typeof(ICommandHandler <>),
                                                           typeof(StubCommandHandler), container);

            // Assert
            AssertThat.ThrowsWithExceptionMessageContains <ArgumentException>(
                "The supplied type ICommandHandler<TCommand> is an open generic type.",
                action);
            AssertThat.ThrowsWithParamName("serviceType", action);
        }
        public static void InterceptWith <TInterceptor>(
            this Container container,
            Lifestyle lifestyle,
            Func <Type, bool> predicate)
            where TInterceptor : class, IInterceptor
        {
            lifestyle = lifestyle ?? container.Options.LifestyleSelectionBehavior
                        .SelectLifestyle(typeof(TInterceptor));

            var producer =
                lifestyle.CreateProducer <TInterceptor, TInterceptor>(container);

            var interceptWith = new InterceptionHelper
            {
                BuildInterceptorExpression = e => producer.BuildExpression(),
                Predicate = type => predicate(type)
            };

            container.ExpressionBuilt += interceptWith.OnExpressionBuilt;
        }
Ejemplo n.º 11
0
 internal static void RegisterFallbackType <TService, TImplementation>(this Container container, Lifestyle lifestyle) where TService : class where TImplementation : class, TService =>
 container.RegisterFallback <TService>(a => a.Register(lifestyle.CreateProducer <TService, TImplementation>(container).Registration));