示例#1
0
        private object ResolveInternal(TypeRegistration registration, ParametersOverloads parameters, IoCOptions options)
        {
            if (options == null)
            {
                options = Options;
            }

            object resolved;

            // Attempt container resolution
            if (TryResolve(out resolved, registration, parameters, options))
            {
                return(resolved);
            }

            // Attempt container resolution
            if (!string.IsNullOrEmpty(registration.Name))
            {
                // Fail if requesting named resolution and settings set to fail if unresolved
                if (options.NamedResolutionFailureAction == NamedResolutionFailureAction.Fail)
                {
                    IoCResolutionException.Raise(registration.Type);
                }

                // Attemped unnamed fallback container resolution if relevant and requested
                if (options.NamedResolutionFailureAction == NamedResolutionFailureAction.AttemptUnnamedResolution)
                {
                    if (TryResolve(out resolved, new TypeRegistration(registration.Type), parameters, options))
                    {
                        return(resolved);
                    }
                }
            }

#if EXPRESSIONS
            // Attempt to construct an automatic lazy factory if possible
            if (IsAutomaticLazyFactoryRequest(registration.Type))
            {
                return(GetLazyAutomaticFactoryRequest(registration.Type));
            }
#endif
#if ENUMERABLE_REQUESTS
            if (IsIEnumerableRequest(registration.Type))
            {
                return(GetIEnumerableRequest(registration.Type));
            }
#endif
            // Attempt unregistered construction if possible and requested
            if ((options.UnregisteredResolutionAction == UnregisteredResolutionAction.AttemptResolve)
                /*|| (registration.Type.IsGenericType() && options.UnregisteredResolutionAction == UnregisteredResolutionAction.GenericsOnly*/)
            {
                if (!registration.Type.IsAbstract() && !registration.Type.IsInterface())
                {
                    return(ConstructType(registration.Type, registration.Type, null, parameters));
                }
            }

            // Unable to resolve - throw
            return(IoCResolutionException.Raise(registration.Type));
        }
示例#2
0
        void RegisterInternal(string regisrationName, Type registerType, Type implementationType, IoCFactory factory)
        {
            if (registerType == null || implementationType == null || implementationType.IsAbstract() || implementationType.IsInterface())
            {
                IoCRegistrationException.Raise(registerType, implementationType);
            }

            if (!IsValidAssignment(registerType, implementationType))
            {
                IoCRegistrationException.Raise(registerType, implementationType);
            }

            var typeRegistration = new TypeRegistration(registerType, regisrationName);

            registry[typeRegistration] = factory;
        }
示例#3
0
        private bool TryResolve(out object resolved, TypeRegistration registration, ParametersOverloads parameters, IoCOptions options)
        {
            for (var container = this; container != null; container = container.Parent)
            {
                IoCFactory factory;
                if (registry.TryGetValue(registration, out factory))
                {
                    try {
                        resolved = factory.GetObject(registration.Type, this, parameters);
                        return(true);
                    }
                    catch (IoCResolutionException) {
                        throw;
                    }
                    catch (Exception ex) {
                        IoCResolutionException.Raise(registration.Type, ex);
                    }
                }

#if RESOLVE_OPEN_GENERICS
                // Attempt container resolution of open generic
                if (registration.Type.IsGenericType())
                {
                    var openTypeRegistration = new TypeRegistration(registration.Type.GetGenericTypeDefinition(),
                                                                    registration.Name);

                    if (_RegisteredTypes.TryGetValue(openTypeRegistration, out factory))
                    {
                        try {
                            return(factory.GetObject(registration.Type, this, parameters, options));
                        }
                        catch (TinyIoCResolutionException) {
                            throw;
                        }
                        catch (Exception ex) {
                            IoCResolutionException.Raise(registration.Type, ex);
                        }
                    }
                }
#endif
            }

            resolved = null;
            return(false);
        }