Skip to content
This repository has been archived by the owner on Feb 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #140 from JacopoWolf/dev/3.2
Browse files Browse the repository at this point in the history
3.1.1
  • Loading branch information
JacopoWolf authored Dec 8, 2020
2 parents 25d709b + 8b8a403 commit 93d2228
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 73 deletions.
5 changes: 5 additions & 0 deletions StackInjector/Core/Cloning/ClonedCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal ClonedCore ( InjectionCore clonedCore )
this.clonedCore = clonedCore;



public IAsyncStackWrapper<TEntry, TIn, TOut> ToAsyncWrapper<TEntry, TIn, TOut> ( AsyncStackDigest<TEntry, TIn, TOut> digest )
{
var wrapper = new AsyncStackWrapper<TEntry,TIn,TOut>( this.clonedCore )
Expand All @@ -20,6 +21,8 @@ public IAsyncStackWrapper<TEntry, TIn, TOut> ToAsyncWrapper<TEntry, TIn, TOut> (
};

this.clonedCore.EntryType = typeof(TEntry);
if( this.clonedCore.settings._registerAfterCloning )
this.clonedCore.ReadAssemblies();
this.clonedCore.ServeAll();

return wrapper;
Expand All @@ -30,6 +33,8 @@ public IStackWrapper<T> ToWrapper<T> ()
var wrapper = new StackWrapper<T>(this.clonedCore);

this.clonedCore.EntryType = typeof(T);
if( this.clonedCore.settings._registerAfterCloning )
this.clonedCore.ReadAssemblies();
this.clonedCore.ServeAll();

return wrapper;
Expand Down
2 changes: 1 addition & 1 deletion StackInjector/Core/StackWrapperCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public StackWrapperCore ( InjectionCore core, Type toRegister )
this.Core = core;

// setting for referencing the calling wrapper as a service
if( this.Core.settings._registerWrapAsService )
if( this.Core.settings._registerWrapperAsService )
{
this.Core.instances.AddType(toRegister);
this.Core.instances[toRegister].Clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private object OfTypeOrInstantiate ( Type type )
if( serviceAtt == null )
throw new NotAServiceException(type, $"The type {type.FullName} is not annotated with [Service]");

//todo allow disabling of this check and add services at runtime
//todo 4.0: allow disabling of this check and add services at runtime
if( !this.instances.ContainsKey(type) )
throw new ServiceNotFoundException(type, $"The type {type.FullName} is not in a registred assembly!");

Expand Down
20 changes: 19 additions & 1 deletion StackInjector/Core/injectionCore/InjectionCore.logic.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using StackInjector.Exceptions;

Expand Down Expand Up @@ -39,6 +40,9 @@ internal void ServeAll ()
toInject.Enqueue(service);
}

if( this.settings._cleanUnusedTypesAftInj )
this.RemoveUnusedTypes();

}

}
Expand All @@ -59,5 +63,19 @@ internal T GetEntryPoint<T> ()
innerException: new ServiceNotFoundException(typeof(T), string.Empty)
);
}


// remove types with no instances
internal void RemoveUnusedTypes ()
{
var unused = this.instances
.Where(p => !p.Value.Any())
.Select(p=>p.Key)
.ToList();

foreach( var type in unused )
this.instances.Remove(type);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ private Type ClassOrVersionFromInterface ( Type type, ServedAttribute servedAttr
}



// reads all [Service] classes
internal void ReadAssemblies ()
{
Expand Down
30 changes: 28 additions & 2 deletions StackInjector/Settings/StackWrapperSettings.configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public sealed partial class StackWrapperSettings
/// <returns>the modified settings</returns>
public StackWrapperSettings RegisterAssemblies ( params Assembly[] assemblies )
{
if( assemblies is null )
throw new ArgumentNullException(nameof(assemblies));

foreach( var assembly in assemblies )
this._registredAssemblies.Add(assembly);
return this;
Expand Down Expand Up @@ -50,7 +53,7 @@ public StackWrapperSettings RegisterDomain ( string regexFilter = Injector.Defau
/// <returns>the modified settings</returns>
public StackWrapperSettings RegisterAssemblyOf<T> ()
{
this._registredAssemblies.Add(typeof(T).Assembly);
this.RegisterAssemblies(typeof(T).Assembly);
return this;
}

Expand All @@ -73,7 +76,19 @@ public StackWrapperSettings RegisterEntryAssembly ( bool register = true )
/// <returns>the modified settings</returns>
public StackWrapperSettings RegisterWrapperAsService ( bool register = true )
{
this._registerWrapAsService = register;
this._registerWrapperAsService = register;
return this;
}


/// <summary>
/// If set, when cloned the StackWrapper will re-scans all assemblies before the injection.
/// Used to update assemblies with new types.
/// </summary>
/// <returns>the modified settings</returns>
public StackWrapperSettings RegisterAfterCloning ( bool register = true )
{
this._registerAfterCloning = register;
return this;
}

Expand Down Expand Up @@ -144,6 +159,17 @@ public StackWrapperSettings InjectionServingMethods ( ServingMethods methods, bo
return this;
}


/// <summary>
/// Remove the reference to unused types after the injection is finished.
/// </summary>
/// <returns>The modified settings</returns>
public StackWrapperSettings RemoveUnusedTypesAfterInjection ( bool remove = true )
{
this._cleanUnusedTypesAftInj = remove;
return this;
}

#endregion


Expand Down
5 changes: 4 additions & 1 deletion StackInjector/Settings/StackWrapperSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public sealed partial class StackWrapperSettings
// registration
internal HashSet<Assembly> _registredAssemblies = new HashSet<Assembly>();
internal bool _registerEntryPointAssembly;
internal bool _registerWrapAsService;
internal bool _registerWrapperAsService;
internal bool _registerAfterCloning;

// disposing
internal bool _trackInstancesDiff;
Expand All @@ -35,6 +36,7 @@ public sealed partial class StackWrapperSettings

internal ServingMethods _servingMethod = ServingMethods.DoNotServe;
internal bool _overrideServingMethod;
internal bool _cleanUnusedTypesAftInj;

// features
internal bool _serveEnumerables;
Expand Down Expand Up @@ -78,6 +80,7 @@ public static StackWrapperSettings Default
new StackWrapperSettings()
.RegisterEntryAssembly()
.RegisterWrapperAsService()
.RegisterAfterCloning(false)
.TrackInstantiationDiff(false, callDispose: false)
.InjectionVersioningMethod(ServedVersionTargetingMethod.None, @override: false)
.InjectionServingMethods(Injector.Defaults.ServeAllStrict, @override: false)
Expand Down
58 changes: 58 additions & 0 deletions tests/StackInjector.TEST.BlackBox/Features/Test.Versioning.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using StackInjector.Attributes;
using StackInjector.Settings;

namespace StackInjector.TEST.BlackBox.Features
{
internal class Versioning
{
#pragma warning disable CS0649

[Service] private class VersionClass {[Served] internal Level1A Level1_2; }

[Test]
public void ServerdVersioningClass ()
{
var settings =
StackWrapperSettings.Default
.InjectionVersioningMethod(ServedVersionTargetingMethod.LatestMajor,true);

var versionedService = Injector.From<VersionClass>(settings).Entry.Level1_2;

/* CLASSES ARE NOT VERSIONED, ONLY INTERFACES
* this is why this tests checks if the field is not of Level1_2
*/
Assert.That(versionedService, Is.Not.InstanceOf<Level1_2>());

}


[Test]
public void SettingVersioningLatestMaj ()
{
var settings =
StackWrapperSettings.Default
.InjectionVersioningMethod(ServedVersionTargetingMethod.LatestMajor,true);

var versionedService = Injector.From<InterfaceVersionedBase>( settings ).Entry.level1;

Assert.That(versionedService, Is.TypeOf<Level1LatestVersion>());
}


[Test]
public void SettingVersioningLatestMin ()
{
var settings =
StackWrapperSettings.Default
.InjectionVersioningMethod(ServedVersionTargetingMethod.LatestMinor,true);

var versionedService = Injector.From<InterfaceVersionedBase>( settings ).Entry.level1;

Assert.That(versionedService, Is.TypeOf<Level1B>());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using StackInjector.Settings;
using StackInjector.TEST.ExternalAssembly;

namespace StackInjector.TEST.BlackBox
namespace StackInjector.TEST.BlackBox.UseCases
{

#pragma warning disable IDE0051, IDE0044, CS0169, CS0649
Expand Down Expand Up @@ -33,7 +33,7 @@ public void ThrowsBaseNotAService ()

// references class in unregistred external assembly
[Service]
private class BaseServiceNotFoundThrower {[Served] public Externalclass externalClass; }
internal class BaseServiceNotFoundThrower {[Served] public Externalclass externalClass; }

[Test]
public void ThrowsServiceNotFound ()
Expand Down
Loading

0 comments on commit 93d2228

Please sign in to comment.