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>
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>
/// Compile all known bindings (which would otherwise be compiled when needed), checking the dependency graph for consistency
/// </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)
{
if (!serviceType.IsInterface)
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)
{
var assemblyName = new AssemblyName(FactoryAssemblyName);
var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
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'
@ -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);
}
var actualType = this.factories.GetOrAdd(serviceType, constructedType);
return actualType;
return constructedType;
}
internal BuilderUpper GetBuilderUpper(Type type)

View File

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