Esempio n. 1
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));
        }
Esempio n. 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 override Expression CreateDependency(Type interfaceType, ServiceRegister register, ServiceState state, string serviceName, TypeContextAttributes attributes)
 {
     return(Create(interfaceType, register, state, serviceName, attributes) ?? Expression.Default(interfaceType));
 }