/// <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())); } }
public void CanRegisterObjectByTypeAndID() { object o = new object(); IReadWriteLocator locator = new Locator(); DependencyResolutionLocatorKey key = new DependencyResolutionLocatorKey(typeof(object), "foo"); locator.Add(key, o); Assert.IsNotNull(locator.Get(key)); Assert.AreSame(o, locator.Get(key)); }
/// <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)); }
/// <summary> /// Maps one type/ID pair to another, using the locator to find the /// current <see cref="CompositionContainer"/>. The container holds /// the current set of type mappings. /// </summary> /// <param name="locator">The <see cref="IReadableLocator"/> being used in the /// current BuildUp operation.</param> /// <param name="incomingTypeIdPair"><see cref="DependencyResolutionLocatorKey"/> that identifies /// the type of the current BuildUp operation.</param> /// <returns>The new type to return; returns the original key if no type mapping is defined.</returns> public DependencyResolutionLocatorKey Map( IReadableLocator locator, DependencyResolutionLocatorKey incomingTypeIdPair) { DependencyResolutionLocatorKey result = incomingTypeIdPair; CompositionContainer container = GetCompositionContainerFromLocator(locator); if (container != null) { result = new DependencyResolutionLocatorKey(container.GetMappedType(result.Type), incomingTypeIdPair.ID); } return result; }
/// <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); }
/// <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)); }
public override object BuildUp(IBuilderContext context, Type t, object existing, string id) { DependencyResolutionLocatorKey incomingTypeIDPair = new DependencyResolutionLocatorKey(t, id); ITypeMappingPolicy policy = context.Policies.Get<ITypeMappingPolicy>(t, id); if (policy != null) { incomingTypeIDPair = policy.Map(incomingTypeIDPair); object[] args = new object[] { incomingTypeIDPair.Type, incomingTypeIDPair.ID ?? "(null)" }; base.TraceBuildUp(context, t, id, Resources.TypeMapped, args); Guard.TypeIsAssignableFromType(t, incomingTypeIDPair.Type, t); } return base.BuildUp(context, incomingTypeIDPair.Type, existing, incomingTypeIDPair.ID); }
/// <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); }
/// <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)); }
public void PolicyShouldRequestTypeMappingFromContainer() { TestableRootCompositionContainer container = new TestableRootCompositionContainer(); container.RegisterTypeMapping<IFoo, Foo>(); ContainerAwareTypeMappingPolicy policy = new ContainerAwareTypeMappingPolicy(); string requestId = "My mapped object"; DependencyResolutionLocatorKey requestKey = new DependencyResolutionLocatorKey(typeof (IFoo), requestId); DependencyResolutionLocatorKey result = policy.Map(container.Locator, requestKey); Assert.AreEqual(typeof (Foo), result.Type); Assert.AreEqual(requestId, result.ID); }
public void PolicyCallsUpToParentContainerToGetMapping() { TestableRootCompositionContainer container = new TestableRootCompositionContainer(); container.RegisterTypeMapping<IFoo, Foo>(); CompositionContainer child = container.Containers.AddNew<CompositionContainer>(); ContainerAwareTypeMappingPolicy policy = new ContainerAwareTypeMappingPolicy(); string requestId = "My mapped object"; DependencyResolutionLocatorKey requestKey = new DependencyResolutionLocatorKey(typeof (IFoo), requestId); DependencyResolutionLocatorKey result = policy.Map(child.Locator, requestKey); Assert.AreEqual(typeof (Foo), result.Type); Assert.AreEqual(requestId, result.ID); }
/// <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 = null; ILifetimeContainer lifeTime = GetLifetime(context); if (context.Locator != null) { key = new DependencyResolutionLocatorKey(typeToBuild, idToBuild); if (context.Locator.Contains(key, SearchMode.Local)) { return context.Locator.Get(key); } } existing = base.BuildUp(context, typeToBuild, existing, idToBuild); if (context.Locator != null && lifeTime != null && IsSingleton(context, key)) { context.Locator.Add(key, existing); lifeTime.Add(existing); } return existing; }
/// <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())); } }
private static bool IsSingleton(IBuilderContext context, DependencyResolutionLocatorKey key) { ISingletonPolicy policy = context.Policies.Get<ISingletonPolicy>(key.Type, key.ID); return policy != null && policy.IsSingleton; }
/// <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); }
/// <summary> /// See <see cref="ITypeMappingPolicy.Map"/> for more information. /// </summary> public DependencyResolutionLocatorKey Map(DependencyResolutionLocatorKey incomingTypeIDPair) { return(pair); }
public TypeMappingPolicy(Type type, string id) { this.pair = new DependencyResolutionLocatorKey(type, id); }
public DependencyResolutionLocatorKey Map(DependencyResolutionLocatorKey incomingTypeIDPair) { return this.pair; }
public override object GetValue(IBuilderContext context) { DependencyResolutionLocatorKey key = new DependencyResolutionLocatorKey(typeof(WorkItem), null); WorkItem wi = (WorkItem)context.Locator.Get(key); if (id != null) return wi.State[id]; foreach (string stateID in wi.State.Keys) if (type.IsAssignableFrom(wi.State[stateID].GetType())) return wi.State[stateID]; return null; }
public override object GetValue(IBuilderContext context) { DependencyResolutionLocatorKey key = new DependencyResolutionLocatorKey(typeof(ITraceSourceCatalogService), null); ITraceSourceCatalogService svc = (ITraceSourceCatalogService)context.Locator.Get(key); return svc != null ? svc.GetTraceSource(sourceName) : null; }