Esempio n. 1
0
        /// <summary>
        /// Adds a singleton service to the container.
        /// Service will be created with first call.
        /// </summary>
        public void AddSingleton(Type serviceType, Type implementationType)
        {
            TwinoServiceDescriptor descriptor = new TwinoServiceDescriptor(ImplementationType.Singleton, serviceType, implementationType);

            _items.Add(descriptor);
            _provider = null;
        }
Esempio n. 2
0
        /// <summary>
        /// Adds a singleton service with instance to the container.
        /// </summary>
        public void AddSingleton(Type serviceType, object instance)
        {
            Type implementationType           = instance.GetType();
            TwinoServiceDescriptor descriptor = new TwinoServiceDescriptor(ImplementationType.Singleton, serviceType, implementationType, instance);

            _items.Add(descriptor);
            _provider = null;
        }
Esempio n. 3
0
        /// <summary>
        /// Adds a singleton service to the container.
        /// Service will be created with first call.
        /// </summary>
        public void AddSingleton(Type serviceType, Type implementationType, Delegate afterCreated)
        {
            TwinoServiceDescriptor descriptor = new TwinoServiceDescriptor(ImplementationType.Singleton, serviceType, implementationType)
            {
                AfterCreatedMethod = afterCreated
            };

            _items.Add(descriptor);
            _provider = null;
        }
Esempio n. 4
0
        /// <summary>
        /// Returns current service provider
        /// </summary>
        /// <returns></returns>
        public ITwinoServiceProvider GetProvider()
        {
            if (_provider == null)
            {
                TwinoServiceProvider provider = new TwinoServiceProvider();
                provider.Build(_items);
                _provider = provider;
            }

            return(_provider);
        }
Esempio n. 5
0
        /// <summary>
        /// Adds a service pool to the container
        /// </summary>
        /// <param name="type">Implementation type</param>
        /// <param name="options">Options function</param>
        /// <param name="instance">After each instance is created, to do custom initialization, this method will be called.</param>
        private void AddPool <TService, TImplementation>(ImplementationType type, Action <ServicePoolOptions> options, Action <TService> instance)
            where TService : class
            where TImplementation : class, TService
        {
            ServicePool <TService, TImplementation> pool = new ServicePool <TService, TImplementation>(type, this, options, instance);
            TwinoServiceDescriptor descriptor            = new TwinoServiceDescriptor(ImplementationType.Singleton,
                                                                                      typeof(TService),
                                                                                      typeof(TImplementation),
                                                                                      //typeof(ServicePool<TService, TImplementation>),
                                                                                      pool)
            {
                IsPool = true
            };

            _items.Add(descriptor);
            _provider = null;
        }
Esempio n. 6
0
        /// <summary>
        /// Adds a service to the container
        /// </summary>
        public void AddScoped(Type serviceType, Type implementationType, Type proxyType, Delegate afterCreated)
        {
            if (_items.Any(x => x.ServiceType == serviceType))
            {
                throw new DuplicateTypeException($"{serviceType.ToTypeString()} service type is already added into service container");
            }

            TwinoServiceDescriptor descriptor = new TwinoServiceDescriptor(ImplementationType.Scoped, serviceType, implementationType)
            {
                ProxyType          = proxyType,
                ProxyInstance      = null,
                AfterCreatedMethod = afterCreated
            };

            _items.Add(descriptor);
            _provider = null;
        }
Esempio n. 7
0
        public object CreateInstance(TwinoServiceProvider provider, IContainerScope scope = null)
        {
            if (!IsPool && Instance != null)
            {
                return(Instance);
            }

            object service;

            if (ImplementationFactory != null)
            {
                service = ImplementationFactory(provider);
                ExecuteAfterCreated(service);
                return(service);
            }

            if (Parameters == null || Parameters.Length == 0)
            {
                service = _instanceCreatorFunctionParameterless();
                ExecuteAfterCreated(service);
                return(service);
            }

            if (Parameters.Length == 1)
            {
                BuiltServiceDescriptor descriptor = ParameterDescriptors[0];
                object parameter = provider.GetWithDescriptor(descriptor, scope);
                service = _instanceCreatorFunctionOneParameter(parameter);
                ExecuteAfterCreated(service);
                return(service);
            }

            object[] p = new object[ParameterDescriptors.Count];
            for (int i = 0; i < p.Length; i++)
            {
                BuiltServiceDescriptor descriptor = ParameterDescriptors[i];
                p[i] = provider.GetWithDescriptor(descriptor, scope);
            }

            service = _instanceCreatorFunctionMultipleParameters(p);
            ExecuteAfterCreated(service);
            return(service);
        }
Esempio n. 8
0
 public DefaultContainerScope(TwinoServiceProvider provider)
 {
     _provider = provider;
 }