From 1e3200b322061c31d66c65d158768b26a573156e Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Tue, 14 Mar 2017 00:33:27 +0100 Subject: [PATCH] Refactor --- src/stashbox/BuildExtensionManager.cs | 69 +++++-------------- .../Registration/RegistrationContextBase.cs | 6 +- src/stashbox/Resolution/ResolverSelector.cs | 1 + src/stashbox/StashboxContainer.cs | 6 +- 4 files changed, 23 insertions(+), 59 deletions(-) diff --git a/src/stashbox/BuildExtensionManager.cs b/src/stashbox/BuildExtensionManager.cs index 8a490bcb..1cbc4ec8 100644 --- a/src/stashbox/BuildExtensionManager.cs +++ b/src/stashbox/BuildExtensionManager.cs @@ -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; @@ -11,87 +10,53 @@ namespace Stashbox { internal class BuildExtensionManager : IContainerExtensionManager { - private readonly HashSet postbuildExtensions; - private readonly HashSet registrationExtensions; - private readonly DisposableReaderWriterLock readerWriterLock; + private readonly ConcurrentTree 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(); - this.registrationExtensions = new HashSet(); - this.readerWriterLock = new DisposableReaderWriterLock(LockRecursionPolicy.SupportsRecursion); + this.repository = new ConcurrentTree(); } 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().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()) + extension.OnRegistration(containerContext, typeTo, typeFrom, injectionParameters); } public IContainerExtensionManager CreateCopy() { var extensionManager = new BuildExtensionManager(); - using (this.readerWriterLock.AcquireReadLock()) - foreach (var extension in this.postbuildExtensions.OfType().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().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().Concat(this.registrationExtensions)) - extension.CleanUp(); + foreach (var extension in this.repository) + extension.CleanUp(); } } } diff --git a/src/stashbox/Registration/RegistrationContextBase.cs b/src/stashbox/Registration/RegistrationContextBase.cs index b9f151a6..bb9d0b9b 100644 --- a/src/stashbox/Registration/RegistrationContextBase.cs +++ b/src/stashbox/Registration/RegistrationContextBase.cs @@ -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); @@ -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(); } } diff --git a/src/stashbox/Resolution/ResolverSelector.cs b/src/stashbox/Resolution/ResolverSelector.cs index 009f5aaf..ff9ee502 100644 --- a/src/stashbox/Resolution/ResolverSelector.cs +++ b/src/stashbox/Resolution/ResolverSelector.cs @@ -12,6 +12,7 @@ internal class ResolverSelector : IResolverSelector { private readonly ConcurrentTree resolverRepository; private int resolverCounter; + public ResolverSelector() { this.resolverRepository = new ConcurrentTree(); diff --git a/src/stashbox/StashboxContainer.cs b/src/stashbox/StashboxContainer.cs index 817719b3..98afccbb 100644 --- a/src/stashbox/StashboxContainer.cs +++ b/src/stashbox/StashboxContainer.cs @@ -76,11 +76,9 @@ public bool CanResolve(string name = null) => this.CanResolve(typeof(TFrom), name); /// - 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 }); - } /// public void Validate()