Пример #1
0
        private Expression GetObjectFromDependencyFactory(Type interfaceType, string factoryName, ServiceRegister registrar, ServiceState state, string mappedServiceName, TypeContextAttributes attributes)
        {
            var subcontractFactory = ServiceProviderHelper.GetDependencyFactory(DependencyFactory, factoryName);

            return(subcontractFactory?.Create(interfaceType, registrar, state, mappedServiceName, attributes) ?? Expression.Default(interfaceType));
        }
Пример #2
0
 /// <summary>
 /// Creates dependency <see cref="Expression"/> object. if no dependency factory is set and shared factory exists then
 /// creates dependency from the shared factory; otherwise creates from the dependency factory.
 /// </summary>
 /// <param name="interfaceType">The type of the contract.</param>
 /// <param name="register">The service register object.</param>
 /// <param name="state">The service state.</param>
 /// <param name="serviceName"></param>
 /// <param name="attributes"></param>
 /// <returns>Dependency expression</returns>
 protected virtual Expression CreateDependency(Type interfaceType, ServiceRegister register, ServiceState state, string serviceName, TypeContextAttributes attributes)
 {
     //let's subcontract take responsibility to create dependency objects
     if (DependencyFactory == null && SharedFactory != null)
     {
         return(SharedFactory.Create(interfaceType, register, state, serviceName, attributes) ?? Expression.Default(interfaceType));
     }
     return(DependencyFactory?.Create(interfaceType, register, state, serviceName, attributes) ?? Expression.Default(interfaceType));
 }
Пример #3
0
 /// <summary>
 /// Create <see cref="Expression"/> for service which is associated with contract.
 /// </summary>
 /// <param name="interfaceType">Type of contract</param>
 /// <param name="register">The ServiceRegister</param>
 /// <param name="state">The ServiceState</param>
 /// <param name="serviceName"></param>
 /// <param name="attributes"></param>
 /// <returns>Returns <see cref="Expression"/> of service constructor.</returns>
 public abstract Expression Create(Type interfaceType, ServiceRegister register, ServiceState state, string serviceName, TypeContextAttributes attributes);
        /// <summary>
        /// Create <see cref="Expression"/> for service which is associated with contract.
        /// </summary>
        /// <param name="interfaceType">Type of contract</param>
        /// <param name="register">The ServiceRegister</param>
        /// <param name="state">The ServiceState</param>
        /// <param name="serviceName"></param>
        /// <param name="attributes"></param>
        /// <returns>Returns <see cref="Expression"/> of service constructor.</returns>
        public override Expression Create(Type interfaceType, ServiceRegister register, ServiceState state, string serviceName, TypeContextAttributes attributes)
        {
            if (interfaceType == null)
            {
                throw new ArgumentNullException(nameof(interfaceType));
            }

            //Register current subcontract service with ServiceRegistrar
            //Root service will refresh with the updated service when there's replacement with new service
            register.Register(interfaceType);

            BaseServiceInfo serviceMeta = GetServiceInfo(interfaceType, serviceName);

            if (serviceMeta == null)
            {
                return(SharedFactory.Create(interfaceType, register, state, serviceName, attributes));
            }

            var userdefinedExpression = serviceMeta.GetServiceInstanceExpression();

            if (userdefinedExpression != null)
            {
                return(Expression.Invoke(userdefinedExpression));
            }

            return(CreateConstructorExpression(interfaceType, serviceMeta.ServiceType, register, state) ?? Expression.Default(interfaceType));
        }
Пример #5
0
        /// <summary>
        /// Create <see cref="Expression"/> for service which is associated with contract.
        /// </summary>
        /// <param name="interfaceType">Type of contract</param>
        /// <param name="register">The ServiceRegister</param>
        /// <param name="state">The ServiceState</param>
        /// <param name="serviceName"></param>
        /// <param name="attributes"></param>
        /// <returns>Returns <see cref="Expression"/> of service constructor.</returns>
        public override Expression Create(Type interfaceType, ServiceRegister register, ServiceState state, string serviceName, TypeContextAttributes attributes)
        {
            if (interfaceType == null)
            {
                throw new ArgumentNullException(nameof(interfaceType));
            }

            //Register current subcontract service with ServiceRegistrar
            //Root service will refresh with the updated service when there's replacement with new service
            register.Register(interfaceType);

            BaseServiceInfo serviceMeta = GetServiceInfo(interfaceType, serviceName);

            //Throw exception if service meta not found in the service map table
            if (serviceMeta == null)
            {
                if (attributes.HasAttribute(typeof(OptionalAttribute)))
                {
                    return(Expression.Default(interfaceType));
                }
                else
                {
                    ExceptionHelper.ThrowContractNotRegisteredException($"Type '{interfaceType.FullName}' was not registered with dependency container or shared container.");
                }
            }

            var userdefinedExpression = serviceMeta.GetServiceInstanceExpression();

            if (userdefinedExpression != null)
            {
                return(Expression.Invoke(userdefinedExpression));
            }

            return(CreateConstructorExpression(interfaceType, serviceMeta.ServiceType, register, state) ?? Expression.Default(interfaceType));
        }
Пример #6
0
 /// <summary>
 /// Creates dependency <see cref="Expression"/> object. if no dependency factory is set and shared factory exists then
 /// creates dependency from the shared factory; otherwise creates from the dependency factory.
 /// </summary>
 /// <param name="interfaceType">The type of the contract.</param>
 /// <param name="register">The service register object.</param>
 /// <param name="state">The service state.</param>
 /// <param name="serviceName"></param>
 /// <param name="attributes"></param>
 /// <returns>Dependency expression</returns>
 protected override Expression CreateDependency(Type interfaceType, ServiceRegister register, ServiceState state, string serviceName, TypeContextAttributes attributes)
 {
     return(Create(interfaceType, register, state, serviceName, attributes) ?? Expression.Default(interfaceType));
 }