Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
z4kn4fein committed Mar 13, 2017
1 parent 0dcf9eb commit 1e3200b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 59 deletions.
69 changes: 17 additions & 52 deletions src/stashbox/BuildExtensionManager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Stashbox.Entity;
using Stashbox.Infrastructure;
using Stashbox.Infrastructure.ContainerExtension;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Stashbox.Utils;
Expand All @@ -11,87 +10,53 @@ namespace Stashbox
{
internal class BuildExtensionManager : IContainerExtensionManager
{
private readonly HashSet<IPostBuildExtension> postbuildExtensions;
private readonly HashSet<IRegistrationExtension> registrationExtensions;
private readonly DisposableReaderWriterLock readerWriterLock;
private readonly ConcurrentTree<IContainerExtension> repository;
private int extensionCounter;

public bool HasPostBuildExtensions => this.hasPostBuildExtensions;

private bool hasPostBuildExtensions;
private bool hasRegistrationExtensions;
public bool HasPostBuildExtensions { get; private set; }

public BuildExtensionManager()
{
this.postbuildExtensions = new HashSet<IPostBuildExtension>();
this.registrationExtensions = new HashSet<IRegistrationExtension>();
this.readerWriterLock = new DisposableReaderWriterLock(LockRecursionPolicy.SupportsRecursion);
this.repository = new ConcurrentTree<IContainerExtension>();
}

public void AddExtension(IContainerExtension containerExtension)
{
using (this.readerWriterLock.AcquireWriteLock())
{
var postBuildExtension = containerExtension as IPostBuildExtension;
if (postBuildExtension != null)
{
this.postbuildExtensions.Add(postBuildExtension);
this.hasPostBuildExtensions = true;
}
if (containerExtension is IPostBuildExtension)
this.HasPostBuildExtensions = true;

var registrationExtension = containerExtension as IRegistrationExtension;
if (registrationExtension == null) return;
this.registrationExtensions.Add(registrationExtension);
this.hasRegistrationExtensions = true;
}
this.repository.AddOrUpdate(Interlocked.Increment(ref extensionCounter), containerExtension);
}

public object ExecutePostBuildExtensions(object instance, IContainerContext containerContext, ResolutionInfo resolutionInfo,
Type resolveType, InjectionParameter[] injectionParameters = null)
{
if (!this.hasPostBuildExtensions) return instance;
using (this.readerWriterLock.AcquireReadLock())
{
var result = instance;
foreach (var extension in this.postbuildExtensions)
result = extension.PostBuild(instance, containerContext, resolutionInfo, resolveType, injectionParameters);

return result;
}
}
Type resolveType, InjectionParameter[] injectionParameters = null) =>
this.repository.OfType<IPostBuildExtension>().Aggregate(instance, (current, extension) => extension.PostBuild(current, containerContext, resolutionInfo, resolveType, injectionParameters));

public void ExecuteOnRegistrationExtensions(IContainerContext containerContext, Type typeTo, Type typeFrom, InjectionParameter[] injectionParameters = null)
{
if (!this.hasRegistrationExtensions) return;
using (this.readerWriterLock.AcquireReadLock())
{
foreach (var extension in this.registrationExtensions)
extension.OnRegistration(containerContext, typeTo, typeFrom, injectionParameters);
}
foreach (var extension in this.repository.OfType<IRegistrationExtension>())
extension.OnRegistration(containerContext, typeTo, typeFrom, injectionParameters);
}

public IContainerExtensionManager CreateCopy()
{
var extensionManager = new BuildExtensionManager();
using (this.readerWriterLock.AcquireReadLock())
foreach (var extension in this.postbuildExtensions.OfType<IContainerExtension>().Concat(this.registrationExtensions))
extensionManager.AddExtension(extension.CreateCopy());
foreach (var extension in this.repository)
extensionManager.AddExtension(extension.CreateCopy());

return extensionManager;
}

public void ReinitalizeExtensions(IContainerContext containerContext)
{
using (this.readerWriterLock.AcquireReadLock())
foreach (var extension in this.postbuildExtensions.OfType<IContainerExtension>().Concat(this.registrationExtensions))
extension.Initialize(containerContext);
foreach (var extension in this.repository)
extension.Initialize(containerContext);
}


public void CleanUp()
{
using (this.readerWriterLock.AcquireWriteLock())
foreach (var extension in this.postbuildExtensions.OfType<IContainerExtension>().Concat(this.registrationExtensions))
extension.CleanUp();
foreach (var extension in this.repository)
extension.CleanUp();
}
}
}
6 changes: 3 additions & 3 deletions src/stashbox/Registration/RegistrationContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected void PrepareRegistration(IContainerExtensionManager containerExtension
}

private IObjectBuilder CompleteRegistration(IContainerExtensionManager containerExtensionManager, bool update,
MetaInfoProvider metaInfoProvider, string registrationName, ILifetime registrationLifetime)
IMetaInfoProvider metaInfoProvider, string registrationName, ILifetime registrationLifetime)
{
var objectBuilder = this.TypeTo.IsOpenGenericType() ? new GenericTypeObjectBuilder(this.RegistrationContextData, this.ContainerContext,
metaInfoProvider, containerExtensionManager, this.expressionBuilder) : this.CreateObjectBuilder(containerExtensionManager, metaInfoProvider);
Expand Down Expand Up @@ -87,13 +87,13 @@ private IObjectBuilder CreateObjectBuilder(IContainerExtensionManager containerE
containerExtensionManager, this.expressionBuilder, this.RegistrationContextData.InjectionParameters);
}

private IServiceRegistration CreateServiceRegistration(ILifetime lifeTime, IObjectBuilder objectBuilder, MetaInfoProvider metaInfoProvider)
private IServiceRegistration CreateServiceRegistration(ILifetime lifeTime, IObjectBuilder objectBuilder, IMetaInfoProvider metaInfoProvider)
{
return new ServiceRegistration(this.TypeFrom, this.TypeTo, this.ContainerContext, lifeTime, objectBuilder, metaInfoProvider, this.RegistrationContextData.AttributeConditions,
this.RegistrationContextData.TargetTypeCondition, this.RegistrationContextData.ResolutionCondition);
}

private ILifetime ChooseLifeTime() => this.RegistrationContextData.ExistingInstance != null ? new TransientLifetime() :
this.RegistrationContextData.Lifetime ?? new TransientLifetime();
this.RegistrationContextData.Lifetime ?? new TransientLifetime();
}
}
1 change: 1 addition & 0 deletions src/stashbox/Resolution/ResolverSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal class ResolverSelector : IResolverSelector
{
private readonly ConcurrentTree<int, Resolver> resolverRepository;
private int resolverCounter;

public ResolverSelector()
{
this.resolverRepository = new ConcurrentTree<int, Resolver>();
Expand Down
6 changes: 2 additions & 4 deletions src/stashbox/StashboxContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,9 @@ public bool CanResolve<TFrom>(string name = null) =>
this.CanResolve(typeof(TFrom), name);

/// <inheritdoc />
public bool CanResolve(Type typeFrom, string name = null)
{
return this.registrationRepository.ContainsRegistration(typeFrom, name) ||
public bool CanResolve(Type typeFrom, string name = null) =>
this.registrationRepository.ContainsRegistration(typeFrom, name) ||
this.resolverSelector.CanResolve(this.ContainerContext, new TypeInformation { Type = typeFrom, DependencyName = name });
}

/// <inheritdoc />
public void Validate()
Expand Down

0 comments on commit 1e3200b

Please sign in to comment.