/// <summary>
        /// Resolves a dependency.
        /// </summary>
        /// <param name="typeToResolve">The type to be resolved.</param>
        /// <param name="typeToCreate">The type to be created, if the type cannot be resolved
        /// (and notPresent is set to <see cref="NotPresentBehavior.CreateNew"/>).</param>
        /// <param name="id">The ID of the object to be resolved. Pass null for the unnamed object.</param>
        /// <param name="notPresent">Flag to describe how to behave if the dependency is not found.</param>
        /// <param name="searchMode">Flag to describe whether searches are local only, or local and up.</param>
        /// <returns>The dependent object. If the object is not found, and notPresent
        /// is set to <see cref="NotPresentBehavior.ReturnNull"/>, will return null.</returns>
        public object Resolve(Type typeToResolve, Type typeToCreate, string id, NotPresentBehavior notPresent, SearchMode searchMode)
        {
            if (typeToResolve == null)
                throw new ArgumentNullException("typeToResolve");
            if (!Enum.IsDefined(typeof(NotPresentBehavior), notPresent))
                throw new ArgumentException(Properties.Resources.InvalidEnumerationValue, "notPresent");

            if (typeToCreate == null)
                typeToCreate = typeToResolve;

            DependencyResolutionLocatorKey key = new DependencyResolutionLocatorKey(typeToResolve, id);

            if (context.Locator.Contains(key, searchMode))
                return context.Locator.Get(key, searchMode);

            switch (notPresent)
            {
                case NotPresentBehavior.CreateNew:
                    return context.HeadOfChain.BuildUp(context, typeToCreate, null, key.ID);

                case NotPresentBehavior.ReturnNull:
                    return null;

                default:
                    throw new DependencyMissingException(
                        string.Format(CultureInfo.CurrentCulture,
                        Properties.Resources.DependencyMissing, typeToResolve.ToString()));
            }
        }
Ejemplo n.º 2
0
        public static object Resolve(IBuilderContext context,
                                     object buildKey,
                                     NotPresentBehavior behavior)
        {
            Guard.ArgumentNotNull(context, "context");
            Guard.ArgumentNotNull(buildKey, "buildKey");

            if (context.Locator.Contains(buildKey))
            {
                return(context.Locator.Get(buildKey));
            }

            switch (behavior)
            {
            case NotPresentBehavior.Build:
                return(context.HeadOfChain.BuildUp(context, buildKey, null));

            case NotPresentBehavior.Null:
                return(null);

            case NotPresentBehavior.Throw:
                throw new DependencyMissingException(buildKey);

            default:
                throw new ArgumentException("Unknown NotPresentBehavior " + behavior);
            }
        }
Ejemplo n.º 3
0
        public static object Resolve(IBuilderContext context,
                                     object buildKey,
                                     NotPresentBehavior behavior)
        {
            Guard.ArgumentNotNull(context, "context");
            Guard.ArgumentNotNull(buildKey, "buildKey");

            if (context.Locator.Contains(buildKey))
            {
                return(context.Locator.Get(buildKey));
            }

            switch (behavior)
            {
            case NotPresentBehavior.Build:
                IBuilderContext recursiveContext = context.CloneForNewBuild(buildKey, null);
                return(recursiveContext.Strategies.ExecuteBuildUp(recursiveContext));

            case NotPresentBehavior.Null:
                return(null);

            case NotPresentBehavior.Throw:
                throw new DependencyMissingException(buildKey);

            default:
                throw new ArgumentException(
                          string.Format(CultureInfo.CurrentCulture,
                                        Resources.UnknownNotPresentBehavior,
                                        behavior));
            }
        }
Ejemplo n.º 4
0
 public DependencyParameter(Type parameterType, string name, Type createType, NotPresentBehavior notPresentBehavior, SearchMode searchMode) : base(parameterType)
 {
     this.name = name;
     this.createType = createType;
     this.notPresentBehavior = notPresentBehavior;
     this.searchMode = searchMode;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DependencyParameter"/> class using the
 /// provided parameter type, name, creation type, not present behavior, and search mode.
 /// </summary>
 public DependencyParameter(Type parameterType, string name,
                            Type createType, NotPresentBehavior notPresentBehavior, SearchMode searchMode)
     : base(parameterType)
 {
     this.name               = name;
     this.createType         = createType;
     this.notPresentBehavior = notPresentBehavior;
     this.searchMode         = searchMode;
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Resolves a dependency.
        /// </summary>
        /// <param name="typeToResolve">The type to be resolved.</param>
        /// <param name="typeToCreate">The type to be created, if the type cannot be resolved
        /// (and notPresent is set to <see cref="NotPresentBehavior.CreateNew"/>).</param>
        /// <param name="id">The ID of the object to be resolved. Pass null for the unnamed object.</param>
        /// <param name="notPresent">Flag to describe how to behave if the dependency is not found.</param>
        /// <param name="searchMode">Flag to describe whether searches are local only, or local and up.</param>
        /// <returns>The dependent object. If the object is not found, and notPresent
        /// is set to <see cref="NotPresentBehavior.ReturnNull"/>, will return null.</returns>
        public object Resolve(Type typeToResolve, Type typeToCreate, string id, NotPresentBehavior notPresent, SearchMode searchMode)
        {
            if (typeToResolve == null)
            {
                throw new ArgumentNullException("typeToResolve");
            }
            if (!Enum.IsDefined(typeof(NotPresentBehavior), notPresent))
            {
                throw new ArgumentException(Properties.Resources.InvalidEnumerationValue, "notPresent");
            }

            if (typeToCreate == null)
            {
                typeToCreate = typeToResolve;
            }

            DependencyResolutionLocatorKey key = new DependencyResolutionLocatorKey(typeToResolve, id);

            if (context.Locator.Contains(key, searchMode))
            {
                return(context.Locator.Get(key, searchMode));
            }

            switch (notPresent)
            {
            case NotPresentBehavior.CreateNew:
                return(context.HeadOfChain.BuildUp(context, typeToCreate, null, key.ID));

            case NotPresentBehavior.ReturnNull:
                return(null);

            default:
                throw new DependencyMissingException(
                          string.Format(CultureInfo.CurrentCulture,
                                        Properties.Resources.DependencyMissing, typeToResolve.ToString()));
            }
        }
Ejemplo n.º 7
0
 public DependencyParameter(object buildKey,
                            NotPresentBehavior notPresentBehavior)
 {
     this.buildKey           = buildKey;
     this.notPresentBehavior = notPresentBehavior;
 }