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

Commit e7d4f3c

Browse files
authored
Merge pull request #177 from JacopoWolf/dev/4
Dev/4
2 parents dcfdc29 + 1ded7fb commit e7d4f3c

16 files changed

Lines changed: 538 additions & 91 deletions

StackInjector/Core/IStackWrapperCore.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ public interface IStackWrapperCore : IDisposable, ICloneableCore
2424
/// <typeparam name="T"></typeparam>
2525
IEnumerable<T> GetServices<T> ();
2626

27-
//! description is wrong
2827
/// <summary>
2928
/// The current number of all tracked services.<br/>
30-
/// Does also include the Wrapper, so if you want all the instances
29+
/// Does also include the Wrapper, so if you want just the instances
3130
/// <c>wrapper.CountServices()-1</c>
3231
/// </summary>
3332
int CountServices ();

StackInjector/Core/InjectionCore/InjectionCore.instantiation.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ private object OfTypeOrInstantiate ( Type type )
1717
if ( serviceAtt == null )
1818
throw new NotAServiceException(type, $"Type {type.FullName} is not a [Service]");
1919

20-
////if( !this.instances.ContainsKey(type) )
21-
//// throw new ServiceNotFoundException(type, $"The type {type.FullName} is not in a registred assembly!");
22-
2320

2421
return serviceAtt.Pattern switch
2522
{

StackInjector/Core/InjectionCore/InjectionCore.reflection.cs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,34 @@ private Type ClassOrVersionFromInterface ( Type type, ServedAttribute servedAttr
1414
{
1515
if ( type.IsInterface )
1616
{
17-
IEnumerable<Type> versions = this.instances.TypesAssignableFrom(type);
17+
IEnumerable<Type> versions = this.Version(type, servedAttribute);
1818

19-
// is there already an implementation for the interface?
2019
if ( versions.Any() )
2120
{
22-
return versions.First();
21+
//todo check for multiple valid versions
22+
var t = versions.First();
23+
MaskPass(t);
24+
return t;
2325
}
26+
27+
if ( servedAttribute is null )
28+
throw new ImplementationNotFoundException(type, $"can't find [Service] for interface {type.Name}");
2429
else
25-
{
26-
versions = this.Version(type, servedAttribute);
27-
if ( versions.Any() )
28-
{
29-
var t = versions.First();
30-
MaskPass(t);
31-
return t;
32-
}
33-
34-
if ( servedAttribute is null )
35-
throw new ImplementationNotFoundException(type, $"can't find [Service] for interface {type.Name}");
36-
else
37-
throw new ImplementationNotFoundException(type, $"can't find [Service] for {type.Name} v{servedAttribute.TargetVersion}");
38-
}
30+
throw new ImplementationNotFoundException(type, $"can't find [Service] for {type.Name} v{servedAttribute.TargetVersion}");
31+
3932
}
4033
else
4134
{
4235
MaskPass(type);
4336
return type;
4437
}
4538

46-
39+
// exception check and thrower
4740
void MaskPass ( Type type )
4841
{
49-
if ( this.settings.Mask.IsMasked(type) )
42+
if ( this.settings.Mask.IsMasked(type) ) //todo create custom exception
5043
throw new InvalidOperationException($"Type {type.Name} is { (this.settings.Mask._isWhiteList ? "not whitelisted" : "blacklisted")}");
51-
//todo create custom exception
44+
5245
}
5346

5447

StackInjector/Core/InjectionCore/InjectionCore.versioning.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,40 @@ namespace StackInjector.Core
99
{
1010
internal partial class InjectionCore
1111
{
12+
private static readonly Type _istackwrappercore = typeof(IStackWrapperCore);
13+
1214
// performs versioning on the specified type
1315
private IEnumerable<Type> Version ( Type targetType, ServedAttribute servedAttribute )
1416
{
17+
if (targetType.IsSubclassOf( _istackwrappercore ) || targetType == _istackwrappercore)
18+
return this.instances.TypesAssignableFrom(targetType);
19+
20+
if (this.settings.Versioning._customBindings != null &&
21+
this.settings.Versioning._customBindings.TryGetValue(targetType,out var t) )
22+
{
23+
return Enumerable.Repeat(t, 1);
24+
}
25+
1526

1627
var targetVersion = servedAttribute?.TargetVersion ?? 0.0;
1728
var method =
18-
(this.settings.Injection._overrideTargetingMethod || servedAttribute is null || !(servedAttribute._targetingDefined))
19-
? this.settings.Injection._targetingMethod
29+
(this.settings.Versioning._overrideTargetingMethod || servedAttribute is null || !(servedAttribute._targetingDefined))
30+
? this.settings.Versioning._targetingMethod
2031
: servedAttribute.TargetingMethod;
2132

22-
23-
////var candidateTypes = this.instances.TypesAssignableFrom(targetType);
24-
var candidateTypes = targetType
25-
.Assembly
26-
.GetTypes()
27-
.Where( t => t.IsClass && !t.IsAbstract && targetType.IsAssignableFrom(t));
33+
IEnumerable<TypeInfo> candidateTypes =
34+
(this.settings.Versioning.AssemblyLookUpOrder.Any())
35+
?
36+
this.settings.Versioning
37+
.AssemblyLookUpOrder
38+
.SelectMany( a => a.DefinedTypes )
39+
.Where( t => t.IsClass && !t.IsAbstract
40+
&& targetType.IsAssignableFrom(t) && !this.settings.Mask.IsMasked(t) )
41+
:
42+
targetType
43+
.Assembly
44+
.DefinedTypes
45+
.Where( t => t.IsClass && !t.IsAbstract && targetType.IsAssignableFrom(t) );
2846

2947

3048
return method switch

StackInjector/Core/InstancesHolder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ internal bool AddType ( Type type )
2727
return this.TryAdd(type, new LinkedList<object>());
2828
}
2929

30-
internal void CountAllInstances ()
30+
internal int CountAllInstances ()
3131
{
3232
this.total_count = 0;
3333
foreach ( var pair in this )
3434
this.total_count += pair.Value.Count;
35+
return this.total_count;
3536
}
3637

3738

StackInjector/Core/StackWrapperCore.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public StackWrapperCore ( InjectionCore core, Type toRegister )
2626
if ( !this.Core.instances.AddType(toRegister) )
2727
this.Core.instances[toRegister].Clear();
2828
this.Core.instances[toRegister].AddFirst(this);
29-
29+
3030
}
3131

3232

@@ -38,8 +38,10 @@ public IEnumerable<T> GetServices<T> ()
3838
.Select(o => (T)o);
3939
}
4040

41-
public int CountServices () => this.Core.instances.total_count;
42-
41+
public int CountServices ()
42+
{
43+
return this.Core.instances.CountAllInstances();
44+
}
4345

4446
public IClonedCore CloneCore ( StackWrapperSettings settings = null )
4547
{

StackInjector/Settings/InjectionOptions.cs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ namespace StackInjector.Settings
55
{
66

77
/// <summary>
8-
/// Options for the injection process.
8+
/// Options for the injection process, regarding HOW the inejction should be performed
99
/// </summary>
1010
public sealed class InjectionOptions : IOptions
1111
{
12-
internal ServedVersionTargetingMethod _targetingMethod = ServedVersionTargetingMethod.None;
13-
internal bool _overrideTargetingMethod;
1412

1513
internal ServingMethods _servingMethod = StackWrapperSettings.ServeAllStrict;
1614
internal bool _overrideServingMethod;
@@ -42,10 +40,7 @@ public object Clone ()
4240
/// <summary>
4341
/// The default injection. Settings are valorized as following:
4442
/// <list type="table">
45-
/// <item>
46-
/// <term><see cref="VersioningMethod(ServedVersionTargetingMethod, bool)"/></term>
47-
/// <description><see cref="ServedVersionTargetingMethod.None"/>, false</description>
48-
/// </item><item>
43+
/// <item>
4944
/// <term><see cref="ServingMethod(ServingMethods, bool)"/></term><description>
5045
/// <see cref="StackWrapperSettings.ServeAllStrict"/>, false</description>
5146
/// </item><item>
@@ -82,20 +77,6 @@ public InjectionOptions TrackInstantiationDiff ( bool track = true, bool callDis
8277
return this;
8378
}
8479

85-
86-
/// <summary>
87-
/// Overrides default targetting method
88-
/// </summary>
89-
/// <param name="targetMethod">the new default targetting method</param>
90-
/// <param name="override">if true, versioning methods for [Served] fields and properties are overriden</param>
91-
/// <returns>the modified settings</returns>
92-
public InjectionOptions VersioningMethod ( ServedVersionTargetingMethod targetMethod, bool @override = false )
93-
{
94-
this._targetingMethod = targetMethod;
95-
this._overrideTargetingMethod = @override;
96-
return this;
97-
}
98-
9980
/// <summary>
10081
/// Overrides default serving method
10182
/// </summary>

StackInjector/Settings/MaskOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public bool IsMasked ( Type type )
5050
{
5151
if ( _isDisabled )
5252
return false;
53-
else
54-
return this._isWhiteList ^ this.Contains(type); //.XOR
53+
54+
return this._isWhiteList ^ this.Contains(type); //.XOR
5555

5656
}
5757

@@ -68,7 +68,7 @@ public object Clone ()
6868
public static MaskOptions WhiteList => new MaskOptions() { _isWhiteList = true };
6969

7070
/// <summary>
71-
/// allow every type <b>except</b> the regisred ones.
71+
/// allow every type <b>except</b> the registred ones.
7272
/// </summary>
7373
public static MaskOptions BlackList => new MaskOptions();
7474

StackInjector/Settings/StackWrapperSettings.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@ public sealed partial class StackWrapperSettings
1818
/// </summary>
1919
public InjectionOptions Injection { get; private set; }
2020

21+
/// <summary>
22+
/// manages versioning options.
23+
/// </summary>
24+
public VersioningOptions Versioning { get; private set; }
25+
2126
/// <summary>
2227
/// manages runtime options
2328
/// </summary>
2429
public RuntimeOptions Runtime { get; private set; }
2530

2631

2732

33+
2834
private StackWrapperSettings () { }
2935

3036

@@ -38,7 +44,8 @@ public StackWrapperSettings Clone ()
3844
return With(
3945
(InjectionOptions)this.Injection.Clone(),
4046
(RuntimeOptions)this.Runtime.Clone(),
41-
(MaskOptions)this.Mask.Clone()
47+
(MaskOptions)this.Mask.Clone(),
48+
(VersioningOptions)this.Versioning.Clone()
4249
);
4350
}
4451

@@ -48,6 +55,7 @@ public StackWrapperSettings Clone ()
4855
/// <seealso cref="InjectionOptions.Default"/>,
4956
/// <seealso cref="RuntimeOptions.Default"/>,
5057
/// <seealso cref="MaskOptions.Disabled"/>
58+
/// <seealso cref="VersioningOptions.Default"/>
5159
/// </summary>
5260
public static StackWrapperSettings Default => With();
5361

@@ -58,14 +66,18 @@ public StackWrapperSettings Clone ()
5866
/// <param name="injection">the injection options</param>
5967
/// <param name="runtime">the runtime options</param>
6068
/// <param name="mask">mask options</param>
69+
/// <param name="versioning">versioning options</param>
6170
/// <returns></returns>
62-
public static StackWrapperSettings With ( InjectionOptions injection = null, RuntimeOptions runtime = null, MaskOptions mask = null )
71+
public static StackWrapperSettings With (
72+
InjectionOptions injection = null, RuntimeOptions runtime = null,
73+
MaskOptions mask = null, VersioningOptions versioning = null )
6374
{
6475
return new StackWrapperSettings()
6576
{
6677
Mask = mask ?? MaskOptions.Disabled,
6778
Injection = injection ?? InjectionOptions.Default,
68-
Runtime = runtime ?? RuntimeOptions.Default
79+
Runtime = runtime ?? RuntimeOptions.Default,
80+
Versioning = versioning ?? VersioningOptions.Default
6981
};
7082
}
7183

@@ -83,8 +95,10 @@ public static StackWrapperSettings With ( InjectionOptions injection = null, Run
8395
mask:
8496
null,
8597
runtime:
98+
null,
99+
versioning:
86100
null
87-
);
101+
);
88102

89103

90104
}

0 commit comments

Comments
 (0)