/// <summary>
        /// Overridden so that TypeIDPair can be used as the key in dictionaries.
        /// </summary>
        public override bool Equals(object obj)
        {
            DependencyResolutionLocatorKey other = obj as DependencyResolutionLocatorKey;

            if (other == null)
            {
                return(false);
            }

            return(Equals(type, other.type) && Equals(id, other.id));
        }
Esempio n. 2
0
        /// <summary>
        /// Implementation of <see cref="IBuilderStrategy.BuildUp"/>.
        /// </summary>
        /// <param name="context">The build context.</param>
        /// <param name="typeToBuild">The type of the object being built.</param>
        /// <param name="existing">The existing instance of the object.</param>
        /// <param name="idToBuild">The ID of the object being built.</param>
        /// <returns>The built object.</returns>
        public override object BuildUp(IBuilderContext context, Type typeToBuild, object existing, string idToBuild)
        {
            DependencyResolutionLocatorKey key = new DependencyResolutionLocatorKey(typeToBuild, idToBuild);

            if (context.Locator != null && context.Locator.Contains(key, SearchMode.Local))
            {
                TraceBuildUp(context, typeToBuild, idToBuild, "");
                return(context.Locator.Get(key));
            }

            return(base.BuildUp(context, typeToBuild, existing, idToBuild));
        }
Esempio n. 3
0
        /// <summary>
        /// Implementation of <see cref="IBuilderStrategy.BuildUp"/>.
        /// </summary>
        public override object BuildUp(IBuilderContext context, Type t, object existing, string id)
        {
            DependencyResolutionLocatorKey result = new DependencyResolutionLocatorKey(t, id);
            ITypeMappingPolicy             policy = context.Policies.Get <ITypeMappingPolicy>(t, id);

            if (policy != null)
            {
                result = policy.Map(result);
                TraceBuildUp(context, t, id, Properties.Resources.TypeMapped, result.Type, result.ID ?? "(null)");
                Guard.TypeIsAssignableFromType(t, result.Type, t);
            }

            return(base.BuildUp(context, result.Type, existing, result.ID));
        }
Esempio n. 4
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()));
            }
        }
Esempio n. 5
0
 /// <summary>
 /// See <see cref="ITypeMappingPolicy.Map"/> for more information.
 /// </summary>
 public DependencyResolutionLocatorKey Map(DependencyResolutionLocatorKey incomingTypeIDPair)
 {
     return(pair);
 }
Esempio n. 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeMappingPolicy"/> class using
 /// the provided type and ID.
 /// </summary>
 /// <param name="type">The new type to be returned during Map.</param>
 /// <param name="id">The new ID to be returned during Map.</param>
 public TypeMappingPolicy(Type type, string id)
 {
     pair = new DependencyResolutionLocatorKey(type, id);
 }