Remove the cache of factories

It would only have been hit if someone did .ToAbstractFactory() on the same
type, twice, which would have caused a multiple registrations error further
down the line, anyway
This commit is contained in:
Antony Male 2014-03-15 11:20:10 +00:00
parent 9e3c13cede
commit e68fbb12cc
2 changed files with 8 additions and 14 deletions

View File

@ -114,11 +114,6 @@ namespace StyletIoC
/// </summary> /// </summary>
private ModuleBuilder factoryBuilder; private ModuleBuilder factoryBuilder;
/// <summary>
/// Cache of services registered with .ToAbstractFactory() to the factory which was generated for each.
/// </summary>
private readonly ConcurrentDictionary<Type, Type> factories = new ConcurrentDictionary<Type, Type>();
/// <summary> /// <summary>
/// Compile all known bindings (which would otherwise be compiled when needed), checking the dependency graph for consistency /// Compile all known bindings (which would otherwise be compiled when needed), checking the dependency graph for consistency
/// </summary> /// </summary>
@ -420,22 +415,22 @@ namespace StyletIoC
} }
} }
/// <summary>
/// </summary>
/// <remarks>Not thread-safe, as it's only ever called from the builder</remarks>
/// <param name="serviceType"></param>
/// <returns></returns>
internal Type GetFactoryForType(Type serviceType) internal Type GetFactoryForType(Type serviceType)
{ {
if (!serviceType.IsInterface) if (!serviceType.IsInterface)
throw new StyletIoCCreateFactoryException(String.Format("Unable to create a factory implementing type {0}, as it isn't an interface", serviceType.Name)); throw new StyletIoCCreateFactoryException(String.Format("Unable to create a factory implementing type {0}, as it isn't an interface", serviceType.Name));
// Have we built it already?
Type factoryType;
if (this.factories.TryGetValue(serviceType, out factoryType))
return factoryType;
if (this.factoryBuilder == null) if (this.factoryBuilder == null)
{ {
var assemblyName = new AssemblyName(FactoryAssemblyName); var assemblyName = new AssemblyName(FactoryAssemblyName);
var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
var moduleBuilder = assemblyBuilder.DefineDynamicModule("StyletIoCFactoryModule"); var moduleBuilder = assemblyBuilder.DefineDynamicModule("StyletIoCFactoryModule");
Interlocked.CompareExchange(ref this.factoryBuilder, moduleBuilder, null); this.factoryBuilder = moduleBuilder;
} }
// If the service is 'ISomethingFactory', call out new class 'SomethingFactory' // If the service is 'ISomethingFactory', call out new class 'SomethingFactory'
@ -524,8 +519,8 @@ namespace StyletIoC
{ {
throw new StyletIoCCreateFactoryException(String.Format("Unable to create factory type for interface {0}. Ensure that the interface is public, or add [assembly: InternalsVisibleTo(StyletIoC.FactoryAssemblyName)] to your AssemblyInfo.cs", serviceType.Name), e); throw new StyletIoCCreateFactoryException(String.Format("Unable to create factory type for interface {0}. Ensure that the interface is public, or add [assembly: InternalsVisibleTo(StyletIoC.FactoryAssemblyName)] to your AssemblyInfo.cs", serviceType.Name), e);
} }
var actualType = this.factories.GetOrAdd(serviceType, constructedType);
return actualType; return constructedType;
} }
internal BuilderUpper GetBuilderUpper(Type type) internal BuilderUpper GetBuilderUpper(Type type)

View File

@ -53,7 +53,6 @@
<ItemGroup> <ItemGroup>
<Compile Include="ActionExtensionTests.cs" /> <Compile Include="ActionExtensionTests.cs" />
<Compile Include="BindableCollectionTests.cs" /> <Compile Include="BindableCollectionTests.cs" />
<Compile Include="ConductorOneActiveTests.cs" />
<Compile Include="ConductorTests.cs" /> <Compile Include="ConductorTests.cs" />
<Compile Include="EventAggregatorTests.cs" /> <Compile Include="EventAggregatorTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />