예제 #1
0
        public object Resolve(Type wantedType, object[] parameters)
        {
            var data = new IoC.ResolveData(wantedType, parameters);

            var type = FindImplementationType(data);

            data.ImplementationType = type;
            var parametersInfo     = FindImplementationTypeConstructorParameters(wantedType, type);
            var resolvedParameters = ResolveParameters(data, parametersInfo);

            return(resolvedParameters.Any() ? Activator.CreateInstance(type, resolvedParameters) : Activator.CreateInstance(type));
        }
예제 #2
0
        protected object[] ResolveParameters(IoC.ResolveData data, ParameterInfo[] parametersInfo)
        {
            var result = GetParameters(data, parametersInfo);

            for (var i = 0; i < parametersInfo.Length; i++)
            {
                if (result[i] != null)
                {
                    continue;
                }

                var parameterInfo = parametersInfo[i];

                //custom rules
                var possibleRegisteredParameters = IoC.RegisteredParameters.Where(x => string.Equals(x.ParameterName, parameterInfo.Name, StringComparison.OrdinalIgnoreCase) && (parameterInfo.ParameterType == x.ParameterType || parameterInfo.ParameterType.IsInstanceOfType(x.ParameterType))).ToArray();
                if (possibleRegisteredParameters.Length > 1)
                {
                    throw new TooManyRegisteredParameterTypesException(parameterInfo.ParameterType);
                }

                var possibleRegisteredParameter = possibleRegisteredParameters.SingleOrDefault();
                if (possibleRegisteredParameter != null)
                {
                    result[i] = possibleRegisteredParameter.GetParameterValue();
                    continue;
                }

                if (parameterInfo.ParameterType.IsSimpleType())
                {
                    result[i] = null;
                    continue;
                }

                try
                {
                    //default action
                    result[i] = IoC.Resolve(this, parameterInfo.ParameterType, data.Parameters.ToArray());
                }
                catch
                {
                    if (data.WantedType.IsInterface && !parameterInfo.ParameterType.IsInterface)
                    {
                        throw new MissingParameterException(FindImplementationType(data), parameterInfo.ParameterType);
                    }

                    throw;
                }
            }

            return(result);
        }
예제 #3
0
        protected object[] GetParameters(IoC.ResolveData data, ParameterInfo[] parametersInfo)
        {
            var result = new object[parametersInfo.Length];

            IDictionary <string, object> parameterDict = null;

            if (data.Parameters.Count == 1 && data.Parameters[0] != null)
            {
                if (data.Parameters[0] is IDictionary <string, object> || data.Parameters[0].GetType().IsAnonymousType())
                {
                    parameterDict = data.Parameters[0].AsLowerDictionary();
                }
            }

            for (var i = 0; i < parametersInfo.Length; i++)
            {
                var parameterInfo = parametersInfo[i];

                if (parameterDict != null && parameterDict.ContainsKey(parameterInfo.Name.ToLower()))
                {
                    result[i] = parameterDict[parameterInfo.Name.ToLower()];
                    continue;
                }

                //user rules
                var possibleParameters = data.Parameters.Where(x => parameterInfo.ParameterType == x.GetType() || parameterInfo.ParameterType.IsInstanceOfType(x)).ToArray();
                if (possibleParameters.Length > 1)
                {
                    throw new TooManyParameterTypesException(parameterInfo.ParameterType);
                }

                var possibleParameter = possibleParameters.SingleOrDefault();
                if (possibleParameter != null)
                {
                    result[i] = possibleParameter;
                    continue;
                }

                result[i] = null;
            }

            return(result);
        }
예제 #4
0
        private Type ResolveImplementationType(IoC.ResolveData data, Type[] implementationTypes)
        {
            var providerAttr = data.WantedType.GetCustomAttribute <SupplierAttribute>() ?? data.WantedType.GetCustomAttribute <SupplierAttribute>(true);

            if (providerAttr == null)
            {
                providerAttr = implementationTypes.Select(x => x.GetCustomAttribute <SupplierAttribute>() ?? x.GetCustomAttribute <SupplierAttribute>(true)).FirstOrDefault(x => x != null);
            }

            if (providerAttr == null)
            {
                throw new TypeResolvingFailedException(data.WantedType, "No SupplierAttribute found on interface or implementation types");
            }

            var supplierName = GetSupplier(providerAttr.Name) ?? DefaultSupplierName;

            var implementationTypesWithSuppliers = implementationTypes.Select(x =>
            {
                return(new
                {
                    Type = x,
                    Suppliers = x.GetCustomAttributes <SupplierAttribute>(true).Where(y => y.Name != null).Select(y => y.Name.ToUpper()).ToArray()
                });
            }).ToArray();

            Type type = null;

            if (!string.IsNullOrEmpty(supplierName))
            {
                type = implementationTypesWithSuppliers.Where(x => x.Suppliers.Contains(supplierName.ToUpper())).Select(x => x.Type).SingleOrDefault();
            }

            if (type == null)
            {
                var possibleLinks = implementationTypesWithSuppliers.SelectMany(x => x.Suppliers).OrderBy(x => x).ToArray();

                var msg = string.IsNullOrEmpty(supplierName) ? string.Format("No supplier configured for provider '{0}'.", providerAttr.Name) : string.Format("Supplier value '{0}' for provider '{1}' is invalid.", supplierName, providerAttr.Name);
                throw new InvalidOperationException(string.Format("{0}\nPossible suppliers: {1}", msg, possibleLinks.Join(", ")));
            }

            return(type);
        }
예제 #5
0
 protected override Type FindImplementationType(IoC.ResolveData data)
 {
     return(IoC.FindImplementationType(data, validateImplementationTypes: ValidateImplementationTypes, resolveImplementationType: ResolveImplementationType));
 }
예제 #6
0
 protected virtual Type FindImplementationType(IoC.ResolveData data)
 {
     return(IoC.FindImplementationType(data));
 }
예제 #7
0
        private Type ResolveImplementationType(IoC.ResolveData data, Type[] implementationTypes)
        {
            var isDatabaseService = typeof(IBaseDatabaseService).IsAssignableFrom(data.WantedType);

            if (!isDatabaseService)
            {
                return(null);
            }

            //var niscode = IoC.FindNiscodeParameter(data) ?? (_getUserNiscode != null ? _getUserNiscode() : null);

            var connectionStringAttr = data.WantedType.GetCustomAttribute <ConnectionStringNameAttribute>() ?? data.WantedType.GetCustomAttribute <ConnectionStringNameAttribute>(true);

            if (connectionStringAttr == null)
            {
                connectionStringAttr = implementationTypes.Select(x => x.GetCustomAttribute <ConnectionStringNameAttribute>() ?? x.GetCustomAttribute <ConnectionStringNameAttribute>(true)).FirstOrDefault(x => x != null);
            }

            var connectionStringName = connectionStringAttr != null ? connectionStringAttr.Name : _defaultConnectionStringName;

            var supplierName = GetSupplier(connectionStringName);

            var implementationTypesWithSuppliers = implementationTypes.Select(x =>
            {
                return(new
                {
                    Type = x,
                    Suppliers = x.GetCustomAttributes <SupplierAttribute>(true).Where(y => y.Name != null).Select(y => y.Name.ToUpper()).ToArray()
                });
            }).ToArray();

            //met geen of "GEOIT" connectionstring is er geen link en geen supplier nodig
            Type type;

            if (string.IsNullOrEmpty(supplierName))
            {
                if (string.Equals(connectionStringName, _defaultConnectionStringName, StringComparison.InvariantCultureIgnoreCase))
                {
                    if (implementationTypes.Length > 1)
                    {
                        throw new TypeResolvingFailedException(data.WantedType, "No ConnectionStringNameAttribute found on interface or implementation types");
                    }

                    type = implementationTypes.SingleOrDefault();
                }
                else if (implementationTypesWithSuppliers.Any(x => x.Suppliers.Contains(DefaultSupplierName)))
                {
                    //als er geen link is bepaald, dan GEOIT nemen indien er zo'n implementatie voor bestaat
                    type = implementationTypesWithSuppliers.First(x => x.Suppliers.Contains(DefaultSupplierName)).Type;
                }
                else
                {
                    type = null;
                }
            }
            else
            {
                type = implementationTypesWithSuppliers.Where(x => x.Suppliers.Contains(supplierName.ToUpper())).Select(x => x.Type).SingleOrDefault();
            }

            if (type == null)
            {
                var possibleLinks = implementationTypesWithSuppliers.SelectMany(x => x.Suppliers).OrderBy(x => x).ToArray();

                var msg = string.IsNullOrEmpty(supplierName) ? string.Format("No supplier configured for connectionstring '{0}'.", connectionStringName) : string.Format("Supplier value '{0}' for connectionstring '{1}' is invalid.", supplierName, connectionStringName);
                throw new InvalidOperationException(string.Format("{0}\nPossible suppliers: {1}", msg, possibleLinks.Join(", ")));
            }

            var hasConnectionStringParameter = data.HasParameter <ConnectionStringParameter>();
            var hasProviderParameter         = data.HasParameter <ProviderParameter>();

            if (!hasConnectionStringParameter || !hasProviderParameter)
            {
                var conn = _getConnectionStringSettings(connectionStringName);

                //als er geen connectiestring is geconfigureerd voor de gewenste, neem dan die van GEOIT
                if (conn == null && _defaultConnectionStringName != null)
                {
                    conn = _getConnectionStringSettings(_defaultConnectionStringName);
                }

                if (conn == null)
                {
                    throw new TypeResolvingFailedException(data.WantedType, string.Format("No ConnectionString found for '{0}'", connectionStringName));
                }

                if (!hasConnectionStringParameter)
                {
                    data.Parameters.Add(new ConnectionStringParameter(conn.ConnectionString));
                }

                if (!hasProviderParameter)
                {
                    data.Parameters.Add(new ProviderParameter(conn.ProviderName));
                }
            }

            //if(!string.IsNullOrEmpty(niscode))
            //{
            //    if(!data.HasParameter<NiscodeParameter>())
            //    {
            //        data.Parameters.Add(new NiscodeParameter(niscode));
            //    }
            //}

            return(type);
        }