public void CanGetNullNameMapperForNonAnnotatedTypeTheFirstTime() { MockImplementor.constructorCalls = 0; IConfigurationNameMapper mapper = cache.GetConfigurationNameMapper(typeof(TypeWithNoCustomFactoryAttribute)); Assert.IsNull(mapper); Assert.AreEqual(0, MockImplementor.constructorCalls); }
public void CanGetCachedNameMapperForAnnotatedTypeTheSecondTime() { MockImplementor.constructorCalls = 0; IConfigurationNameMapper mapper1 = cache.GetConfigurationNameMapper(typeof(TypeWithCustomFactoryAttribute)); IConfigurationNameMapper mapper2 = cache.GetConfigurationNameMapper(typeof(TypeWithCustomFactoryAttribute)); Assert.IsNotNull(mapper1); Assert.IsNotNull(mapper2); Assert.AreSame(typeof(MockImplementor), mapper1.GetType()); Assert.AreSame(typeof(MockImplementor), mapper2.GetType()); Assert.AreEqual(1, MockImplementor.constructorCalls); Assert.AreSame(mapper1, mapper2); }
/// <summary> /// Override of <see cref="IBuilderStrategy.BuildUp"/>. Updates the instance name using a name mapper associated to type <paramref name="t"/> /// so later strategies in the build chain will use the updated instance name. /// </summary> /// <remarks> /// Will only update the instance name if it is <see langword="null"/>. /// </remarks> /// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param> /// <param name="t">The type to build.</param> /// <param name="existing">The existing object. Should be <see langword="null"/>.</param> /// <param name="id">The ID of the object to be created.</param> /// <returns>The created object.</returns> /// <exception cref="System.Configuration.ConfigurationErrorsException"> when the configuration required to do the mapping is not present or is /// invalid in the configuration source.</exception> public override object BuildUp(IBuilderContext context, Type t, object existing, string id) { if (id == null) { ConfigurationReflectionCache reflectionCache = GetReflectionCache(context); IConfigurationNameMapper mapper = reflectionCache.GetConfigurationNameMapper(t); if (mapper != null) { id = mapper.MapName(id, GetConfigurationSource(context)); } } return(base.BuildUp(context, t, existing, id)); }
public override void PreBuildUp(IBuilderContext context) { base.PreBuildUp(context); NamedTypeBuildKey key = (NamedTypeBuildKey)context.BuildKey; if (key.Name == null) { ConfigurationReflectionCache reflectionCache = GetReflectionCache(context); IConfigurationNameMapper mapper = reflectionCache.GetConfigurationNameMapper(key.Type); if (mapper != null) { context.BuildKey = new NamedTypeBuildKey(key.Type, mapper.MapName(null, GetConfigurationSource(context))); } } }