diff --git a/Telerik.JustMock/AutoMock/MockingContainer.cs b/Telerik.JustMock/AutoMock/MockingContainer.cs index f4b8f048..595f250b 100644 --- a/Telerik.JustMock/AutoMock/MockingContainer.cs +++ b/Telerik.JustMock/AutoMock/MockingContainer.cs @@ -54,23 +54,23 @@ public MockingContainer(AutoMockSettings settings = null) { } - /// - /// Implementation detail. - /// - protected override bool ShouldAddComponent(Type component, Type implementation) - { - if (implementation == typeof(SelfBindingResolver)) - { - return false; - } - - return base.ShouldAddComponent(component, implementation); - } - - /// - /// Implementation detail. - /// - protected override void AddComponents() + /// + /// Implementation detail. + /// + protected override bool ShouldAddComponent(Type component, Type implementation) + { + if (implementation == typeof(SelfBindingResolver)) + { + return false; + } + + return base.ShouldAddComponent(component, implementation); + } + + /// + /// Implementation detail. + /// + protected override void AddComponents() { base.AddComponents(); diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Blocks/ActivationBlock.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Blocks/ActivationBlock.cs index 3b109700..0bc6fe27 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Blocks/ActivationBlock.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Blocks/ActivationBlock.cs @@ -1,40 +1,41 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Disposal; -using Telerik.JustMock.AutoMock.Ninject.Parameters; -using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; -using Telerik.JustMock.AutoMock.Ninject.Syntax; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation.Blocks { + using System; + using System.Collections.Generic; + + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Disposal; + using Telerik.JustMock.AutoMock.Ninject.Parameters; + using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; + using Telerik.JustMock.AutoMock.Ninject.Syntax; + /// /// A block used for deterministic disposal of activated instances. When the block is /// disposed, all instances activated via it will be deactivated. /// public class ActivationBlock : DisposableObject, IActivationBlock { - /// - /// Gets or sets the parent resolution root (usually the kernel). - /// - public IResolutionRoot Parent { get; private set; } - - /// - /// Occurs when the object is disposed. - /// - public event EventHandler Disposed; - /// /// Initializes a new instance of the class. /// @@ -42,25 +43,23 @@ public class ActivationBlock : DisposableObject, IActivationBlock public ActivationBlock(IResolutionRoot parent) { Ensure.ArgumentNotNull(parent, "parent"); - Parent = parent; + + this.Parent = parent; } /// - /// Releases resources held by the object. + /// Gets the parent resolution root (usually the kernel). /// - public override void Dispose(bool disposing) - { - lock (this) - { - if (disposing && !IsDisposed) - { - var evt = Disposed; - if (evt != null) evt(this, EventArgs.Empty); - Disposed = null; - } + public IResolutionRoot Parent { get; private set; } - base.Dispose(disposing); - } + /// + /// Injects the specified existing instance, without managing its lifecycle. + /// + /// The instance to inject. + /// The parameters to pass to the request. + public void Inject(object instance, params IParameter[] parameters) + { + this.Parent.Inject(instance, parameters); } /// @@ -71,6 +70,7 @@ public override void Dispose(bool disposing) public bool CanResolve(IRequest request) { Ensure.ArgumentNotNull(request, "request"); + return this.Parent.CanResolve(request); } @@ -85,6 +85,7 @@ public bool CanResolve(IRequest request) public bool CanResolve(IRequest request, bool ignoreImplicitBindings) { Ensure.ArgumentNotNull(request, "request"); + return this.Parent.CanResolve(request, ignoreImplicitBindings); } @@ -97,7 +98,8 @@ public bool CanResolve(IRequest request, bool ignoreImplicitBindings) public IEnumerable Resolve(IRequest request) { Ensure.ArgumentNotNull(request, "request"); - return Parent.Resolve(request); + + return this.Parent.Resolve(request); } /// @@ -113,6 +115,7 @@ public virtual IRequest CreateRequest(Type service, Func { Ensure.ArgumentNotNull(service, "service"); Ensure.ArgumentNotNull(parameters, "parameters"); + return new Request(service, constraint, parameters, () => this, isOptional, isUnique); } @@ -121,10 +124,9 @@ public virtual IRequest CreateRequest(Type service, Func /// /// The instance to release. /// if the instance was found and released; otherwise . - /// public bool Release(object instance) { - return Parent.Release(instance); + return this.Parent.Release(instance); } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Blocks/IActivationBlock.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Blocks/IActivationBlock.cs index 7c229e91..6c09c50b 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Blocks/IActivationBlock.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Blocks/IActivationBlock.cs @@ -1,23 +1,34 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Disposal; -using Telerik.JustMock.AutoMock.Ninject.Syntax; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation.Blocks { + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Disposal; + using Telerik.JustMock.AutoMock.Ninject.Syntax; + /// /// A block used for deterministic disposal of activated instances. When the block is /// disposed, all instances activated via it will be deactivated. /// - public interface IActivationBlock : IResolutionRoot, INotifyWhenDisposed { } + public interface IActivationBlock : IResolutionRoot, INotifyWhenDisposed + { + } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/ActivationCache.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/ActivationCache.cs index b4da23b1..22ad78d6 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/ActivationCache.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/ActivationCache.cs @@ -1,8 +1,28 @@ +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- + namespace Telerik.JustMock.AutoMock.Ninject.Activation.Caching { - using System; using System.Collections.Generic; - using System.Linq; + using Telerik.JustMock.AutoMock.Ninject.Components; using Telerik.JustMock.AutoMock.Ninject.Infrastructure; @@ -11,17 +31,6 @@ namespace Telerik.JustMock.AutoMock.Ninject.Activation.Caching /// public class ActivationCache : NinjectComponent, IActivationCache, IPruneable { -#if SILVERLIGHT_20 || SILVERLIGHT_30 || WINDOWS_PHONE || NETCF || MONO - /// - /// The objects that were activated as reference equal weak references. - /// - private readonly IDictionary activatedObjects = new Dictionary(new WeakReferenceEqualityComparer()); - - /// - /// The objects that were activated as reference equal weak references. - /// - private readonly IDictionary deactivatedObjects = new Dictionary(new WeakReferenceEqualityComparer()); -#else /// /// The objects that were activated as reference equal weak references. /// @@ -31,7 +40,6 @@ public class ActivationCache : NinjectComponent, IActivationCache, IPruneable /// The objects that were activated as reference equal weak references. /// private readonly HashSet deactivatedObjects = new HashSet(new WeakReferenceEqualityComparer()); -#endif /// /// Initializes a new instance of the class. @@ -39,9 +47,10 @@ public class ActivationCache : NinjectComponent, IActivationCache, IPruneable /// The cache pruner. public ActivationCache(ICachePruner cachePruner) { + Ensure.ArgumentNotNull(cachePruner, "cachePruner"); cachePruner.Start(this); } - + /// /// Gets the activated object count. /// @@ -65,7 +74,7 @@ public int DeactivatedObjectCount return this.deactivatedObjects.Count; } } - + /// /// Clears the cache. /// @@ -90,11 +99,7 @@ public void AddActivatedInstance(object instance) { lock (this.activatedObjects) { -#if SILVERLIGHT_20 || SILVERLIGHT_30 || WINDOWS_PHONE || NETCF || MONO - this.activatedObjects.Add(new ReferenceEqualWeakReference(instance), true); -#else this.activatedObjects.Add(new ReferenceEqualWeakReference(instance)); -#endif } } @@ -106,11 +111,7 @@ public void AddDeactivatedInstance(object instance) { lock (this.deactivatedObjects) { -#if SILVERLIGHT_20 || SILVERLIGHT_30 || WINDOWS_PHONE || NETCF || MONO - this.deactivatedObjects.Add(new ReferenceEqualWeakReference(instance), true); -#else this.deactivatedObjects.Add(new ReferenceEqualWeakReference(instance)); -#endif } } @@ -123,11 +124,7 @@ public void AddDeactivatedInstance(object instance) /// public bool IsActivated(object instance) { -#if SILVERLIGHT_20 || SILVERLIGHT_30 || WINDOWS_PHONE || NETCF || MONO - return this.activatedObjects.ContainsKey(instance); -#else return this.activatedObjects.Contains(instance); -#endif } /// @@ -139,11 +136,7 @@ public bool IsActivated(object instance) /// public bool IsDeactivated(object instance) { -#if SILVERLIGHT_20 || SILVERLIGHT_30 || WINDOWS_PHONE || NETCF || MONO - return this.deactivatedObjects.ContainsKey(instance); -#else return this.deactivatedObjects.Contains(instance); -#endif } /// @@ -162,20 +155,6 @@ public void Prune() } } -#if SILVERLIGHT_20 || SILVERLIGHT_30 || WINDOWS_PHONE || NETCF || MONO - /// - /// Removes all dead objects. - /// - /// The objects collection to be freed of dead objects. - private static void RemoveDeadObjects(IDictionary objects) - { - var deadObjects = objects.Where(entry => !((ReferenceEqualWeakReference)entry.Key).IsAlive).ToList(); - foreach (var deadObject in deadObjects) - { - objects.Remove(deadObject.Key); - } - } -#else /// /// Removes all dead objects. /// @@ -184,6 +163,5 @@ private static void RemoveDeadObjects(HashSet objects) { objects.RemoveWhere(reference => !((ReferenceEqualWeakReference)reference).IsAlive); } -#endif } -} \ No newline at end of file +} diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/Cache.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/Cache.cs index 25a0857c..c2f603a5 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/Cache.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/Cache.cs @@ -1,12 +1,23 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation.Caching { @@ -60,10 +71,10 @@ public int Count /// /// Releases resources held by the object. /// - /// + /// True if called manually, otherwise by GC. public override void Dispose(bool disposing) { - if (disposing && !IsDisposed) + if (disposing && !this.IsDisposed) { this.Clear(); } @@ -89,8 +100,7 @@ public void Remember(IContext context, InstanceReference reference) if (!this.entries.ContainsKey(weakScopeReference)) { this.entries[weakScopeReference] = new Multimap(); - var notifyScope = scope as INotifyWhenDisposed; - if (notifyScope != null) + if (scope is INotifyWhenDisposed notifyScope) { notifyScope.Disposed += (o, e) => this.Clear(weakScopeReference); } @@ -108,6 +118,7 @@ public void Remember(IContext context, InstanceReference reference) public object TryGet(IContext context) { Ensure.ArgumentNotNull(context, "context"); + var scope = context.GetScope(); if (scope == null) { @@ -116,8 +127,7 @@ public object TryGet(IContext context) lock (this.entries) { - Multimap bindings; - if (!this.entries.TryGetValue(scope, out bindings)) + if (!this.entries.TryGetValue(scope, out Multimap bindings)) { return null; } @@ -149,7 +159,7 @@ public object TryGet(IContext context) /// if the instance was found and released; otherwise . public bool Release(object instance) { - lock(this.entries) + lock (this.entries) { var instanceFound = false; foreach (var bindingEntry in this.entries.Values.SelectMany(bindingEntries => bindingEntries.Values).ToList()) @@ -177,8 +187,8 @@ public void Prune() var disposedScopes = this.entries.Where(scope => !((ReferenceEqualWeakReference)scope.Key).IsAlive).Select(scope => scope).ToList(); foreach (var disposedScope in disposedScopes) { - this.Forget(GetAllBindingEntries(disposedScope.Value)); this.entries.Remove(disposedScope.Key); + this.Forget(GetAllBindingEntries(disposedScope.Value)); } } } @@ -192,11 +202,10 @@ public void Clear(object scope) { lock (this.entries) { - Multimap bindings; - if (this.entries.TryGetValue(scope, out bindings)) + if (this.entries.TryGetValue(scope, out Multimap bindings)) { - this.Forget(GetAllBindingEntries(bindings)); this.entries.Remove(scope); + this.Forget(GetAllBindingEntries(bindings)); } } } @@ -214,13 +223,13 @@ public void Clear() } /// - /// Gets all entries for a binding withing the selected scope. + /// Gets all entries for a binding within the selected scope. /// /// The bindings. /// All bindings of a binding. - private static IEnumerable GetAllBindingEntries(IEnumerable>> bindings) + private static IEnumerable GetAllBindingEntries(Multimap bindings) { - return bindings.SelectMany(bindingEntries => bindingEntries.Value); + return bindings.Values.SelectMany(bindingEntries => bindingEntries); } /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/GarbageCollectionCachePruner.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/GarbageCollectionCachePruner.cs index 60e4a588..a4f3b748 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/GarbageCollectionCachePruner.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/GarbageCollectionCachePruner.cs @@ -1,18 +1,30 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation.Caching { using System; using System.Collections.Generic; using System.Threading; + using Telerik.JustMock.AutoMock.Ninject.Components; using Telerik.JustMock.AutoMock.Ninject.Infrastructure; using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; @@ -27,25 +39,29 @@ public class GarbageCollectionCachePruner : NinjectComponent, ICachePruner /// indicator for if GC has been run. /// private readonly WeakReference indicator = new WeakReference(new object()); - + /// /// The caches that are being pruned. /// private readonly List caches = new List(); /// - /// The timer used to trigger the cache pruning + /// The timer used to trigger the cache pruning. /// private Timer timer; + /// + /// The flag to indicate whether the cache pruning is stopped or not. + /// private bool stop; /// /// Releases resources held by the object. /// + /// True if called manually, otherwise by GC. public override void Dispose(bool disposing) { - if (disposing && !IsDisposed && this.timer != null) + if (disposing && !this.IsDisposed && this.timer != null) { this.Stop(); } @@ -80,13 +96,8 @@ public void Stop() using (var signal = new ManualResetEvent(false)) { -#if !NETCF this.timer.Dispose(signal); signal.WaitOne(); -#else - this.timer.Dispose(); -#endif - this.timer = null; this.caches.Clear(); } @@ -120,7 +131,7 @@ private void PruneCacheIfGarbageCollectorHasRun(object state) private int GetTimeoutInMilliseconds() { - TimeSpan interval = Settings.CachePruningInterval; + var interval = this.Settings.CachePruningInterval; return interval == TimeSpan.MaxValue ? -1 : (int)interval.TotalMilliseconds; } } diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/IActivationCache.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/IActivationCache.cs index 728363ad..c1729b5e 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/IActivationCache.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/IActivationCache.cs @@ -1,4 +1,25 @@ -namespace Telerik.JustMock.AutoMock.Ninject.Activation.Caching +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- + +namespace Telerik.JustMock.AutoMock.Ninject.Activation.Caching { using Telerik.JustMock.AutoMock.Ninject.Components; @@ -23,7 +44,7 @@ public interface IActivationCache : INinjectComponent /// /// The instance to be added. void AddDeactivatedInstance(object instance); - + /// /// Determines whether the specified instance is activated. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/ICache.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/ICache.cs index 2f74f6d0..83c0c24d 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/ICache.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/ICache.cs @@ -1,19 +1,28 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Components; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation.Caching { + using Telerik.JustMock.AutoMock.Ninject.Components; + /// /// Tracks instances for re-use in certain scopes. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/ICachePruner.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/ICachePruner.cs index a3be302d..7d6b1349 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/ICachePruner.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/ICachePruner.cs @@ -1,19 +1,28 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Components; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation.Caching { + using Telerik.JustMock.AutoMock.Ninject.Components; + /// /// Prunes instances from an based on environmental information. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/IPruneable.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/IPruneable.cs index dc5e2fd0..df233b8f 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/IPruneable.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/IPruneable.cs @@ -1,7 +1,28 @@ +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- + namespace Telerik.JustMock.AutoMock.Ninject.Activation.Caching { /// - /// An object that is prunealble. + /// An object that is pruneable. /// public interface IPruneable { diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/WeakReferenceEqualityComparer.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/WeakReferenceEqualityComparer.cs index 865e002f..13e79874 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/WeakReferenceEqualityComparer.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Caching/WeakReferenceEqualityComparer.cs @@ -1,3 +1,24 @@ +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- + namespace Telerik.JustMock.AutoMock.Ninject.Activation.Caching { using System.Collections.Generic; @@ -11,7 +32,7 @@ namespace Telerik.JustMock.AutoMock.Ninject.Activation.Caching public class WeakReferenceEqualityComparer : IEqualityComparer { /// - /// Returns if the specifed objects are equal. + /// Returns if the specified objects are equal. /// /// The first object. /// The second object. @@ -29,12 +50,7 @@ public class WeakReferenceEqualityComparer : IEqualityComparer public int GetHashCode(object obj) { var weakReference = obj as ReferenceEqualWeakReference; - return weakReference != null ? weakReference.GetHashCode() : -#if !NETCF - RuntimeHelpers.GetHashCode(obj); -#else - obj.GetHashCode(); -#endif + return weakReference != null ? weakReference.GetHashCode() : RuntimeHelpers.GetHashCode(obj); } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Context.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Context.cs index 7fe83c69..790c2099 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Context.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Context.cs @@ -1,45 +1,91 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using System.Linq; -using Telerik.JustMock.AutoMock.Ninject.Activation.Caching; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Introspection; -using Telerik.JustMock.AutoMock.Ninject.Parameters; -using Telerik.JustMock.AutoMock.Ninject.Planning; -using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation { + using System; + using System.Collections.Generic; + using System.Linq; + + using Telerik.JustMock.AutoMock.Ninject.Activation.Caching; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Introspection; + using Telerik.JustMock.AutoMock.Ninject.Parameters; + using Telerik.JustMock.AutoMock.Ninject.Planning; + using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; + using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; + /// /// Contains information about the activation of a single instance. /// public class Context : IContext { - private WeakReference cachedScope; + private object cachedScope; + + /// + /// Initializes a new instance of the class. + /// + /// The kernel managing the resolution. + /// The context's request. + /// The context's binding. + /// The cache component. + /// The planner component. + /// The pipeline component. + public Context(IKernel kernel, IRequest request, IBinding binding, ICache cache, IPlanner planner, IPipeline pipeline) + { + Ensure.ArgumentNotNull(kernel, "kernel"); + Ensure.ArgumentNotNull(request, "request"); + Ensure.ArgumentNotNull(binding, "binding"); + Ensure.ArgumentNotNull(cache, "cache"); + Ensure.ArgumentNotNull(planner, "planner"); + Ensure.ArgumentNotNull(pipeline, "pipeline"); + + this.Kernel = kernel; + this.Request = request; + this.Binding = binding; + this.Parameters = request.Parameters.Union(binding.Parameters).ToList(); + + this.Cache = cache; + this.Planner = planner; + this.Pipeline = pipeline; + + if (binding.Service.IsGenericTypeDefinition) + { + this.HasInferredGenericArguments = true; + this.GenericArguments = request.Service.GenericTypeArguments; + } + } /// - /// Gets the kernel that is driving the activation. + /// Gets or sets the kernel that is driving the activation. /// public IKernel Kernel { get; set; } /// - /// Gets the request. + /// Gets or sets the request. /// public IRequest Request { get; set; } /// - /// Gets the binding. + /// Gets or sets the binding. /// public IBinding Binding { get; set; } @@ -49,7 +95,7 @@ public class Context : IContext public IPlan Plan { get; set; } /// - /// Gets the parameters that were passed to manipulate the activation process. + /// Gets or sets the parameters that were passed to manipulate the activation process. /// public ICollection Parameters { get; set; } @@ -64,67 +110,27 @@ public class Context : IContext public bool HasInferredGenericArguments { get; private set; } /// - /// Gets or sets the cache component. + /// Gets the cache component. /// public ICache Cache { get; private set; } /// - /// Gets or sets the planner component. + /// Gets the planner component. /// public IPlanner Planner { get; private set; } /// - /// Gets or sets the pipeline component. + /// Gets the pipeline component. /// public IPipeline Pipeline { get; private set; } - /// - /// Initializes a new instance of the class. - /// - /// The kernel managing the resolution. - /// The context's request. - /// The context's binding. - /// The cache component. - /// The planner component. - /// The pipeline component. - public Context(IKernel kernel, IRequest request, IBinding binding, ICache cache, IPlanner planner, IPipeline pipeline) - { - Ensure.ArgumentNotNull(kernel, "kernel"); - Ensure.ArgumentNotNull(request, "request"); - Ensure.ArgumentNotNull(binding, "binding"); - Ensure.ArgumentNotNull(cache, "cache"); - Ensure.ArgumentNotNull(planner, "planner"); - Ensure.ArgumentNotNull(pipeline, "pipeline"); - - Kernel = kernel; - Request = request; - Binding = binding; - Parameters = request.Parameters.Union(binding.Parameters).ToList(); - - Cache = cache; - Planner = planner; - Pipeline = pipeline; - - if (binding.Service.IsGenericTypeDefinition) - { - HasInferredGenericArguments = true; - GenericArguments = request.Service.GetGenericArguments(); - } - } - /// /// Gets the scope for the context that "owns" the instance activated therein. /// /// The object that acts as the scope. public object GetScope() { - if (this.cachedScope == null) - { - var scope = this.Request.GetScope() ?? this.Binding.GetScope(this); - this.cachedScope = new WeakReference(scope); - } - - return this.cachedScope.Target; + return this.cachedScope ?? this.Request.GetScope() ?? this.Binding.GetScope(this); } /// @@ -133,7 +139,7 @@ public object GetScope() /// The provider that should be used. public IProvider GetProvider() { - return Binding.GetProvider(this); + return this.Binding.GetProvider(this); } /// @@ -142,47 +148,112 @@ public IProvider GetProvider() /// The resolved instance. public object Resolve() { - lock (Binding) + if (this.Request.ActiveBindings.Contains(this.Binding) && + this.IsCyclical(this.Request.ParentContext)) + { + throw new ActivationException(ExceptionFormatter.CyclicalDependenciesDetected(this)); + } + + try + { + this.cachedScope = this.Request.GetScope() ?? this.Binding.GetScope(this); + + if (this.cachedScope != null) + { + lock (this.cachedScope) + { + return this.ResolveInternal(this.cachedScope); + } + } + else + { + return this.ResolveInternal(null); + } + } + finally { - if (Request.ActiveBindings.Contains(Binding)) - throw new ActivationException(ExceptionFormatter.CyclicalDependenciesDetected(this)); + this.cachedScope = null; + } + } - var cachedInstance = Cache.TryGet(this); + private object ResolveInternal(object scope) + { + var cachedInstance = this.Cache.TryGet(this); - if (cachedInstance != null) - return cachedInstance; + if (cachedInstance != null) + { + return cachedInstance; + } - Request.ActiveBindings.Push(Binding); + this.Request.ActiveBindings.Push(this.Binding); - var reference = new InstanceReference { Instance = GetProvider().Create(this) }; + var reference = new InstanceReference { Instance = this.GetProvider().Create(this) }; - Request.ActiveBindings.Pop(); + this.Request.ActiveBindings.Pop(); - if (reference.Instance == null) + if (reference.Instance == null) + { + if (!this.Kernel.Settings.AllowNullInjection) { - if (!this.Kernel.Settings.AllowNullInjection) - { - throw new ActivationException(ExceptionFormatter.ProviderReturnedNull(this)); - } + throw new ActivationException(ExceptionFormatter.ProviderReturnedNull(this)); + } - if (this.Plan == null) - { - this.Plan = this.Planner.GetPlan(this.Request.Service); - } + if (this.Plan == null) + { + this.Plan = this.Planner.GetPlan(this.Request.Service); + } + + return null; + } - return null; + if (scope != null) + { + this.Cache.Remember(this, reference); + } + + if (this.Plan == null) + { + this.Plan = this.Planner.GetPlan(reference.Instance.GetType()); + } + + try + { + this.Pipeline.Activate(this, reference); + } + catch (ActivationException) + { + if (scope != null) + { + this.Cache.Release(reference.Instance); } - if (GetScope() != null) - Cache.Remember(this, reference); + throw; + } + + return reference.Instance; + } - if (Plan == null) - Plan = Planner.GetPlan(reference.Instance.GetType()); + private bool IsCyclical(IContext targetContext) + { + if (targetContext == null) + { + return false; + } - Pipeline.Activate(this, reference); + if (targetContext.Request.Service == this.Request.Service) + { + if ((this.Request.Target is ParameterTarget && targetContext.Request.Target is ParameterTarget) || targetContext.GetScope() != this.GetScope() || this.GetScope() == null) + { + return true; + } + } - return reference.Instance; + if (this.IsCyclical(targetContext.Request.ParentContext)) + { + return true; } + + return false; } } -} \ No newline at end of file +} diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/IContext.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/IContext.cs index e8d4a22c..e3577d3f 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/IContext.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/IContext.cs @@ -1,22 +1,34 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using Telerik.JustMock.AutoMock.Ninject.Parameters; -using Telerik.JustMock.AutoMock.Ninject.Planning; -using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation { + using System; + using System.Collections.Generic; + + using Telerik.JustMock.AutoMock.Ninject.Activation.Caching; + using Telerik.JustMock.AutoMock.Ninject.Parameters; + using Telerik.JustMock.AutoMock.Ninject.Planning; + using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; + /// /// Contains information about the activation of a single instance. /// @@ -42,6 +54,11 @@ public interface IContext /// IPlan Plan { get; set; } + /// + /// Gets the cache component. + /// + ICache Cache { get; } + /// /// Gets the parameters that were passed to manipulate the activation process. /// @@ -75,4 +92,4 @@ public interface IContext /// The resolved instance. object Resolve(); } -} \ No newline at end of file +} diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/IPipeline.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/IPipeline.cs index 7205b292..c6f21dfe 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/IPipeline.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/IPipeline.cs @@ -1,21 +1,31 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using Telerik.JustMock.AutoMock.Ninject.Activation.Strategies; -using Telerik.JustMock.AutoMock.Ninject.Components; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation { + using System.Collections.Generic; + + using Telerik.JustMock.AutoMock.Ninject.Activation.Strategies; + using Telerik.JustMock.AutoMock.Ninject.Components; + /// /// Drives the activation (injection, etc.) of an instance. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/IProvider.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/IProvider.cs index b553fe3a..64dd4d5c 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/IProvider.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/IProvider.cs @@ -1,18 +1,28 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation { + using System; + /// /// Creates instances of services. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/IProvider{T}.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/IProvider{T}.cs index 4eb8ab7b..e4353729 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/IProvider{T}.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/IProvider{T}.cs @@ -1,10 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -17,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation { @@ -26,6 +26,6 @@ namespace Telerik.JustMock.AutoMock.Ninject.Activation /// /// The type provides by this implementation. public interface IProvider : IProvider - { + { } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/IRequest.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/IRequest.cs index eaeebfbf..bde0e574 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/IRequest.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/IRequest.cs @@ -1,23 +1,33 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using Telerik.JustMock.AutoMock.Ninject.Parameters; -using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; -using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; -using Telerik.JustMock.AutoMock.Ninject.Syntax; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation { + using System; + using System.Collections.Generic; + + using Telerik.JustMock.AutoMock.Ninject.Parameters; + using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; + using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; + /// /// Describes the request for a service resolution. /// @@ -64,15 +74,22 @@ public interface IRequest int Depth { get; } /// - /// Gets or sets value indicating whether the request is optional. + /// Gets or sets a value indicating whether the request is optional. /// bool IsOptional { get; set; } /// - /// Gets or sets value indicating whether the request should return a unique result. + /// Gets or sets a value indicating whether the request should return a unique result. /// bool IsUnique { get; set; } + /// + /// Gets or sets a value indicating whether the request should force to return a unique value even if the request is optional. + /// If this value is set true the request will throw an ActivationException if there are multiple satisfying bindings rather + /// than returning null for the request is optional. For none optional requests this parameter does not change anything. + /// + bool ForceUnique { get; set; } + /// /// Determines whether the specified binding satisfies the constraint defined on this request. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/InstanceReference.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/InstanceReference.cs index a94cfec9..4a732533 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/InstanceReference.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/InstanceReference.cs @@ -1,22 +1,29 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using Telerik.JustMock.AutoMock.Ninject.Parameters; -using Telerik.JustMock.AutoMock.Ninject.Planning; -using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation { + using System; + using System.Security; + /// /// Holds an instance during activation or after it has been cached. /// @@ -32,9 +39,17 @@ public class InstanceReference /// /// The type in question. /// if the instance is of the specified type, otherwise . + [SecuritySafeCritical] public bool Is() { - return Instance is T; +#if !NO_REMOTING + if (System.Runtime.Remoting.RemotingServices.IsTransparentProxy(this.Instance) + && System.Runtime.Remoting.RemotingServices.GetRealProxy(this.Instance).GetType().Name == "RemotingProxy") + { + return typeof(T).IsAssignableFrom(this.Instance.GetType()); + } +#endif + return this.Instance is T; } /// @@ -44,7 +59,7 @@ public bool Is() /// The instance. public T As() { - return (T)Instance; + return (T)this.Instance; } /// @@ -54,8 +69,10 @@ public T As() /// The action to execute. public void IfInstanceIs(Action action) { - if (Instance is T) - action((T)Instance); + if (this.Is()) + { + action((T)this.Instance); + } } } -} \ No newline at end of file +} diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Pipeline.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Pipeline.cs index fde8a8ae..13ca8a10 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Pipeline.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Pipeline.cs @@ -1,17 +1,29 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation { using System.Collections.Generic; using System.Linq; + using Telerik.JustMock.AutoMock.Ninject.Activation.Caching; using Telerik.JustMock.AutoMock.Ninject.Activation.Strategies; using Telerik.JustMock.AutoMock.Ninject.Components; @@ -36,6 +48,8 @@ public class Pipeline : NinjectComponent, IPipeline public Pipeline(IEnumerable strategies, IActivationCache activationCache) { Ensure.ArgumentNotNull(strategies, "strategies"); + Ensure.ArgumentNotNull(activationCache, "activationCache"); + this.Strategies = strategies.ToList(); this.activationCache = activationCache; } @@ -53,6 +67,8 @@ public Pipeline(IEnumerable strategies, IActivationCache ac public void Activate(IContext context, InstanceReference reference) { Ensure.ArgumentNotNull(context, "context"); + Ensure.ArgumentNotNull(reference, "reference"); + if (!this.activationCache.IsActivated(reference.Instance)) { this.Strategies.Map(s => s.Activate(context, reference)); @@ -67,6 +83,8 @@ public void Activate(IContext context, InstanceReference reference) public void Deactivate(IContext context, InstanceReference reference) { Ensure.ArgumentNotNull(context, "context"); + Ensure.ArgumentNotNull(reference, "reference"); + if (!this.activationCache.IsDeactivated(reference.Instance)) { this.Strategies.Map(s => s.Deactivate(context, reference)); diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Provider.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Provider.cs index a67889a1..6e61ac36 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Provider.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Provider.cs @@ -1,16 +1,28 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation { using System; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; /// @@ -35,6 +47,7 @@ public virtual Type Type public object Create(IContext context) { Ensure.ArgumentNotNull(context, "context"); + return this.CreateInstance(context); } diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Providers/CallbackProvider.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Providers/CallbackProvider.cs index 58cba4de..7b022b11 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Providers/CallbackProvider.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Providers/CallbackProvider.cs @@ -1,19 +1,30 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation.Providers { + using System; + + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + /// /// A provider that delegates to a callback method to create instances. /// @@ -21,20 +32,21 @@ namespace Telerik.JustMock.AutoMock.Ninject.Activation.Providers public class CallbackProvider : Provider { /// - /// Gets the callback method used by the provider. - /// - public Func Method { get; private set; } - - /// - /// Initializes a new instance of the CallbackProvider<T> class. + /// Initializes a new instance of the class. /// /// The callback method that will be called to create instances. public CallbackProvider(Func method) { Ensure.ArgumentNotNull(method, "method"); - Method = method; + + this.Method = method; } + /// + /// Gets the callback method used by the provider. + /// + public Func Method { get; private set; } + /// /// Invokes the callback method to create an instance. /// @@ -42,7 +54,7 @@ public CallbackProvider(Func method) /// The created instance. protected override T CreateInstance(IContext context) { - return Method(context); + return this.Method(context); } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Providers/ConstantProvider.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Providers/ConstantProvider.cs index 908a4ebf..ff6a4ec6 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Providers/ConstantProvider.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Providers/ConstantProvider.cs @@ -1,16 +1,23 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation.Providers { @@ -21,19 +28,19 @@ namespace Telerik.JustMock.AutoMock.Ninject.Activation.Providers public class ConstantProvider : Provider { /// - /// Gets the value that the provider will return. - /// - public T Value { get; private set; } - - /// - /// Initializes a new instance of the ConstantProvider<T> class. + /// Initializes a new instance of the class. /// /// The value that the provider should return. public ConstantProvider(T value) { - Value = value; + this.Value = value; } + /// + /// Gets the value that the provider will return. + /// + public T Value { get; private set; } + /// /// Creates an instance within the specified context. /// @@ -41,7 +48,7 @@ public ConstantProvider(T value) /// The constant value this provider returns. protected override T CreateInstance(IContext context) { - return Value; + return this.Value; } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Providers/StandardProvider.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Providers/StandardProvider.cs index 90179d26..c5f963e3 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Providers/StandardProvider.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Providers/StandardProvider.cs @@ -1,67 +1,103 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Linq; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Introspection; -using Telerik.JustMock.AutoMock.Ninject.Parameters; -using Telerik.JustMock.AutoMock.Ninject.Planning; -using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; -using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; -using Telerik.JustMock.AutoMock.Ninject.Selection; - -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation.Providers { + using System; + using System.Linq; using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Introspection; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; + using Telerik.JustMock.AutoMock.Ninject.Parameters; + using Telerik.JustMock.AutoMock.Ninject.Planning; + using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; + using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; + using Telerik.JustMock.AutoMock.Ninject.Selection; using Telerik.JustMock.AutoMock.Ninject.Selection.Heuristics; - using Telerik.JustMock.Core; + using Telerik.JustMock.Core; /// /// The standard provider for types, which activates instances via a . /// public class StandardProvider : IProvider { + /// + /// Initializes a new instance of the class. + /// + /// The type (or prototype) of instances the provider creates. + /// The planner component. + /// The constructor scorer component. + public StandardProvider(Type type, IPlanner planner, IConstructorScorer constructorScorer) + { + Ensure.ArgumentNotNull(type, "type"); + Ensure.ArgumentNotNull(planner, "planner"); + Ensure.ArgumentNotNull(constructorScorer, "constructorScorer"); + + this.Type = type; + this.Planner = planner; + this.ConstructorScorer = constructorScorer; + } + /// /// Gets the type (or prototype) of instances the provider creates. /// public Type Type { get; private set; } /// - /// Gets or sets the planner component. + /// Gets the planner component. /// public IPlanner Planner { get; private set; } /// - /// Gets or sets the selector component. + /// Gets the constructor scorer component. /// public IConstructorScorer ConstructorScorer { get; private set; } /// - /// Initializes a new instance of the class. + /// Gets a callback that creates an instance of the + /// for the specified type. /// - /// The type (or prototype) of instances the provider creates. - /// The planner component. - /// The constructor scorer component. - public StandardProvider(Type type, IPlanner planner, IConstructorScorer constructorScorer - ) + /// The prototype the provider instance will create. + /// The created callback. + public static Func GetCreationCallback(Type prototype) { - Ensure.ArgumentNotNull(type, "type"); - Ensure.ArgumentNotNull(planner, "planner"); - Ensure.ArgumentNotNull(constructorScorer, "constructorScorer"); + Ensure.ArgumentNotNull(prototype, "prototype"); - Type = type; - Planner = planner; - ConstructorScorer = constructorScorer; + return ctx => new StandardProvider(prototype, ctx.Kernel.Components.Get(), ctx.Kernel.Components.Get().ConstructorScorer); + } + + /// + /// Gets a callback that creates an instance of the + /// for the specified type and constructor. + /// + /// The prototype the provider instance will create. + /// The constructor. + /// The created callback. + public static Func GetCreationCallback(Type prototype, ConstructorInfo constructor) + { + Ensure.ArgumentNotNull(prototype, "prototype"); + + return ctx => new StandardProvider(prototype, ctx.Kernel.Components.Get(), new SpecificConstructorSelector(constructor)); } /// @@ -78,25 +114,18 @@ public virtual object Create(IContext context) context.Plan = this.Planner.GetPlan(this.GetImplementationType(context.Request.Service)); } - if (!context.Plan.Has()) - { - throw new ActivationException(ExceptionFormatter.NoConstructorsAvailable(context)); - } + var directive = this.DetermineConstructorInjectionDirective(context); + + var arguments = directive.Targets.Select(target => this.GetValue(context, target)).ToArray(); - var directives = context.Plan.GetAll(); - var bestDirectives = directives - .GroupBy(option => this.ConstructorScorer.Score(context, option)) - .OrderByDescending(g => g.Key) - .First(); - if (bestDirectives.Skip(1).Any()) + var cachedInstance = context.Cache.TryGet(context); + + if (cachedInstance != null) { - throw new ActivationException(ExceptionFormatter.ConstructorsAmbiguous(context, bestDirectives)); + return cachedInstance; } - var directive = bestDirectives.Single(); - var arguments = directive.Targets.Select(target => this.GetValue(context, target)).ToArray(); - var injector = directive.Injector; - return ProfilerInterceptor.GuardExternal(() => injector(arguments)); + return ProfilerInterceptor.GuardExternal(() => directive.Injector(arguments)); } /// @@ -112,7 +141,7 @@ public object GetValue(IContext context, ITarget target) var parameter = context .Parameters.OfType() - .Where(p => p.AppliesToTarget(context, target)).SingleOrDefault(); + .SingleOrDefault(p => p.AppliesToTarget(context, target)); return parameter != null ? parameter.GetValue(context, target) : target.ResolveWithin(context); } @@ -125,32 +154,30 @@ public object GetValue(IContext context, ITarget target) public Type GetImplementationType(Type service) { Ensure.ArgumentNotNull(service, "service"); - return Type.ContainsGenericParameters ? Type.MakeGenericType(service.GetGenericArguments()) : Type; - } - /// - /// Gets a callback that creates an instance of the - /// for the specified type. - /// - /// The prototype the provider instance will create. - /// The created callback. - public static Func GetCreationCallback(Type prototype) - { - Ensure.ArgumentNotNull(prototype, "prototype"); - return ctx => new StandardProvider(prototype, ctx.Kernel.Components.Get(), ctx.Kernel.Components.Get().ConstructorScorer); + return this.Type.ContainsGenericParameters ? this.Type.MakeGenericType(service.GetGenericArguments()) : this.Type; } - /// - /// Gets a callback that creates an instance of the - /// for the specified type and constructor. - /// - /// The prototype the provider instance will create. - /// The constructor. - /// The created callback. - public static Func GetCreationCallback(Type prototype, ConstructorInfo constructor) + private ConstructorInjectionDirective DetermineConstructorInjectionDirective(IContext context) { - Ensure.ArgumentNotNull(prototype, "prototype"); - return ctx => new StandardProvider(prototype, ctx.Kernel.Components.Get(), new SpecificConstructorSelector(constructor)); + var directives = context.Plan.ConstructorInjectionDirectives; + if (directives.Count == 1) + { + return directives[0]; + } + + var bestDirectives = + directives + .GroupBy(option => this.ConstructorScorer.Score(context, option)) + .OrderByDescending(g => g.Key) + .FirstOrDefault(); + if (bestDirectives == null) + { + throw new ActivationException(ExceptionFormatter.NoConstructorsAvailable(context)); + } + + return bestDirectives.SingleOrThrowException( + () => new ActivationException(ExceptionFormatter.ConstructorsAmbiguous(context, bestDirectives))); } } -} \ No newline at end of file +} diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Request.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Request.cs index 0330c786..0ab68685 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Request.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Request.cs @@ -1,29 +1,82 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using System.Linq; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Parameters; -using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; -using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation { + using System; + using System.Collections.Generic; + using System.Linq; + + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Introspection; + using Telerik.JustMock.AutoMock.Ninject.Parameters; + using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; + using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; + /// /// Describes the request for a service resolution. /// public class Request : IRequest { + /// + /// Initializes a new instance of the class. + /// + /// The service that was requested. + /// The constraint that will be applied to filter the bindings used for the request. + /// The parameters that affect the resolution. + /// The scope callback, if an external scope was specified. + /// True if the request is optional; otherwise, false. + /// True if the request should return a unique result; otherwise, false. + public Request(Type service, Func constraint, IEnumerable parameters, Func scopeCallback, bool isOptional, bool isUnique) + { + this.Service = service; + this.Constraint = constraint; + this.Parameters = parameters.ToList(); + this.ScopeCallback = scopeCallback; + this.ActiveBindings = new Stack(); + this.Depth = 0; + this.IsOptional = isOptional; + this.IsUnique = isUnique; + } + + /// + /// Initializes a new instance of the class. + /// + /// The parent context. + /// The service that was requested. + /// The target that will receive the injection. + /// The scope callback, if an external scope was specified. + public Request(IContext parentContext, Type service, ITarget target, Func scopeCallback) + { + this.ParentContext = parentContext; + this.ParentRequest = parentContext.Request; + this.Service = service; + this.Target = target; + this.Constraint = target.Constraint; + this.IsOptional = target.IsOptional; + this.Parameters = parentContext.Parameters.Where(p => p.ShouldInherit).ToList(); + this.ScopeCallback = scopeCallback; + this.ActiveBindings = new Stack(this.ParentRequest.ActiveBindings); + this.Depth = this.ParentRequest.Depth + 1; + } + /// /// Gets the service that was requested. /// @@ -65,12 +118,12 @@ public class Request : IRequest public int Depth { get; private set; } /// - /// Gets or sets value indicating whether the request is optional. + /// Gets or sets a value indicating whether the request is optional. /// public bool IsOptional { get; set; } /// - /// Gets or sets value indicating whether the request is for a single service. + /// Gets or sets a value indicating whether the request is for a single service. /// public bool IsUnique { @@ -78,58 +131,19 @@ public bool IsUnique } /// - /// Gets the callback that resolves the scope for the request, if an external scope was provided. + /// Gets or sets a value indicating whether the request should force to return a unique value even if the request is optional. + /// If this value is set true the request will throw an ActivationException if there are multiple satisfying bindings rather + /// than returning null for the request is optional. For none optional requests this parameter does not change anything. /// - public Func ScopeCallback { get; private set; } - - /// - /// Initializes a new instance of the class. - /// - /// The service that was requested. - /// The constraint that will be applied to filter the bindings used for the request. - /// The parameters that affect the resolution. - /// The scope callback, if an external scope was specified. - /// True if the request is optional; otherwise, false. - /// True if the request should return a unique result; otherwise, false. - public Request(Type service, Func constraint, IEnumerable parameters, Func scopeCallback, bool isOptional, bool isUnique) + public bool ForceUnique { - Ensure.ArgumentNotNull(service, "service"); - Ensure.ArgumentNotNull(parameters, "parameters"); - - Service = service; - Constraint = constraint; - Parameters = parameters.ToList(); - ScopeCallback = scopeCallback; - ActiveBindings = new Stack(); - Depth = 0; - IsOptional = isOptional; - IsUnique = isUnique; + get; set; } /// - /// Initializes a new instance of the class. + /// Gets the callback that resolves the scope for the request, if an external scope was provided. /// - /// The parent context. - /// The service that was requested. - /// The target that will receive the injection. - /// The scope callback, if an external scope was specified. - public Request(IContext parentContext, Type service, ITarget target, Func scopeCallback) - { - Ensure.ArgumentNotNull(parentContext, "parentContext"); - Ensure.ArgumentNotNull(service, "service"); - Ensure.ArgumentNotNull(target, "target"); - - ParentContext = parentContext; - ParentRequest = parentContext.Request; - Service = service; - Target = target; - Constraint = target.Constraint; - IsOptional = target.IsOptional; - Parameters = parentContext.Parameters.Where(p => p.ShouldInherit).ToList(); - ScopeCallback = scopeCallback; - ActiveBindings = new Stack(ParentRequest.ActiveBindings); - Depth = ParentRequest.Depth + 1; - } + public Func ScopeCallback { get; private set; } /// /// Determines whether the specified binding satisfies the constraints defined on this request. @@ -138,7 +152,7 @@ public Request(IContext parentContext, Type service, ITarget target, FuncTrue if the binding satisfies the constraints; otherwise false. public bool Matches(IBinding binding) { - return Constraint == null || Constraint(binding.Metadata); + return this.Constraint == null || this.Constraint(binding.Metadata); } /// @@ -147,7 +161,7 @@ public bool Matches(IBinding binding) /// The object that acts as the scope. public object GetScope() { - return ScopeCallback == null ? null : ScopeCallback(); + return this.ScopeCallback == null ? null : this.ScopeCallback(); } /// @@ -159,7 +173,16 @@ public object GetScope() /// The child request. public IRequest CreateChild(Type service, IContext parentContext, ITarget target) { - return new Request(parentContext, service, target, ScopeCallback); + return new Request(parentContext, service, target, this.ScopeCallback); + } + + /// + /// Formats this object into a meaningful string representation. + /// + /// The request formatted as string. + public override string ToString() + { + return this.Format(); } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/ActivationCacheStrategy.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/ActivationCacheStrategy.cs index 35a3e244..07ec230b 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/ActivationCacheStrategy.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/ActivationCacheStrategy.cs @@ -1,11 +1,34 @@ +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- + namespace Telerik.JustMock.AutoMock.Ninject.Activation.Strategies { using Telerik.JustMock.AutoMock.Ninject.Activation.Caching; + using Telerik.JustMock.AutoMock.Ninject.Components; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; /// /// Adds all activated instances to the activation cache. /// - public class ActivationCacheStrategy : IActivationStrategy + public class ActivationCacheStrategy : NinjectComponent, IActivationStrategy { /// /// The activation cache. @@ -18,20 +41,9 @@ public class ActivationCacheStrategy : IActivationStrategy /// The activation cache. public ActivationCacheStrategy(IActivationCache activationCache) { - this.activationCache = activationCache; - } + Ensure.ArgumentNotNull(activationCache, "activationCache"); - /// - /// Gets or sets the settings. - /// - /// The ninject settings. - public INinjectSettings Settings { get; set; } - - /// - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// - public void Dispose() - { + this.activationCache = activationCache; } /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/ActivationStrategy.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/ActivationStrategy.cs index 67d2dea8..f62ebc13 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/ActivationStrategy.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/ActivationStrategy.cs @@ -1,19 +1,28 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Components; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation.Strategies { + using Telerik.JustMock.AutoMock.Ninject.Components; + /// /// Contributes to a , and is called during the activation /// and deactivation of an instance. @@ -25,13 +34,17 @@ public abstract class ActivationStrategy : NinjectComponent, IActivationStrategy /// /// The context. /// A reference to the instance being activated. - public virtual void Activate(IContext context, InstanceReference reference) { } + public virtual void Activate(IContext context, InstanceReference reference) + { + } /// /// Contributes to the deactivation of the instance in the specified context. /// /// The context. /// A reference to the instance being deactivated. - public virtual void Deactivate(IContext context, InstanceReference reference) { } + public virtual void Deactivate(IContext context, InstanceReference reference) + { + } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/BindingActionStrategy.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/BindingActionStrategy.cs index a8c4d50a..4e9caabe 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/BindingActionStrategy.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/BindingActionStrategy.cs @@ -1,20 +1,29 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation.Strategies { + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; + /// /// Executes actions defined on the binding during activation and deactivation. /// @@ -28,6 +37,7 @@ public class BindingActionStrategy : ActivationStrategy public override void Activate(IContext context, InstanceReference reference) { Ensure.ArgumentNotNull(context, "context"); + context.Binding.ActivationActions.Map(action => action(context, reference.Instance)); } @@ -39,6 +49,7 @@ public override void Activate(IContext context, InstanceReference reference) public override void Deactivate(IContext context, InstanceReference reference) { Ensure.ArgumentNotNull(context, "context"); + context.Binding.DeactivationActions.Map(action => action(context, reference.Instance)); } } diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/DisposableStrategy.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/DisposableStrategy.cs index 66f70d61..7713f62d 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/DisposableStrategy.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/DisposableStrategy.cs @@ -1,18 +1,28 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation.Strategies { + using System; + /// /// During deactivation, disposes instances that implement . /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/IActivationStrategy.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/IActivationStrategy.cs index f2777588..032d052a 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/IActivationStrategy.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/IActivationStrategy.cs @@ -1,19 +1,28 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Components; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation.Strategies { + using Telerik.JustMock.AutoMock.Ninject.Components; + /// /// Contributes to a , and is called during the activation /// and deactivation of an instance. diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/InitializableStrategy.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/InitializableStrategy.cs index a87a6f3c..51c05631 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/InitializableStrategy.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/InitializableStrategy.cs @@ -1,15 +1,23 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation.Strategies { diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/MethodInjectionStrategy.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/MethodInjectionStrategy.cs index 3f9a2eaa..c9098e66 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/MethodInjectionStrategy.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/MethodInjectionStrategy.cs @@ -1,22 +1,31 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Linq; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Injection; -using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation.Strategies { + using System.Linq; + + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; + /// /// Injects methods on an instance during activation. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/PropertyInjectionStrategy.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/PropertyInjectionStrategy.cs index e9be9bbf..309985e5 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/PropertyInjectionStrategy.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/PropertyInjectionStrategy.cs @@ -1,28 +1,38 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Introspection; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; -using Telerik.JustMock.AutoMock.Ninject.Injection; -using Telerik.JustMock.AutoMock.Ninject.Parameters; -using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; -using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation.Strategies { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Introspection; + using Telerik.JustMock.AutoMock.Ninject.Injection; + using Telerik.JustMock.AutoMock.Ninject.Parameters; + using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; + using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; + /// /// Injects properties on an instance during activation. /// @@ -30,30 +40,30 @@ public class PropertyInjectionStrategy : ActivationStrategy { private const BindingFlags DefaultFlags = BindingFlags.Public | BindingFlags.Instance; - private BindingFlags Flags + /// + /// Initializes a new instance of the class. + /// + /// The injector factory component. + public PropertyInjectionStrategy(IInjectorFactory injectorFactory) { - get - { - #if !NO_LCG && !SILVERLIGHT - return Settings.InjectNonPublic ? (DefaultFlags | BindingFlags.NonPublic) : DefaultFlags; - #else - return DefaultFlags; - #endif - } + this.InjectorFactory = injectorFactory; } /// - /// Gets the injector factory component. + /// Gets or sets the injector factory component. /// public IInjectorFactory InjectorFactory { get; set; } - /// - /// Initializes a new instance of the class. - /// - /// The injector factory component. - public PropertyInjectionStrategy(IInjectorFactory injectorFactory) + private BindingFlags Flags { - this.InjectorFactory = injectorFactory; + get + { +#if !NO_LCG + return this.Settings.InjectNonPublic ? (DefaultFlags | BindingFlags.NonPublic) : DefaultFlags; +#else + return DefaultFlags; +#endif + } } /// @@ -71,11 +81,11 @@ public override void Activate(IContext context, InstanceReference reference) foreach (var directive in context.Plan.GetAll()) { - object value = this.GetValue(context, directive.Target, propertyValues); + var value = this.GetValue(context, directive.Target, propertyValues); directive.Injector(reference.Instance, value); } - this.AssignProperyOverrides(context, reference, propertyValues); + this.AssignPropertyOverrides(context, reference, propertyValues); } /// @@ -84,12 +94,13 @@ public override void Activate(IContext context, InstanceReference reference) /// The context. /// A reference to the instance being activated. /// The parameter override value accessors. - private void AssignProperyOverrides(IContext context, InstanceReference reference, IList propertyValues) + private void AssignPropertyOverrides(IContext context, InstanceReference reference, IList propertyValues) { var properties = reference.Instance.GetType().GetProperties(this.Flags); + foreach (var propertyValue in propertyValues) { - string propertyName = propertyValue.Name; + var propertyName = propertyValue.Name; var propertyInfo = properties.FirstOrDefault(property => string.Equals(property.Name, propertyName, StringComparison.Ordinal)); if (propertyInfo == null) @@ -98,7 +109,7 @@ private void AssignProperyOverrides(IContext context, InstanceReference referenc } var target = new PropertyInjectionDirective(propertyInfo, this.InjectorFactory.Create(propertyInfo)); - object value = this.GetValue(context, target.Target, propertyValues); + var value = this.GetValue(context, target.Target, propertyValues); target.Injector(reference.Instance, value); } } diff --git a/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/StartableStrategy.cs b/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/StartableStrategy.cs index d0b53fd2..da0eea35 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/StartableStrategy.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Activation/Strategies/StartableStrategy.cs @@ -1,15 +1,23 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Activation.Strategies { diff --git a/Telerik.JustMock/AutoMock/Ninject/ActivationException.cs b/Telerik.JustMock/AutoMock/Ninject/ActivationException.cs index 20c1a37d..9129a1aa 100644 --- a/Telerik.JustMock/AutoMock/Ninject/ActivationException.cs +++ b/Telerik.JustMock/AutoMock/Ninject/ActivationException.cs @@ -1,54 +1,69 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -#if !NO_EXCEPTION_SERIALIZATION -using System.Runtime.Serialization; -#endif -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject { + using System; + using System.Runtime.Serialization; + /// - /// Indicates that an error occured during activation of an instance. + /// Indicates that an error occurred during activation of an instance. /// - #if !NO_EXCEPTION_SERIALIZATION [Serializable] - #endif public class ActivationException : Exception { /// /// Initializes a new instance of the class. /// - public ActivationException() { } + public ActivationException() + { + } /// /// Initializes a new instance of the class. /// /// The exception message. - public ActivationException(string message) : base(message) { } + public ActivationException(string message) + : base(message) + { + } /// /// Initializes a new instance of the class. /// /// The exception message. /// The inner exception. - public ActivationException(string message, Exception innerException) : base(message, innerException) { } + public ActivationException(string message, Exception innerException) + : base(message, innerException) + { + } - #if !NO_EXCEPTION_SERIALIZATION /// /// Initializes a new instance of the class. /// /// The serialized object data. /// The serialization context. - protected ActivationException(SerializationInfo info, StreamingContext context) : base(info, context) { } - #endif + protected ActivationException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } } -} \ No newline at end of file +} diff --git a/Telerik.JustMock/AutoMock/Ninject/Attributes/ConstraintAttribute.cs b/Telerik.JustMock/AutoMock/Ninject/Attributes/ConstraintAttribute.cs index 3910717e..08a98bca 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Attributes/ConstraintAttribute.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Attributes/ConstraintAttribute.cs @@ -1,19 +1,30 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject { + using System; + + using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; + /// /// Defines a constraint on the decorated member. /// @@ -27,4 +38,4 @@ public abstract class ConstraintAttribute : Attribute /// True if the metadata matches; otherwise false. public abstract bool Matches(IBindingMetadata metadata); } -} +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Attributes/InjectAttribute.cs b/Telerik.JustMock/AutoMock/Ninject/Attributes/InjectAttribute.cs index d15fe88a..d89cc870 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Attributes/InjectAttribute.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Attributes/InjectAttribute.cs @@ -1,22 +1,36 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject { + using System; + /// /// Indicates that the decorated member should be injected. /// - [AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field, - AllowMultiple = false, Inherited = true)] - public class InjectAttribute : Attribute { } -} + [AttributeUsage( + AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field, + AllowMultiple = false, + Inherited = true)] + public class InjectAttribute : Attribute + { + } +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Attributes/NamedAttribute.cs b/Telerik.JustMock/AutoMock/Ninject/Attributes/NamedAttribute.cs index f704bad7..c8e0a635 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Attributes/NamedAttribute.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Attributes/NamedAttribute.cs @@ -1,31 +1,35 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject { + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; + /// /// Indicates that the decorated member should only be injected using binding(s) registered /// with the specified name. /// public class NamedAttribute : ConstraintAttribute { - /// - /// Gets the binding name. - /// - public string Name { get; private set; } - /// /// Initializes a new instance of the class. /// @@ -33,9 +37,15 @@ public class NamedAttribute : ConstraintAttribute public NamedAttribute(string name) { Ensure.ArgumentNotNullOrEmpty(name, "name"); - Name = name; + + this.Name = name; } + /// + /// Gets the binding name. + /// + public string Name { get; private set; } + /// /// Determines whether the specified binding metadata matches the constraint. /// @@ -44,7 +54,8 @@ public NamedAttribute(string name) public override bool Matches(IBindingMetadata metadata) { Ensure.ArgumentNotNull(metadata, "metadata"); - return metadata.Name == Name; + + return metadata.Name == this.Name; } } -} +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Attributes/OptionalAttribute.cs b/Telerik.JustMock/AutoMock/Ninject/Attributes/OptionalAttribute.cs index 9f7c63d2..aaf00a21 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Attributes/OptionalAttribute.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Attributes/OptionalAttribute.cs @@ -1,22 +1,36 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject { + using System; + /// /// Indicates that the decorated member represents an optional dependency. /// - [AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Parameter, - AllowMultiple = false, Inherited = true)] - public class OptionalAttribute : Attribute { } -} + [AttributeUsage( + AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Parameter, + AllowMultiple = false, + Inherited = true)] + public class OptionalAttribute : Attribute + { + } +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Components/ComponentContainer.cs b/Telerik.JustMock/AutoMock/Ninject/Components/ComponentContainer.cs index 90aed720..dc6bfac7 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Components/ComponentContainer.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Components/ComponentContainer.cs @@ -1,32 +1,43 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Disposal; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Introspection; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Components { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Disposal; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Introspection; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; + /// /// An internal container that manages and resolves components that contribute to Ninject. /// public class ComponentContainer : DisposableObject, IComponentContainer { - private readonly Multimap _mappings = new Multimap(); - private readonly Dictionary _instances = new Dictionary(); + private readonly Multimap mappings = new Multimap(); + private readonly Dictionary instances = new Dictionary(); private readonly HashSet> transients = new HashSet>(); /// @@ -37,15 +48,18 @@ public class ComponentContainer : DisposableObject, IComponentContainer /// /// Releases resources held by the object. /// + /// True if called manually, otherwise by GC. public override void Dispose(bool disposing) { - if (disposing && !IsDisposed) + if (disposing && !this.IsDisposed) { - foreach (INinjectComponent instance in _instances.Values) + foreach (INinjectComponent instance in this.instances.Values) + { instance.Dispose(); + } - _mappings.Clear(); - _instances.Clear(); + this.mappings.Clear(); + this.instances.Clear(); } base.Dispose(disposing); @@ -60,7 +74,7 @@ public void Add() where TComponent : INinjectComponent where TImplementation : TComponent, INinjectComponent { - _mappings.Add(typeof(TComponent), typeof(TImplementation)); + this.mappings.Add(typeof(TComponent), typeof(TImplementation)); } /// @@ -75,7 +89,7 @@ public void AddTransient() this.Add(); this.transients.Add(new KeyValuePair(typeof(TComponent), typeof(TImplementation))); } - + /// /// Removes all registrations for the specified component. /// @@ -83,7 +97,27 @@ public void AddTransient() public void RemoveAll() where T : INinjectComponent { - RemoveAll(typeof(T)); + this.RemoveAll(typeof(T)); + } + + /// + /// Removes the specified registration. + /// + /// The component type. + /// The implementation type. + public void Remove() + where T : INinjectComponent + where TImplementation : T + { + var implementation = typeof(TImplementation); + if (this.instances.ContainsKey(implementation)) + { + this.instances[implementation].Dispose(); + } + + this.instances.Remove(implementation); + + this.mappings.Remove(typeof(T), typeof(TImplementation)); } /// @@ -94,15 +128,17 @@ public void RemoveAll(Type component) { Ensure.ArgumentNotNull(component, "component"); - foreach (Type implementation in _mappings[component]) + foreach (Type implementation in this.mappings[component]) { - if (_instances.ContainsKey(implementation)) - _instances[implementation].Dispose(); + if (this.instances.ContainsKey(implementation)) + { + this.instances[implementation].Dispose(); + } - _instances.Remove(implementation); + this.instances.Remove(implementation); } - _mappings.RemoveAll(component); + this.mappings.RemoveAll(component); } /// @@ -113,7 +149,7 @@ public void RemoveAll(Type component) public T Get() where T : INinjectComponent { - return (T) Get(typeof(T)); + return (T)this.Get(typeof(T)); } /// @@ -124,7 +160,7 @@ public T Get() public IEnumerable GetAll() where T : INinjectComponent { - return GetAll(typeof(T)).Cast(); + return this.GetAll(typeof(T)).Cast(); } /// @@ -137,29 +173,29 @@ public object Get(Type component) Ensure.ArgumentNotNull(component, "component"); if (component == typeof(IKernel)) - return Kernel; + { + return this.Kernel; + } if (component.IsGenericType) { - Type gtd = component.GetGenericTypeDefinition(); - Type argument = component.GetGenericArguments()[0]; - -#if WINDOWS_PHONE - Type discreteGenericType = - typeof (IEnumerable<>).MakeGenericType(argument); - if (gtd.IsInterface && discreteGenericType.IsAssignableFrom(component)) - return GetAll(argument).CastSlow(argument); -#else - if (gtd.IsInterface && typeof (IEnumerable<>).IsAssignableFrom(gtd)) - return GetAll(argument).CastSlow(argument); -#endif + var gtd = component.GetGenericTypeDefinition(); + var argument = component.GenericTypeArguments[0]; + + if (gtd.IsInterface && typeof(IEnumerable<>).IsAssignableFrom(gtd)) + { + return this.GetAll(argument).CastSlow(argument); + } } - Type implementation = _mappings[component].FirstOrDefault(); + + var implementation = this.mappings[component].FirstOrDefault(); if (implementation == null) + { throw new InvalidOperationException(ExceptionFormatter.NoSuchComponentRegistered(component)); + } - return ResolveInstance(component, implementation); + return this.ResolveInstance(component, implementation); } /// @@ -171,29 +207,44 @@ public IEnumerable GetAll(Type component) { Ensure.ArgumentNotNull(component, "component"); - return _mappings[component] - .Select(implementation => ResolveInstance(component, implementation)); + return this.mappings[component] + .Select(implementation => this.ResolveInstance(component, implementation)); + } + + private static ConstructorInfo SelectConstructor(Type component, Type implementation) + { + var constructor = implementation.GetConstructors().OrderByDescending(c => c.GetParameters().Length).FirstOrDefault(); + + if (constructor == null) + { + throw new InvalidOperationException(ExceptionFormatter.NoConstructorsAvailableForComponent(component, implementation)); + } + + return constructor; } private object ResolveInstance(Type component, Type implementation) { - lock (_instances) - return _instances.ContainsKey(implementation) ? _instances[implementation] : CreateNewInstance(component, implementation); + lock (this.instances) + { + return this.instances.ContainsKey(implementation) ? this.instances[implementation] : this.CreateNewInstance(component, implementation); + } } private object CreateNewInstance(Type component, Type implementation) { - ConstructorInfo constructor = SelectConstructor(component, implementation); - var arguments = constructor.GetParameters().Select(parameter => Get(parameter.ParameterType)).ToArray(); + var constructor = SelectConstructor(component, implementation); + var arguments = constructor.GetParameters().Select(parameter => this.Get(parameter.ParameterType)).ToArray(); try { var instance = constructor.Invoke(arguments) as INinjectComponent; - instance.Settings = Kernel.Settings; + + instance.Settings = this.Kernel.Settings; if (!this.transients.Contains(new KeyValuePair(component, implementation))) { - _instances.Add(implementation, instance); + this.instances.Add(implementation, instance); } return instance; @@ -204,32 +255,5 @@ private object CreateNewInstance(Type component, Type implementation) return null; } } - - private static ConstructorInfo SelectConstructor(Type component, Type implementation) - { - var constructor = implementation.GetConstructors().OrderByDescending(c => c.GetParameters().Length).FirstOrDefault(); - - if (constructor == null) - throw new InvalidOperationException(ExceptionFormatter.NoConstructorsAvailableForComponent(component, implementation)); - - return constructor; - } - -#if SILVERLIGHT_30 || SILVERLIGHT_20 || WINDOWS_PHONE || NETCF_35 - private class HashSet - { - private IDictionary data = new Dictionary(); - - public void Add(T o) - { - this.data.Add(o, null); - } - - public bool Contains(T o) - { - return this.data.ContainsKey(o); - } - } -#endif } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Components/IComponentContainer.cs b/Telerik.JustMock/AutoMock/Ninject/Components/IComponentContainer.cs index c5702614..077769e0 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Components/IComponentContainer.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Components/IComponentContainer.cs @@ -1,19 +1,29 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Components { + using System; + using System.Collections.Generic; + /// /// An internal container that manages and resolves components that contribute to Ninject. /// @@ -37,7 +47,8 @@ void Add() /// Removes all registrations for the specified component. /// /// The component type. - void RemoveAll() where T : INinjectComponent; + void RemoveAll() + where T : INinjectComponent; /// /// Removes all registrations for the specified component. @@ -45,19 +56,30 @@ void Add() /// The component's type. void RemoveAll(Type component); + /// + /// Removes the specified registration. + /// + /// The component type. + /// The implementation type. + void Remove() + where T : INinjectComponent + where TImplementation : T; + /// /// Gets one instance of the specified component. /// /// The component type. /// The instance of the component. - T Get() where T : INinjectComponent; + T Get() + where T : INinjectComponent; /// /// Gets all available instances of the specified component. /// /// The component type. /// A series of instances of the specified component. - IEnumerable GetAll() where T : INinjectComponent; + IEnumerable GetAll() + where T : INinjectComponent; /// /// Gets one instance of the specified component. diff --git a/Telerik.JustMock/AutoMock/Ninject/Components/INinjectComponent.cs b/Telerik.JustMock/AutoMock/Ninject/Components/INinjectComponent.cs index cd9488c8..c54fcad4 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Components/INinjectComponent.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Components/INinjectComponent.cs @@ -1,18 +1,28 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Components { + using System; + /// /// A component that contributes to the internals of Ninject. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Components/NinjectComponent.cs b/Telerik.JustMock/AutoMock/Ninject/Components/NinjectComponent.cs index efce03a4..2d2eb499 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Components/NinjectComponent.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Components/NinjectComponent.cs @@ -1,19 +1,28 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Disposal; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Components { + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Disposal; + /// /// A component that contributes to the internals of Ninject. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/GlobalKernelRegistration.cs b/Telerik.JustMock/AutoMock/Ninject/GlobalKernelRegistration.cs index 9796a5dd..e6a1e586 100644 --- a/Telerik.JustMock/AutoMock/Ninject/GlobalKernelRegistration.cs +++ b/Telerik.JustMock/AutoMock/Ninject/GlobalKernelRegistration.cs @@ -1,10 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -17,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject { @@ -32,23 +32,36 @@ namespace Telerik.JustMock.AutoMock.Ninject /// public abstract class GlobalKernelRegistration { - private static readonly ReaderWriterLock kernelRegistrationsLock = new ReaderWriterLock(); - private static readonly IDictionary kernelRegistrations = new Dictionary(); + private static readonly ReaderWriterLockSlim KernelRegistrationsLock = new ReaderWriterLockSlim(); + + private static readonly IDictionary KernelRegistrations = new Dictionary(); + /// + /// Registers the kernel for the specified type. + /// + /// The . + /// The service type. internal static void RegisterKernelForType(IKernel kernel, Type type) { var registration = GetRegistrationForType(type); - registration.KernelLock.AcquireWriterLock(Timeout.Infinite); + + registration.KernelLock.EnterWriteLock(); + try { registration.Kernels.Add(new WeakReference(kernel)); } finally { - registration.KernelLock.ReleaseWriterLock(); + registration.KernelLock.ExitWriteLock(); } } + /// + /// Un-registers the kernel for the specified type. + /// + /// The . + /// The service type. internal static void UnregisterKernelForType(IKernel kernel, Type type) { var registration = GetRegistrationForType(type); @@ -61,16 +74,16 @@ internal static void UnregisterKernelForType(IKernel kernel, Type type) /// The action. protected void MapKernels(Action action) { - bool requiresCleanup = false; + var requiresCleanup = false; var registration = GetRegistrationForType(this.GetType()); - registration.KernelLock.AcquireReaderLock(Timeout.Infinite); + + registration.KernelLock.EnterReadLock(); try { foreach (var weakReference in registration.Kernels) { - var kernel = weakReference.Target as IKernel; - if (kernel != null) + if (weakReference.Target is IKernel kernel) { action(kernel); } @@ -82,7 +95,7 @@ protected void MapKernels(Action action) } finally { - registration.KernelLock.ReleaseReaderLock(); + registration.KernelLock.ExitReadLock(); } if (requiresCleanup) @@ -90,10 +103,11 @@ protected void MapKernels(Action action) RemoveKernels(registration, registration.Kernels.Where(reference => !reference.IsAlive)); } } - + private static void RemoveKernels(Registration registration, IEnumerable references) { - registration.KernelLock.AcquireWriterLock(Timeout.Infinite); + registration.KernelLock.EnterWriteLock(); + try { foreach (var reference in references.ToArray()) @@ -103,47 +117,46 @@ private static void RemoveKernels(Registration registration, IEnumerable(); } - public ReaderWriterLock KernelLock { get; private set; } + public ReaderWriterLockSlim KernelLock { get; private set; } + public IList Kernels { get; private set; } } } diff --git a/Telerik.JustMock/AutoMock/Ninject/GlobalKernelRegistrationModule.cs b/Telerik.JustMock/AutoMock/Ninject/GlobalKernelRegistrationModule.cs index 762fd133..571bbc18 100644 --- a/Telerik.JustMock/AutoMock/Ninject/GlobalKernelRegistrationModule.cs +++ b/Telerik.JustMock/AutoMock/Ninject/GlobalKernelRegistrationModule.cs @@ -1,10 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -17,9 +17,8 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- -#if !SILVERLIGHT && !NETCF namespace Telerik.JustMock.AutoMock.Ninject { using Telerik.JustMock.AutoMock.Ninject.Modules; @@ -48,5 +47,4 @@ public override void Unload() GlobalKernelRegistration.UnregisterKernelForType(this.Kernel, typeof(TGlobalKernelRegistry)); } } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/IHaveNinjectComponents.cs b/Telerik.JustMock/AutoMock/Ninject/IHaveNinjectComponents.cs new file mode 100644 index 00000000..4ed33210 --- /dev/null +++ b/Telerik.JustMock/AutoMock/Ninject/IHaveNinjectComponents.cs @@ -0,0 +1,36 @@ +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- + +namespace Telerik.JustMock.AutoMock.Ninject +{ + using Telerik.JustMock.AutoMock.Ninject.Components; + + /// + /// Provides access to Ninject components. + /// + public interface IHaveNinjectComponents + { + /// + /// Gets the component container, which holds components that contribute to Ninject. + /// + IComponentContainer Components { get; } + } +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/IHaveNinjectSettings.cs b/Telerik.JustMock/AutoMock/Ninject/IHaveNinjectSettings.cs new file mode 100644 index 00000000..77762f26 --- /dev/null +++ b/Telerik.JustMock/AutoMock/Ninject/IHaveNinjectSettings.cs @@ -0,0 +1,34 @@ +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- + +namespace Telerik.JustMock.AutoMock.Ninject +{ + /// + /// Provides access to Ninject settings. + /// + public interface IHaveNinjectSettings + { + /// + /// Gets the kernel settings. + /// + INinjectSettings Settings { get; } + } +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/IInitializable.cs b/Telerik.JustMock/AutoMock/Ninject/IInitializable.cs index 9681d69a..f1e44495 100644 --- a/Telerik.JustMock/AutoMock/Ninject/IInitializable.cs +++ b/Telerik.JustMock/AutoMock/Ninject/IInitializable.cs @@ -1,15 +1,23 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject { @@ -23,4 +31,4 @@ public interface IInitializable /// void Initialize(); } -} +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/IKernel.cs b/Telerik.JustMock/AutoMock/Ninject/IKernel.cs index 7a292017..c1372748 100644 --- a/Telerik.JustMock/AutoMock/Ninject/IKernel.cs +++ b/Telerik.JustMock/AutoMock/Ninject/IKernel.cs @@ -1,31 +1,41 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Activation.Blocks; -using Telerik.JustMock.AutoMock.Ninject.Components; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Disposal; -using Telerik.JustMock.AutoMock.Ninject.Modules; -using Telerik.JustMock.AutoMock.Ninject.Parameters; -using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; -using Telerik.JustMock.AutoMock.Ninject.Syntax; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject { + using System; + using System.Collections.Generic; + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Activation.Blocks; + using Telerik.JustMock.AutoMock.Ninject.Components; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Disposal; + using Telerik.JustMock.AutoMock.Ninject.Modules; + using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; + using Telerik.JustMock.AutoMock.Ninject.Syntax; + /// /// A super-factory that can create objects of all kinds, following hints provided by s. /// - public interface IKernel : IBindingRoot, IResolutionRoot, IServiceProvider, IDisposableObject + public interface IKernel : IBindingRoot, IResolutionRoot, IServiceProvider, IDisposableObject, INotifyWhenDisposed { /// /// Gets the kernel settings. @@ -56,7 +66,6 @@ public interface IKernel : IBindingRoot, IResolutionRoot, IServiceProvider, IDis /// The modules to load. void Load(IEnumerable m); - #if !NO_ASSEMBLY_SCANNING /// /// Loads modules from the files that match the specified pattern(s). /// @@ -68,7 +77,6 @@ public interface IKernel : IBindingRoot, IResolutionRoot, IServiceProvider, IDis /// /// The assemblies to search. void Load(IEnumerable assemblies); - #endif /// /// Unloads the plugin with the specified name. @@ -76,13 +84,6 @@ public interface IKernel : IBindingRoot, IResolutionRoot, IServiceProvider, IDis /// The plugin's name. void Unload(string name); - /// - /// Injects the specified existing instance, without managing its lifecycle. - /// - /// The instance to inject. - /// The parameters to pass to the request. - void Inject(object instance, params IParameter[] parameters); - /// /// Gets the bindings registered for the specified service. /// @@ -96,4 +97,4 @@ public interface IKernel : IBindingRoot, IResolutionRoot, IServiceProvider, IDis /// The new activation block. IActivationBlock BeginBlock(); } -} +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/INinjectSettings.cs b/Telerik.JustMock/AutoMock/Ninject/INinjectSettings.cs index e2424c24..da8df5ef 100644 --- a/Telerik.JustMock/AutoMock/Ninject/INinjectSettings.cs +++ b/Telerik.JustMock/AutoMock/Ninject/INinjectSettings.cs @@ -1,20 +1,30 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Activation; - -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject { + using System; + + using Telerik.JustMock.AutoMock.Ninject.Activation; + /// /// Contains configuration options for Ninject. /// @@ -35,7 +45,6 @@ public interface INinjectSettings /// Func DefaultScopeCallback { get; } - #if !NO_ASSEMBLY_SCANNING /// /// Gets a value indicating whether the kernel should automatically load extensions at startup. /// @@ -45,31 +54,28 @@ public interface INinjectSettings /// Gets the paths that should be searched for extensions. /// string[] ExtensionSearchPatterns { get; } - #endif //!NO_ASSEMBLY_SCANNING - #if !NO_LCG +#if !NO_LCG /// /// Gets a value indicating whether Ninject should use reflection-based injection instead of /// the (usually faster) lightweight code generation system. /// bool UseReflectionBasedInjection { get; } - #endif //!NO_LCG +#endif //!NO_LCG - #if !SILVERLIGHT /// - /// Gets a value indicating whether Ninject should inject non public members. + /// Gets or sets a value indicating whether Ninject should inject non public members. /// bool InjectNonPublic { get; set; } /// - /// Gets a value indicating whether Ninject should inject private properties of base classes. + /// Gets or sets a value indicating whether Ninject should inject private properties of base classes. /// /// - /// Activating this setting has an impact on the performance. It is recomended not + /// Activating this setting has an impact on the performance. It is recommended not /// to use this feature and use constructor injection instead. /// bool InjectParentPrivateProperties { get; set; } - #endif //!SILVERLIGHT /// /// Gets or sets a value indicating whether the activation cache is disabled. @@ -85,11 +91,19 @@ public interface INinjectSettings /// /// Gets or sets a value indicating whether Null is a valid value for injection. - /// By defuault this is disabled and whenever a provider returns null an exception is thrown. + /// By default this is disabled and whenever a provider returns null an exception is thrown. /// /// true if null is allowed as injected value otherwise false. bool AllowNullInjection { get; set; } + /// + /// Gets or sets a value indicating whether the old (<= 3.3.4) behavior of + /// should be used which throws an exception if the requested service cannot be found. Note that the documentation + /// of that method https://docs.microsoft.com/en-us/dotnet/api/system.iserviceprovider.getservice?view=netframework-4.6.2 + /// states that the method should return if there is no such service. + /// + bool ThrowOnGetServiceNotFound { get; set; } + /// /// Gets the value for the specified key. /// @@ -106,4 +120,4 @@ public interface INinjectSettings /// The setting's value. void Set(string key, object value); } -} +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/IStartable.cs b/Telerik.JustMock/AutoMock/Ninject/IStartable.cs index ea172fd1..15ce53e5 100644 --- a/Telerik.JustMock/AutoMock/Ninject/IStartable.cs +++ b/Telerik.JustMock/AutoMock/Ninject/IStartable.cs @@ -1,15 +1,23 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject { @@ -28,4 +36,4 @@ public interface IStartable /// void Stop(); } -} +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/BaseWeakReference.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/BaseWeakReference.cs deleted file mode 100644 index 96d5c0f0..00000000 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/BaseWeakReference.cs +++ /dev/null @@ -1,60 +0,0 @@ -namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure -{ - using System; - - /// - /// Inheritable weak reference base class for Silverlight - /// - public abstract class BaseWeakReference - { - private readonly WeakReference innerWeakReference; - - /// - /// Initializes a new instance of the class. - /// - /// The target. - protected BaseWeakReference(object target) - { - this.innerWeakReference = new WeakReference(target); - } - - /// - /// Initializes a new instance of the class. - /// - /// The target. - /// if set to true [track resurrection]. - protected BaseWeakReference(object target, bool trackResurrection) - { - this.innerWeakReference = new WeakReference(target, trackResurrection); - } - - /// - /// Gets a value indicating whether this instance is alive. - /// - /// true if this instance is alive; otherwise, false. - public bool IsAlive - { - get - { - return this.innerWeakReference.IsAlive; - } - } - - /// - /// Gets or sets the target of this weak reference. - /// - /// The target of this weak reference. - public object Target - { - get - { - return this.innerWeakReference.Target; - } - - set - { - this.innerWeakReference.Target = value; - } - } - } -} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Disposal/DisposableObject.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Disposal/DisposableObject.cs index ebe4f201..9505f21f 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Disposal/DisposableObject.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Disposal/DisposableObject.cs @@ -1,24 +1,46 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure.Disposal { + using System; + /// /// An object that notifies when it is disposed. /// - public abstract class DisposableObject : IDisposableObject + public abstract class DisposableObject : IDisposableObject, INotifyWhenDisposed { + /// + /// Finalizes an instance of the class. + /// + ~DisposableObject() + { + this.Dispose(false); + } + + /// + /// Occurs when the object is disposed. + /// + public event EventHandler Disposed; + /// /// Gets a value indicating whether this instance is disposed. /// @@ -29,30 +51,25 @@ public abstract class DisposableObject : IDisposableObject /// public void Dispose() { - Dispose(true); + this.Dispose(true); } /// /// Releases resources held by the object. /// + /// True if called manually, otherwise by GC. public virtual void Dispose(bool disposing) { lock (this) { - if (disposing && !IsDisposed) + if (disposing && !this.IsDisposed) { - IsDisposed = true; + this.IsDisposed = true; + this.Disposed?.Invoke(this, EventArgs.Empty); + this.Disposed = null; GC.SuppressFinalize(this); } } } - - /// - /// Releases resources before the object is reclaimed by garbage collection. - /// - ~DisposableObject() - { - Dispose(false); - } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Disposal/IDisposableObject.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Disposal/IDisposableObject.cs index 07867e08..7dad029d 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Disposal/IDisposableObject.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Disposal/IDisposableObject.cs @@ -1,18 +1,28 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure.Disposal { + using System; + /// /// An object that can report whether or not it is disposed. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Disposal/INotifyWhenDisposed.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Disposal/INotifyWhenDisposed.cs index ed991b0f..7c2d65f3 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Disposal/INotifyWhenDisposed.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Disposal/INotifyWhenDisposed.cs @@ -1,18 +1,28 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure.Disposal { + using System; + /// /// An object that fires an event when it is disposed. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Ensure.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Ensure.cs index f1f876c6..0ccd6a21 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Ensure.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Ensure.cs @@ -1,28 +1,57 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure { + using System; + + /// + /// Argument guard. + /// internal static class Ensure { - public static void ArgumentNotNull(object argument, string name) + /// + /// Ensures the argument is not null. + /// + /// The argument value. + /// The argument name. + internal static void ArgumentNotNull(object argument, string name) { - if (argument == null) throw new ArgumentNullException(name, "Cannot be null"); + if (argument == null) + { + throw new ArgumentNullException(name, "Cannot be null"); + } } - public static void ArgumentNotNullOrEmpty(string argument, string name) + /// + /// Ensures the argument is not null or empty. + /// + /// The argument value. + /// The argument name. + internal static void ArgumentNotNullOrEmpty(string argument, string name) { - if (String.IsNullOrEmpty(argument)) throw new ArgumentException("Cannot be null or empty", name); + if (string.IsNullOrEmpty(argument)) + { + throw new ArgumentException("Cannot be null or empty", name); + } } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Future.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Future.cs deleted file mode 100644 index 4ceaafd6..00000000 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Future.cs +++ /dev/null @@ -1,67 +0,0 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; -#endregion - -namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure -{ - /// - /// Represents a future value. - /// - /// The type of value. - public class Future - { - private bool _hasValue; - private T _value; - - /// - /// Gets the value, resolving it if necessary. - /// - public T Value - { - get - { - if (!_hasValue) - { - _value = Callback(); - _hasValue = true; - } - - return _value; - } - } - - /// - /// Gets the callback that will be called to resolve the value. - /// - public Func Callback { get; private set; } - - /// - /// Initializes a new instance of the Future<T> class. - /// - /// The callback that will be triggered to read the value. - public Future(Func callback) - { - Callback = callback; - } - - /// - /// Gets the value from the future. - /// - /// The future. - /// The future value. - public static implicit operator T(Future future) - { - return future.Value; - } - } -} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/IHaveBindingConfiguration.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/IHaveBindingConfiguration.cs index d2df576a..0c0394e0 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/IHaveBindingConfiguration.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/IHaveBindingConfiguration.cs @@ -1,19 +1,28 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure { + using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; + /// /// Indicates the object has a reference to a . /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/IHaveKernel.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/IHaveKernel.cs index f8fbeed3..2d52c8f5 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/IHaveKernel.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/IHaveKernel.cs @@ -1,15 +1,23 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure { diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Introspection/ExceptionFormatter.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Introspection/ExceptionFormatter.cs index 6482c953..211106bc 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Introspection/ExceptionFormatter.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Introspection/ExceptionFormatter.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure.Introspection { @@ -50,6 +48,7 @@ public static string ModulesWithNullOrEmptyNamesAreNotSupported() /// /// Generates a message saying that modules without names are not supported. /// + /// The target. /// The exception message. public static string TargetDoesNotHaveADefaultValue(ITarget target) { @@ -71,10 +70,10 @@ public static string ModuleWithSameNameIsAlreadyLoaded(INinjectModule newModule, sw.WriteLine("Suggestions:"); sw.WriteLine(" 1) Ensure that you have not accidentally loaded the same module twice."); - #if !SILVERLIGHT +#if !NO_ASSEMBLY_SCANNING sw.WriteLine(" 2) If you are using automatic module loading, ensure you have not manually loaded a module"); sw.WriteLine(" that may be found by the module loader."); - #endif +#endif return sw.ToString(); } @@ -117,6 +116,7 @@ public static string CouldNotUniquelyResolveBinding(IRequest request, string[] f { sw.WriteLine(" {0}) {1}", i + 1, formattedMatchingBindings[i]); } + sw.WriteLine("Activation path:"); sw.WriteLine(request.FormatActivationPath()); @@ -147,9 +147,9 @@ public static string CouldNotResolveBinding(IRequest request) sw.WriteLine(" 2) If the binding was defined in a module, ensure that the module has been loaded into the kernel."); sw.WriteLine(" 3) Ensure you have not accidentally created more than one kernel."); sw.WriteLine(" 4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name."); - #if !SILVERLIGHT +#if !NO_ASSEMBLY_SCANNING sw.WriteLine(" 5) If you are using automatic module loading, ensure the search path and filters are correct."); - #endif +#endif return sw.ToString(); } @@ -229,7 +229,7 @@ public static string NoConstructorsAvailable(IContext context) return sw.ToString(); } } - + /// /// Generates a message saying that no constructors are available for the given component. /// @@ -297,6 +297,28 @@ public static string CouldNotResolvePropertyForValueInjection(IRequest request, } } + /// + /// Generates a message saying that the provider callback on the specified context is null. + /// + /// The context. + /// The exception message. + public static string ProviderCallbackIsNull(IContext context) + { + using (var sw = new StringWriter()) + { + sw.WriteLine("Error activating {0}", context.Request.Service.Format()); + sw.WriteLine("Provider callback is null."); + + sw.WriteLine("Activation path:"); + sw.WriteLine(context.Request.FormatActivationPath()); + + sw.WriteLine("Suggestions:"); + sw.WriteLine(" 1) Ensure that one of the 'To' methods is called after 'Bind' methond."); + + return sw.ToString(); + } + } + /// /// Generates a message saying that the provider on the specified context returned null. /// @@ -308,13 +330,13 @@ public static string ProviderReturnedNull(IContext context) { sw.WriteLine("Error activating {0} using {1}", context.Request.Service.Format(), context.Binding.Format(context)); sw.WriteLine("Provider returned null."); - + sw.WriteLine("Activation path:"); sw.WriteLine(context.Request.FormatActivationPath()); sw.WriteLine("Suggestions:"); sw.WriteLine(" 1) Ensure that the provider handles creation requests properly."); - + return sw.ToString(); } } @@ -332,7 +354,7 @@ public static string ConstructorsAmbiguous(IContext context, IGrouping -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.IO; -using System.Reflection; -using System.Text; -using Telerik.JustMock.AutoMock.Ninject.Activation; -using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; -using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure.Introspection { - using System.Globalization; + using System; + using System.IO; + using System.Reflection; + using System.Text; + + using Telerik.JustMock.AutoMock.Ninject.Activation; + using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; + using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; /// /// Provides extension methods for string formatting @@ -35,7 +44,7 @@ public static string FormatActivationPath(this IRequest request) { using (var sw = new StringWriter()) { - IRequest current = request; + var current = request; while (current != null) { @@ -48,7 +57,7 @@ public static string FormatActivationPath(this IRequest request) } /// - /// Formats the given binding into a meaningful string representation. + /// Formats the given binding into a meaningful string representation. /// /// The binding to be formatted. /// The context. @@ -58,12 +67,16 @@ public static string Format(this IBinding binding, IContext context) using (var sw = new StringWriter()) { if (binding.Condition != null) + { sw.Write("conditional "); + } if (binding.IsImplicit) + { sw.Write("implicit "); + } - IProvider provider = binding.GetProvider(context); + var provider = binding.GetProvider(context); switch (binding.Target) { @@ -76,8 +89,11 @@ public static string Format(this IBinding binding, IContext context) break; case BindingTarget.Provider: - sw.Write("provider binding from {0} to {1} (via {2})", binding.Service.Format(), - provider.Type.Format(), provider.GetType().Format()); + sw.Write( + "provider binding from {0} to {1} (via {2})", + binding.Service.Format(), + provider.Type.Format(), + provider.GetType().Format()); break; case BindingTarget.Method: @@ -106,9 +122,13 @@ public static string Format(this IRequest request) using (var sw = new StringWriter()) { if (request.Target == null) + { sw.Write("Request for {0}", request.Service.Format()); + } else + { sw.Write("Injection of dependency {0} into {1}", request.Service.Format(), request.Target.Format()); + } return sw.ToString(); } @@ -156,16 +176,12 @@ public static string Format(this Type type) { var friendlyName = GetFriendlyName(type); -#if !MONO if (friendlyName.Contains("AnonymousType")) + { return "AnonymousType"; -#else - - if (friendlyName.Contains("__AnonType")) - return "AnonymousType"; -#endif + } - switch (friendlyName.ToLower(CultureInfo.InvariantCulture)) + switch (friendlyName.ToLowerInvariant()) { case "int16": return "short"; case "int32": return "int"; @@ -186,9 +202,12 @@ public static string Format(this Type type) } var genericArguments = type.GetGenericArguments(); - if(genericArguments.Length > 0) + + if (genericArguments.Length > 0) + { return FormatGenericType(friendlyName, genericArguments); - + } + return friendlyName; } @@ -199,30 +218,29 @@ private static string GetFriendlyName(Type type) // remove generic arguments var firstBracket = friendlyName.IndexOf('['); if (firstBracket > 0) + { friendlyName = friendlyName.Substring(0, firstBracket); + } // remove assembly info var firstComma = friendlyName.IndexOf(','); if (firstComma > 0) + { friendlyName = friendlyName.Substring(0, firstComma); + } // remove namespace var lastPeriod = friendlyName.LastIndexOf('.'); if (lastPeriod >= 0) + { friendlyName = friendlyName.Substring(lastPeriod + 1); + } return friendlyName; } private static string FormatGenericType(string friendlyName, Type[] genericArguments) { - //var genericTag = "`" + genericArguments.Length; - //var genericArgumentNames = new string[genericArguments.Length]; - //for (int i = 0; i < genericArguments.Length; i++) - // genericArgumentNames[i] = genericArguments[i].Format(); - - //return friendlyName.Replace(genericTag, string.Join(", ", genericArgumentNames)); - var sb = new StringBuilder(friendlyName.Length + 10); var genericArgumentIndex = 0; @@ -231,8 +249,8 @@ private static string FormatGenericType(string friendlyName, Type[] genericArgum { if (friendlyName[index] == '`') { - var numArguments = friendlyName[index+1] - 48; - + var numArguments = friendlyName[index + 1] - 48; + sb.Append(friendlyName.Substring(startIndex, index - startIndex)); AppendGenericArguments(sb, genericArguments, genericArgumentIndex, numArguments); genericArgumentIndex += numArguments; @@ -240,8 +258,11 @@ private static string FormatGenericType(string friendlyName, Type[] genericArgum startIndex = index + 2; } } + if (startIndex < friendlyName.Length) + { sb.Append(friendlyName.Substring(startIndex)); + } return sb.ToString(); } @@ -250,14 +271,16 @@ private static void AppendGenericArguments(StringBuilder sb, Type[] genericArgum { sb.Append("{"); - for(int i = 0; i < count; i++) + for (int i = 0; i < count; i++) { if (i != 0) + { sb.Append(", "); + } sb.Append(genericArguments[start + i].Format()); } - + sb.Append("}"); } } diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForAssembly.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForAssembly.cs index e76d3afa..194c6a00 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForAssembly.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForAssembly.cs @@ -1,35 +1,59 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#if !NO_ASSEMBLY_SCANNING -#region Using Directives -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Modules; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Modules; + + /// + /// Provides extension methods for . + /// internal static class ExtensionsForAssembly { + /// + /// Determines whether the assembly has loadable . + /// + /// The . + /// True if there's any loadable , otherwise False. public static bool HasNinjectModules(this Assembly assembly) { - return assembly.GetExportedTypes().Any(IsLoadableModule); + return !assembly.IsDynamic && assembly.ExportedTypes.Any(IsLoadableModule); } + /// + /// Gets loadable s from the . + /// + /// The . + /// The loadable s public static IEnumerable GetNinjectModules(this Assembly assembly) { - return assembly.GetExportedTypes() - .Where(IsLoadableModule) - .Select(type => Activator.CreateInstance(type) as INinjectModule); + return assembly.IsDynamic ? + Enumerable.Empty() : + assembly.ExportedTypes.Where(IsLoadableModule) + .Select(type => Activator.CreateInstance(type) as INinjectModule); } private static bool IsLoadableModule(Type type) @@ -40,5 +64,4 @@ private static bool IsLoadableModule(Type type) && type.GetConstructor(Type.EmptyTypes) != null; } } -} -#endif //!NO_ASSEMBLY_SCANNING \ No newline at end of file +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForICustomAttributeProvider.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForICustomAttributeProvider.cs index b448aa80..c35944f5 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForICustomAttributeProvider.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForICustomAttributeProvider.cs @@ -1,24 +1,43 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language { using System; using System.Reflection; + /// + /// Provides extension methods for . + /// internal static class ExtensionsForICustomAttributeProvider { + /// + /// Determines if the has the specified attribute. + /// + /// The . + /// The attribute type. + /// True if the has the attribute, otherwise False. public static bool HasAttribute(this ICustomAttributeProvider member, Type type) { - var memberInfo = member as MemberInfo; - if (memberInfo != null) + if (member is MemberInfo memberInfo) { return memberInfo.HasAttribute(type); } @@ -26,10 +45,16 @@ public static bool HasAttribute(this ICustomAttributeProvider member, Type type) return member.IsDefined(type, true); } + /// + /// Gets custom attributes which supports and . + /// + /// The . + /// The attribute type. + /// When true, look up the hierarchy chain for the inherited custom attribute. + /// The attributes. public static object[] GetCustomAttributesExtended(this ICustomAttributeProvider member, Type attributeType, bool inherit) { - var memberInfo = member as MemberInfo; - if (memberInfo != null) + if (member is MemberInfo memberInfo) { return memberInfo.GetCustomAttributesExtended(attributeType, inherit); } diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForIEnumerable.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForIEnumerable.cs index 49562ab1..a2418cc5 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForIEnumerable.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForIEnumerable.cs @@ -1,37 +1,76 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections; -using System.Linq; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language { + using System; + using System.Collections; + using System.Linq; + using System.Reflection; + + /// + /// Provides extension methods for . + /// internal static class ExtensionsForIEnumerable { + private static readonly MethodInfo Cast = typeof(Enumerable).GetMethod(nameof(Cast)); + private static readonly MethodInfo ToArray = typeof(Enumerable).GetMethod(nameof(ToArray)); + private static readonly MethodInfo ToList = typeof(Enumerable).GetMethod(nameof(ToList)); + + /// + /// Casts the elements of an to the specified type using reflection. + /// + /// The that contains the elements to be cast. + /// The type to cast the elements of source to. + /// + /// An that contains each element of the + /// source sequence cast to the specified type. + /// public static IEnumerable CastSlow(this IEnumerable series, Type elementType) { - var method = typeof(Enumerable).GetMethod("Cast").MakeGenericMethod(elementType); + var method = Cast.MakeGenericMethod(elementType); return method.Invoke(null, new[] { series }) as IEnumerable; } + /// + /// Creates an array from an . + /// + /// An to create an array from. + /// The type of the elements. + /// An array that contains the elements from the input sequence. public static Array ToArraySlow(this IEnumerable series, Type elementType) { - var method = typeof(Enumerable).GetMethod("ToArray").MakeGenericMethod(elementType); + var method = ToArray.MakeGenericMethod(elementType); return method.Invoke(null, new[] { series }) as Array; } + /// + /// Creates an from an . + /// + /// An to create an from. + /// The type of the elements. + /// An that contains the elements from the input sequence. public static IList ToListSlow(this IEnumerable series, Type elementType) { - var method = typeof(Enumerable).GetMethod("ToList").MakeGenericMethod(elementType); + var method = ToList.MakeGenericMethod(elementType); return method.Invoke(null, new[] { series }) as IList; } } diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForIEnumerableOfT.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForIEnumerableOfT.cs index 356b4d8c..9650ebac 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForIEnumerableOfT.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForIEnumerableOfT.cs @@ -1,35 +1,47 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using System.Linq; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language { + using System; + using System.Collections.Generic; + using System.Linq; + /// - /// Provides extension methods for see cref="IEnumerable{T}"/> + /// Provides extension methods for . /// public static class ExtensionsForIEnumerableOfT { /// /// Executes the given action for each of the elements in the enumerable. /// - /// + /// Type of the enumerable. /// The series. /// The action. public static void Map(this IEnumerable series, Action action) { foreach (T item in series) + { action(item); + } } /// @@ -42,5 +54,28 @@ public static IEnumerable ToEnumerable(this IEnumerable series) { return series.Select(x => x); } + + /// + /// Returns single element of enumerable or throws exception. + /// + /// The series. + /// The exception creator. + /// Type of the enumerable. + /// The single element of enumerable. + /// + /// Exception specified by exception creator. + /// + public static T SingleOrThrowException(this IEnumerable series, Func exceptionCreator) + { + var e = series.GetEnumerator(); + e.MoveNext(); + var result = e.Current; + if (e.MoveNext()) + { + throw exceptionCreator(); + } + + return result; + } } -} \ No newline at end of file +} diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForMemberInfo.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForMemberInfo.cs index d542e8cb..e5f61d88 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForMemberInfo.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForMemberInfo.cs @@ -1,12 +1,23 @@ -#region License +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. // -// Author: Remo Gloor (remo.gloor@bbv.ch) -// Copyright (c) 2010, bbv Software Engineering AG. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language { @@ -17,18 +28,17 @@ namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language using System.Reflection; /// - /// Extensions for MemberInfo + /// Provides extension methods for . /// public static class ExtensionsForMemberInfo { - const BindingFlags DefaultFlags = BindingFlags.Public | BindingFlags.Instance; -#if !NO_LCG && !SILVERLIGHT - const BindingFlags Flags = DefaultFlags | BindingFlags.NonPublic; + private const BindingFlags DefaultFlags = BindingFlags.Public | BindingFlags.Instance; +#if !NO_LCG + private const BindingFlags Flags = DefaultFlags | BindingFlags.NonPublic; #else - const BindingFlags Flags = DefaultFlags; + private const BindingFlags Flags = DefaultFlags; #endif -#if !MONO private static MethodInfo parentDefinitionMethodInfo; private static MethodInfo ParentDefinitionMethodInfo @@ -44,7 +54,6 @@ private static MethodInfo ParentDefinitionMethodInfo return parentDefinitionMethodInfo; } } -#endif /// /// Determines whether the specified member has attribute. @@ -52,7 +61,7 @@ private static MethodInfo ParentDefinitionMethodInfo /// The type of the attribute. /// The member. /// - /// true if the specified member has attribute; otherwise, false. + /// true if the specified member has attribute; otherwise, false. /// public static bool HasAttribute(this MemberInfo member) { @@ -65,26 +74,15 @@ public static bool HasAttribute(this MemberInfo member) /// The member. /// The type of the attribute. /// - /// true if the specified member has attribute; otherwise, false. + /// true if the specified member has attribute; otherwise, false. /// public static bool HasAttribute(this MemberInfo member, Type type) { - var propertyInfo = member as PropertyInfo; - if (propertyInfo != null) + if (member is PropertyInfo propertyInfo) { return IsDefined(propertyInfo, type, true); } -#if NETCF - // Workaround for the CF bug that derived generic methods throw an exception for IsDefined - // This means that the Inject attribute can not be defined on base methods for CF framework - var methodInfo = member as MethodInfo; - if (methodInfo != null) - { - return methodInfo.IsDefined(type, false); - } -#endif - return member.IsDefined(type, true); } @@ -95,10 +93,7 @@ public static bool HasAttribute(this MemberInfo member, Type type) /// The property definition. /// The flags. /// The property info from the declared type of the property. - public static PropertyInfo GetPropertyFromDeclaredType( - this MemberInfo memberInfo, - PropertyInfo propertyDefinition, - BindingFlags flags) + public static PropertyInfo GetPropertyFromDeclaredType(this MemberInfo memberInfo, PropertyInfo propertyDefinition, BindingFlags flags) { return memberInfo.DeclaringType.GetProperty( propertyDefinition.Name, @@ -114,7 +109,7 @@ public static PropertyInfo GetPropertyFromDeclaredType( /// /// The property info. /// - /// true if the specified property info is private; otherwise, false. + /// true if the specified property info is private; otherwise, false. /// public static bool IsPrivate(this PropertyInfo propertyInfo) { @@ -125,25 +120,15 @@ public static bool IsPrivate(this PropertyInfo propertyInfo) /// /// Gets the custom attributes. - /// This version is able to get custom attributes for properties from base types even if the property is none public. + /// This version is able to get custom attributes for properties from base types even if the property is non-public. /// /// The member. /// Type of the attribute. /// if set to true [inherited]. - /// + /// The custom attributes. public static object[] GetCustomAttributesExtended(this MemberInfo member, Type attributeType, bool inherited) { -#if !NET_35 && !MONO_40 return Attribute.GetCustomAttributes(member, attributeType, inherited); -#else - var propertyInfo = member as PropertyInfo; - if (propertyInfo != null) - { - return GetCustomAttributes(propertyInfo, attributeType, inherited); - } - - return member.GetCustomAttributes(attributeType, inherited); -#endif } private static PropertyInfo GetParentDefinition(PropertyInfo property) @@ -163,25 +148,12 @@ private static PropertyInfo GetParentDefinition(PropertyInfo property) private static MethodInfo GetParentDefinition(this MethodInfo method, BindingFlags flags) { -#if MEDIUM_TRUST || MONO - var baseDefinition = method.GetBaseDefinition(); - var type = method.DeclaringType.BaseType; - MethodInfo result = null; - while (result == null && type != null) - { - result = type.GetMethods(flags).Where(m => m.GetBaseDefinition().Equals(baseDefinition)).SingleOrDefault(); - type = type.BaseType; - } - - return result; -#else if (ParentDefinitionMethodInfo == null) { return null; } return (MethodInfo)ParentDefinitionMethodInfo.Invoke(method, flags, null, null, CultureInfo.InvariantCulture); -#endif } private static bool IsDefined(PropertyInfo element, Type attributeType, bool inherit) @@ -225,7 +197,7 @@ private static object[] GetCustomAttributes(PropertyInfo propertyInfo, Type attr info != null; info = GetParentDefinition(info)) { - object[] customAttributes = info.GetCustomAttributes(attributeType, false); + var customAttributes = info.GetCustomAttributes(attributeType, false); AddAttributes(attributes, customAttributes, attributeUsages); } @@ -240,9 +212,9 @@ private static object[] GetCustomAttributes(PropertyInfo propertyInfo, Type attr private static void AddAttributes(List attributes, object[] customAttributes, Dictionary attributeUsages) { - foreach (object attribute in customAttributes) + foreach (var attribute in customAttributes) { - Type type = attribute.GetType(); + var type = attribute.GetType(); if (!attributeUsages.ContainsKey(type)) { attributeUsages[type] = InternalGetAttributeUsage(type).Inherited; @@ -257,8 +229,7 @@ private static void AddAttributes(List attributes, object[] customAttrib private static AttributeUsageAttribute InternalGetAttributeUsage(Type type) { - object[] customAttributes = type.GetCustomAttributes(typeof(AttributeUsageAttribute), true); - return (AttributeUsageAttribute)customAttributes[0]; - } + return type.GetCustomAttribute(true); + } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForTargetInvocationException.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForTargetInvocationException.cs index ce2cf8b5..8663ad99 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForTargetInvocationException.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForTargetInvocationException.cs @@ -1,27 +1,42 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Reflection; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language { + using System.Reflection; + using System.Runtime.ExceptionServices; + + /// + /// Provides extension methods for . + /// internal static class ExtensionsForTargetInvocationException { + /// + /// Re-throws inner exception. + /// + /// The . public static void RethrowInnerException(this TargetInvocationException exception) { - Exception innerException = exception.InnerException; - - FieldInfo stackTraceField = typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.Instance | BindingFlags.NonPublic); - stackTraceField.SetValue(innerException, innerException.StackTrace); + var innerException = exception.InnerException; + ExceptionDispatchInfo.Capture(innerException).Throw(); throw innerException; } diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForType.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForType.cs index 3f4d6cce..2c74a481 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForType.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Language/ExtensionsForType.cs @@ -1,10 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -17,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language { @@ -25,9 +25,8 @@ namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language using System.Collections.Generic; /// - /// Extension methods for type + /// Extension methods for . /// - /// public static class ExtensionsForType { /// @@ -40,6 +39,7 @@ public static IEnumerable GetAllBaseTypes(this Type type) while (type != null) { yield return type; + type = type.BaseType; } } diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Multimap.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Multimap.cs index 9d8471ae..0d723fe1 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Multimap.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Multimap.cs @@ -1,60 +1,71 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections; -using System.Collections.Generic; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure { + using System.Collections; + using System.Collections.Generic; + /// /// A data structure that contains multiple values for a each key. /// - /// The type of key. - /// The type of value. - public class Multimap : IEnumerable>> + /// The type of key. + /// The type of value. + public class Multimap : IEnumerable>> { - private readonly Dictionary> _items = new Dictionary>(); + private readonly Dictionary> items = new Dictionary>(); /// - /// Gets the collection of values stored under the specified key. + /// Gets the collection of keys. /// - /// The key. - public ICollection this[K key] + public ICollection Keys { - get - { - Ensure.ArgumentNotNull(key, "key"); - - if (!_items.ContainsKey(key)) - _items[key] = new List(); - - return _items[key]; - } + get { return this.items.Keys; } } /// - /// Gets the collection of keys. + /// Gets the collection of collections of values. /// - public ICollection Keys + public ICollection> Values { - get { return _items.Keys; } + get { return this.items.Values; } } /// - /// Gets the collection of collections of values. + /// Gets the collection of values stored under the specified key. /// - public ICollection> Values + /// The key. + public ICollection this[TKey key] { - get { return _items.Values; } + get + { + Ensure.ArgumentNotNull(key, "key"); + + if (!this.items.ContainsKey(key)) + { + this.items[key] = new List(); + } + + return this.items[key]; + } } /// @@ -62,7 +73,7 @@ public ICollection> Values /// /// The key. /// The value. - public void Add(K key, V value) + public void Add(TKey key, TValue value) { Ensure.ArgumentNotNull(key, "key"); Ensure.ArgumentNotNull(value, "value"); @@ -76,15 +87,17 @@ public void Add(K key, V value) /// The key. /// The value. /// True if such a value existed and was removed; otherwise false. - public bool Remove(K key, V value) + public bool Remove(TKey key, TValue value) { Ensure.ArgumentNotNull(key, "key"); Ensure.ArgumentNotNull(value, "value"); - if (!_items.ContainsKey(key)) + if (!this.items.ContainsKey(key)) + { return false; + } - return _items[key].Remove(value); + return this.items[key].Remove(value); } /// @@ -92,10 +105,10 @@ public bool Remove(K key, V value) /// /// The key. /// True if any such values existed; otherwise false. - public bool RemoveAll(K key) + public bool RemoveAll(TKey key) { Ensure.ArgumentNotNull(key, "key"); - return _items.Remove(key); + return this.items.Remove(key); } /// @@ -103,7 +116,7 @@ public bool RemoveAll(K key) /// public void Clear() { - _items.Clear(); + this.items.Clear(); } /// @@ -111,10 +124,10 @@ public void Clear() /// /// The key. /// True if the multimap has one or more values for the specified key; otherwise, false. - public bool ContainsKey(K key) + public bool ContainsKey(TKey key) { Ensure.ArgumentNotNull(key, "key"); - return _items.ContainsKey(key); + return this.items.ContainsKey(key); } /// @@ -123,12 +136,12 @@ public bool ContainsKey(K key) /// The key. /// The value. /// True if the multimap contains such a value; otherwise, false. - public bool ContainsValue(K key, V value) + public bool ContainsValue(TKey key, TValue value) { Ensure.ArgumentNotNull(key, "key"); Ensure.ArgumentNotNull(value, "value"); - return _items.ContainsKey(key) && _items[key].Contains(value); + return this.items.ContainsKey(key) && this.items[key].Contains(value); } /// @@ -137,12 +150,16 @@ public bool ContainsValue(K key, V value) /// An object that can be used to iterate through the multimap. public IEnumerator GetEnumerator() { - return _items.GetEnumerator(); + return this.items.GetEnumerator(); } - IEnumerator>> IEnumerable>>.GetEnumerator() + /// + /// Returns an enumerator that iterates through a the multimap. + /// + /// An object that can be used to iterate through the multimap. + IEnumerator>> IEnumerable>>.GetEnumerator() { - return _items.GetEnumerator(); + return this.items.GetEnumerator(); } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/ReferenceEqualWeakReference.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/ReferenceEqualWeakReference.cs index 9ed0bdee..1213802d 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/ReferenceEqualWeakReference.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/ReferenceEqualWeakReference.cs @@ -1,19 +1,28 @@ -#region License -// -// Author: Remo Gloor (remo.gloor@bbv.ch) -// Copyright (c) 2010, bbv Software Services AG -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- + namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure { using System; using System.Runtime.CompilerServices; -#if SILVERLIGHT - using WeakReference = BaseWeakReference; -#endif /// /// Weak reference that can be used in collections. It is equal to the @@ -21,19 +30,16 @@ namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure /// public class ReferenceEqualWeakReference : WeakReference { - private readonly int cashedHashCode; + private readonly int cachedHashCode; /// /// Initializes a new instance of the class. /// /// The target. - public ReferenceEqualWeakReference(object target) : base(target) + public ReferenceEqualWeakReference(object target) + : base(target) { -#if !NETCF - this.cashedHashCode = RuntimeHelpers.GetHashCode(target); -#else - this.cashedHashCode = target.GetHashCode(); -#endif + this.cachedHashCode = RuntimeHelpers.GetHashCode(target); } /// @@ -41,13 +47,10 @@ public ReferenceEqualWeakReference(object target) : base(target) /// /// The target. /// if set to true [track resurrection]. - public ReferenceEqualWeakReference(object target, bool trackResurrection) : base(target, trackResurrection) + public ReferenceEqualWeakReference(object target, bool trackResurrection) + : base(target, trackResurrection) { -#if !NETCF - this.cashedHashCode = RuntimeHelpers.GetHashCode(target); -#else - this.cashedHashCode = target.GetHashCode(); -#endif + this.cachedHashCode = RuntimeHelpers.GetHashCode(target); } /// @@ -64,8 +67,7 @@ public override bool Equals(object obj) { var thisInstance = this.IsAlive ? this.Target : this; - var referenceEqualWeakReference = obj as WeakReference; - if (referenceEqualWeakReference != null && referenceEqualWeakReference.IsAlive) + if (obj is WeakReference referenceEqualWeakReference && referenceEqualWeakReference.IsAlive) { obj = referenceEqualWeakReference.Target; } @@ -77,11 +79,11 @@ public override bool Equals(object obj) /// Returns a hash code for this instance. /// /// - /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// public override int GetHashCode() { - return this.cashedHashCode; + return this.cachedHashCode; } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/StandardScopeCallbacks.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/StandardScopeCallbacks.cs index d62a6bd8..153e1e21 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/StandardScopeCallbacks.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/StandardScopeCallbacks.cs @@ -1,19 +1,30 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Activation; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Infrastructure { + using System; + + using Telerik.JustMock.AutoMock.Ninject.Activation; + /// /// Scope callbacks for standard scopes. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Threading/ReaderWriterLock.cs b/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Threading/ReaderWriterLock.cs deleted file mode 100644 index 06cce2c9..00000000 --- a/Telerik.JustMock/AutoMock/Ninject/Infrastructure/Threading/ReaderWriterLock.cs +++ /dev/null @@ -1,416 +0,0 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// -// -// -// A reader-writer lock implementation that is intended to be simple, yet very -// efficient. In particular only 1 interlocked operation is taken for any lock -// operation (we use spin locks to achieve this). The spin lock is never held -// for more than a few instructions (in particular, we never call event APIs -// or in fact any non-trivial API while holding the spin lock). -// Currently this ReaderWriterLock does not support recurision, however it is -// not hard to add -// -// -------------------------------------------------------------------------------------------------------------------- - -#if SILVERLIGHT || NETCF -namespace System.Threading -{ - using System.Diagnostics; - - /// - /// A reader-writer lock implementation that is intended to be simple, yet very - /// efficient. In particular only 1 interlocked operation is taken for any lock - /// operation (we use spin locks to achieve this). The spin lock is never held - /// for more than a few instructions (in particular, we never call event APIs - /// or in fact any non-trivial API while holding the spin lock). - /// - /// Currently this ReaderWriterLock does not support recurision, however it is - /// not hard to add - /// - /// - /// By Vance Morrison - /// Taken from - http://blogs.msdn.com/vancem/archive/2006/03/28/563180.aspx - /// Code at - http://blogs.msdn.com/vancem/attachment/563180.ashx - /// - public class ReaderWriterLock - { - // Lock specifiation for myLock: This lock protects exactly the local fields associted - // instance of MyReaderWriterLock. It does NOT protect the memory associted with the - // the events that hang off this lock (eg writeEvent, readEvent upgradeEvent). -#region Constants and Fields - - /// - /// The my lock. - /// - private int myLock; - - // Who owns the lock owners > 0 => readers - // owners = -1 means there is one writer. Owners must be >= -1. - - /// - /// The number read waiters. - /// - private uint numReadWaiters; // maximum number of threads that can be doing a WaitOne on the readEvent - - /// - /// The number upgrade waiters. - /// - private uint numUpgradeWaiters; // maximum number of threads that can be doing a WaitOne on the upgradeEvent (at most 1). - - /// - /// The number write waiters. - /// - private uint numWriteWaiters; // maximum number of threads that can be doing a WaitOne on the writeEvent - - /// - /// The owners. - /// - private int owners; - - // conditions we wait on. - - /// - /// The read event. - /// - private EventWaitHandle readEvent; // threads waiting to aquire a read lock go here (will be released in bulk) - - /// - /// The upgrade event. - /// - private EventWaitHandle upgradeEvent; // thread waiting to upgrade a read lock to a write lock go here (at most one) - - /// - /// The write event. - /// - private EventWaitHandle writeEvent; // threads waiting to aquire a write lock go here. - - #endregion - -#region Properties - - /// - /// Gets a value indicating whether MyLockHeld. - /// - private bool MyLockHeld - { - get - { - return this.myLock != 0; - } - } - - #endregion - -#region Public Methods - - /// - /// The acquire reader lock. - /// - /// - /// The milliseconds timeout. - /// - public void AcquireReaderLock(int millisecondsTimeout) - { - this.EnterMyLock(); - for (;;) - { - // We can enter a read lock if there are only read-locks have been given out - // and a writer is not trying to get in. - if (this.owners >= 0 && this.numWriteWaiters == 0) - { - // Good case, there is no contention, we are basically done - this.owners++; // Indicate we have another reader - break; - } - - // Drat, we need to wait. Mark that we have waiters and wait. - if (this.readEvent == null) - { - // Create the needed event - this.LazyCreateEvent(ref this.readEvent, false); - continue; // since we left the lock, start over. - } - - this.WaitOnEvent(this.readEvent, ref this.numReadWaiters, millisecondsTimeout); - } - - this.ExitMyLock(); - } - - /// - /// The acquire writer lock. - /// - /// - /// The milliseconds timeout. - /// - public void AcquireWriterLock(int millisecondsTimeout) - { - this.EnterMyLock(); - for (;;) - { - if (this.owners == 0) - { - // Good case, there is no contention, we are basically done - this.owners = -1; // indicate we have a writer. - break; - } - - // Drat, we need to wait. Mark that we have waiters and wait. - if (this.writeEvent == null) - { - // create the needed event. - this.LazyCreateEvent(ref this.writeEvent, true); - continue; // since we left the lock, start over. - } - - this.WaitOnEvent(this.writeEvent, ref this.numWriteWaiters, millisecondsTimeout); - } - - this.ExitMyLock(); - } - - /// - /// The downgrade to reader lock. - /// - /// The lock cookie. - public void DowngradeFromWriterLock(ref int lockCookie) - { - this.EnterMyLock(); - Debug.Assert(this.owners == -1, "Downgrading when no writer lock held"); - this.owners = 1; - this.ExitAndWakeUpAppropriateWaiters(); - } - - /// - /// The release reader lock. - /// - public void ReleaseReaderLock() - { - this.EnterMyLock(); - Debug.Assert(this.owners > 0, "ReleasingReaderLock: releasing lock and no read lock taken"); - --this.owners; - this.ExitAndWakeUpAppropriateWaiters(); - } - - /// - /// The release writer lock. - /// - public void ReleaseWriterLock() - { - this.EnterMyLock(); - Debug.Assert(this.owners == -1, "Calling ReleaseWriterLock when no write lock is held"); - Debug.Assert(this.numUpgradeWaiters > 0); - this.owners++; - this.ExitAndWakeUpAppropriateWaiters(); - } - - /// - /// The upgrade to writer lock. - /// - /// - /// The milliseconds timeout. - /// - /// - /// - public int UpgradeToWriterLock(int millisecondsTimeout) - { - this.EnterMyLock(); - for (;;) - { - Debug.Assert(this.owners > 0, "Upgrading when no reader lock held"); - if (this.owners == 1) - { - // Good case, there is no contention, we are basically done - this.owners = -1; // inidicate we have a writer. - break; - } - - // Drat, we need to wait. Mark that we have waiters and wait. - if (this.upgradeEvent == null) - { - // Create the needed event - this.LazyCreateEvent(ref this.upgradeEvent, false); - continue; // since we left the lock, start over. - } - - if (this.numUpgradeWaiters > 0) - { - this.ExitMyLock(); - throw new InvalidOperationException("UpgradeToWriterLock already in process. Deadlock!"); - } - - this.WaitOnEvent(this.upgradeEvent, ref this.numUpgradeWaiters, millisecondsTimeout); - } - - this.ExitMyLock(); - return 0; - } - - #endregion - -#region Methods - - /// - /// The enter my lock. - /// - private void EnterMyLock() - { - if (Interlocked.CompareExchange(ref this.myLock, 1, 0) != 0) - { - this.EnterMyLockSpin(); - } - } - - /// - /// The enter my lock spin. - /// - private void EnterMyLockSpin() - { - for (int i = 0;; i++) - { -#if !NETCF - if (i < 3 && Environment.ProcessorCount > 1) - { - Thread.SpinWait(20); // Wait a few dozen instructions to let another processor release lock. - } - else - { - Thread.Sleep(0); // Give up my quantum. - } -#else - Thread.Sleep(0); // Give up my quantum. -#endif - - if (Interlocked.CompareExchange(ref this.myLock, 1, 0) == 0) - { - return; - } - } - } - - /// - /// Determines the appropriate events to set, leaves the locks, and sets the events. - /// - private void ExitAndWakeUpAppropriateWaiters() - { - Debug.Assert(this.MyLockHeld); - - if (this.owners == 0 && this.numWriteWaiters > 0) - { - this.ExitMyLock(); // Exit before signaling to improve efficiency (wakee will need the lock) - this.writeEvent.Set(); // release one writer. - } - else if (this.owners == 1 && this.numUpgradeWaiters != 0) - { - this.ExitMyLock(); // Exit before signaling to improve efficiency (wakee will need the lock) - this.upgradeEvent.Set(); // release all upgraders (however there can be at most one). - - // two threads upgrading is a guarenteed deadlock, so we throw in that case. - } - else if (this.owners >= 0 && this.numReadWaiters != 0) - { - this.ExitMyLock(); // Exit before signaling to improve efficiency (wakee will need the lock) - this.readEvent.Set(); // release all readers. - } - else - { - this.ExitMyLock(); - } - } - - /// - /// The exit my lock. - /// - private void ExitMyLock() - { - Debug.Assert(this.myLock != 0, "Exiting spin lock that is not held"); - this.myLock = 0; - } - - /// - /// A routine for lazily creating a event outside the lock (so if errors - /// happen they are outside the lock and that we don't do much work - /// while holding a spin lock). If all goes well, reenter the lock and - /// set 'waitEvent' - /// - /// - /// The wait Event. - /// - /// - /// The make Auto Reset Event. - /// - private void LazyCreateEvent(ref EventWaitHandle waitEvent, bool makeAutoResetEvent) - { - Debug.Assert(this.MyLockHeld); - Debug.Assert(waitEvent == null); - - this.ExitMyLock(); - EventWaitHandle newEvent; - if (makeAutoResetEvent) - { - newEvent = new AutoResetEvent(false); - } - else - { - newEvent = new ManualResetEvent(false); - } - - this.EnterMyLock(); - waitEvent = newEvent; - } - - /// - /// Waits on 'waitEvent' with a timeout of 'millisceondsTimeout. - /// Before the wait 'numWaiters' is incremented and is restored before leaving this routine. - /// - /// - /// The wait Event. - /// - /// - /// The num Waiters. - /// - /// - /// The milliseconds Timeout. - /// - private void WaitOnEvent(EventWaitHandle waitEvent, ref uint numWaiters, int millisecondsTimeout) - { - Debug.Assert(this.MyLockHeld); - - waitEvent.Reset(); - numWaiters++; - - bool waitSuccessful = false; - this.ExitMyLock(); // Do the wait outside of any lock - try - { -#if !NETCF - if (!waitEvent.WaitOne(millisecondsTimeout)) - { - throw new InvalidOperationException("ReaderWriterLock timeout expired"); - } -#else - if (!waitEvent.WaitOne(millisecondsTimeout, false)) - { - throw new InvalidOperationException("ReaderWriterLock timeout expired"); - } -#endif - - waitSuccessful = true; - } - finally - { - this.EnterMyLock(); - --numWaiters; - if (!waitSuccessful) - { - // We are going to throw for some reason. Exit myLock. - this.ExitMyLock(); - } - } - } - -#endregion - } -} -#endif \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Injection/ConstructorInjector.cs b/Telerik.JustMock/AutoMock/Ninject/Injection/ConstructorInjector.cs index fa4f4a99..904e116e 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Injection/ConstructorInjector.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Injection/ConstructorInjector.cs @@ -1,17 +1,30 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Injection { /// - /// A delegate that can inject values into a constructor. + /// Represents a delegate that can inject values into a constructor. /// + /// The arguments used for the constructor. + /// An object created from the constructor. public delegate object ConstructorInjector(params object[] arguments); } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Injection/DynamicMethodInjectorFactory.cs b/Telerik.JustMock/AutoMock/Ninject/Injection/DynamicMethodInjectorFactory.cs index 9a2050f6..272d2455 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Injection/DynamicMethodInjectorFactory.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Injection/DynamicMethodInjectorFactory.cs @@ -1,27 +1,40 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#if !NO_LCG -#region Using Directives -using System; -using System.Reflection; -using System.Reflection.Emit; -using Telerik.JustMock.AutoMock.Ninject.Components; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- +#if !NO_LCG namespace Telerik.JustMock.AutoMock.Ninject.Injection { + using System; + using System.Reflection; + using System.Reflection.Emit; + + using Telerik.JustMock.AutoMock.Ninject.Components; + /// /// Creates injectors for members via s. /// public class DynamicMethodInjectorFactory : NinjectComponent, IInjectorFactory { + private static readonly MethodInfo UnboxPointer = typeof(Pointer).GetMethod("Unbox"); + /// /// Gets or creates an injector for the specified constructor. /// @@ -29,23 +42,21 @@ public class DynamicMethodInjectorFactory : NinjectComponent, IInjectorFactory /// The created injector. public ConstructorInjector Create(ConstructorInfo constructor) { - #if SILVERLIGHT - var dynamicMethod = new DynamicMethod(GetAnonymousMethodName(), typeof(object), new[] { typeof(object[]) }); - #else - var dynamicMethod = new DynamicMethod(GetAnonymousMethodName(), typeof(object), new[] { typeof(object[]) }, true); - #endif + var dynamicMethod = new DynamicMethod(GetAnonymousMethodName(), typeof(object), new[] { typeof(object[]) }, constructor.Module, true); - ILGenerator il = dynamicMethod.GetILGenerator(); + var il = dynamicMethod.GetILGenerator(); EmitLoadMethodArguments(il, constructor); il.Emit(OpCodes.Newobj, constructor); if (constructor.ReflectedType.IsValueType) + { il.Emit(OpCodes.Box, constructor.ReflectedType); + } il.Emit(OpCodes.Ret); - return (ConstructorInjector) dynamicMethod.CreateDelegate(typeof(ConstructorInjector)); + return (ConstructorInjector)dynamicMethod.CreateDelegate(typeof(ConstructorInjector)); } /// @@ -55,13 +66,13 @@ public ConstructorInjector Create(ConstructorInfo constructor) /// The created injector. public PropertyInjector Create(PropertyInfo property) { - #if NO_SKIP_VISIBILITY - var dynamicMethod = new DynamicMethod(GetAnonymousMethodName(), typeof(void), new[] { typeof(object), typeof(object) }); - #else - var dynamicMethod = new DynamicMethod(GetAnonymousMethodName(), typeof(void), new[] { typeof(object), typeof(object) }, true); - #endif - - ILGenerator il = dynamicMethod.GetILGenerator(); +#if NO_SKIP_VISIBILITY + var dynamicMethod = new DynamicMethod(GetAnonymousMethodName(), typeof(void), new[] { typeof(object), typeof(object), property.Module }); +#else + var dynamicMethod = new DynamicMethod(GetAnonymousMethodName(), typeof(void), new[] { typeof(object), typeof(object) }, property.Module, true); +#endif + + var il = dynamicMethod.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); EmitUnboxOrCast(il, property.DeclaringType); @@ -69,16 +80,12 @@ public PropertyInjector Create(PropertyInfo property) il.Emit(OpCodes.Ldarg_1); EmitUnboxOrCast(il, property.PropertyType); - #if !SILVERLIGHT - bool injectNonPublic = Settings.InjectNonPublic; - #else - const bool injectNonPublic = false; - #endif // !SILVERLIGHT + var injectNonPublic = this.Settings.InjectNonPublic; EmitMethodCall(il, property.GetSetMethod(injectNonPublic)); il.Emit(OpCodes.Ret); - return (PropertyInjector) dynamicMethod.CreateDelegate(typeof(PropertyInjector)); + return (PropertyInjector)dynamicMethod.CreateDelegate(typeof(PropertyInjector)); } /// @@ -88,13 +95,13 @@ public PropertyInjector Create(PropertyInfo property) /// The created injector. public MethodInjector Create(MethodInfo method) { - #if NO_SKIP_VISIBILITY - var dynamicMethod = new DynamicMethod(GetAnonymousMethodName(), typeof(void), new[] { typeof(object), typeof(object[]) }); - #else - var dynamicMethod = new DynamicMethod(GetAnonymousMethodName(), typeof(void), new[] { typeof(object), typeof(object[]) }, true); - #endif +#if NO_SKIP_VISIBILITY + var dynamicMethod = new DynamicMethod(GetAnonymousMethodName(), typeof(void), new[] { typeof(object), typeof(object[]) }, method.Module); +#else + var dynamicMethod = new DynamicMethod(GetAnonymousMethodName(), typeof(void), new[] { typeof(object), typeof(object[]) }, method.Module, true); +#endif - ILGenerator il = dynamicMethod.GetILGenerator(); + var il = dynamicMethod.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); EmitUnboxOrCast(il, method.DeclaringType); @@ -103,18 +110,20 @@ public MethodInjector Create(MethodInfo method) EmitMethodCall(il, method); if (method.ReturnType != typeof(void)) + { il.Emit(OpCodes.Pop); + } il.Emit(OpCodes.Ret); - return (MethodInjector) dynamicMethod.CreateDelegate(typeof(MethodInjector)); + return (MethodInjector)dynamicMethod.CreateDelegate(typeof(MethodInjector)); } private static void EmitLoadMethodArguments(ILGenerator il, MethodBase targetMethod) { - ParameterInfo[] parameters = targetMethod.GetParameters(); - OpCode ldargOpcode = targetMethod is ConstructorInfo ? OpCodes.Ldarg_0 : OpCodes.Ldarg_1; - + var parameters = targetMethod.GetParameters(); + var ldargOpcode = targetMethod is ConstructorInfo ? OpCodes.Ldarg_0 : OpCodes.Ldarg_1; + for (int idx = 0; idx < parameters.Length; idx++) { il.Emit(ldargOpcode); @@ -127,14 +136,24 @@ private static void EmitLoadMethodArguments(ILGenerator il, MethodBase targetMet private static void EmitMethodCall(ILGenerator il, MethodInfo method) { - OpCode opCode = method.IsFinal ? OpCodes.Call : OpCodes.Callvirt; + var opCode = method.IsFinal ? OpCodes.Call : OpCodes.Callvirt; il.Emit(opCode, method); } private static void EmitUnboxOrCast(ILGenerator il, Type type) { - OpCode opCode = type.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass; - il.Emit(opCode, type); + if (type.IsValueType) + { + il.Emit(OpCodes.Unbox_Any, type); + } + else if (type.IsPointer) + { + il.Emit(OpCodes.Call, UnboxPointer); + } + else + { + il.Emit(OpCodes.Castclass, type); + } } private static string GetAnonymousMethodName() diff --git a/Telerik.JustMock/AutoMock/Ninject/Injection/IInjectorFactory.cs b/Telerik.JustMock/AutoMock/Ninject/Injection/IInjectorFactory.cs index e8936929..b0c50c83 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Injection/IInjectorFactory.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Injection/IInjectorFactory.cs @@ -1,20 +1,30 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Components; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Injection { + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Components; + /// /// Creates injectors from members. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Injection/MethodInjector.cs b/Telerik.JustMock/AutoMock/Ninject/Injection/MethodInjector.cs index 98e54476..a419a377 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Injection/MethodInjector.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Injection/MethodInjector.cs @@ -1,16 +1,30 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- + namespace Telerik.JustMock.AutoMock.Ninject.Injection { /// - /// A delegate that can inject values into a method. + /// Represents a delegate that can inject values into a method. /// + /// The method info. + /// The arguments used for the method. public delegate void MethodInjector(object target, params object[] arguments); } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Injection/PropertyInjector.cs b/Telerik.JustMock/AutoMock/Ninject/Injection/PropertyInjector.cs index 30fed456..368a38cb 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Injection/PropertyInjector.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Injection/PropertyInjector.cs @@ -1,16 +1,30 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- + namespace Telerik.JustMock.AutoMock.Ninject.Injection { /// - /// A delegate that can inject values into a property. + /// Represents a delegate that can inject values into a property. /// + /// The property info. + /// The value to be injected to the property. public delegate void PropertyInjector(object target, object value); } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Injection/ReflectionInjectorFactory.cs b/Telerik.JustMock/AutoMock/Ninject/Injection/ReflectionInjectorFactory.cs index d9e797b0..63ef58cd 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Injection/ReflectionInjectorFactory.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Injection/ReflectionInjectorFactory.cs @@ -1,20 +1,30 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Components; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Injection { + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Components; + /// /// Creates injectors from members via reflective invocation. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/KernelBase.cs b/Telerik.JustMock/AutoMock/Ninject/KernelBase.cs index a5432cee..0febf571 100644 --- a/Telerik.JustMock/AutoMock/Ninject/KernelBase.cs +++ b/Telerik.JustMock/AutoMock/Ninject/KernelBase.cs @@ -1,10 +1,23 @@ -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject { @@ -12,6 +25,7 @@ namespace Telerik.JustMock.AutoMock.Ninject using System.Collections.Generic; using System.Linq; using System.Reflection; + using Telerik.JustMock.AutoMock.Ninject.Activation; using Telerik.JustMock.AutoMock.Ninject.Activation.Blocks; using Telerik.JustMock.AutoMock.Ninject.Activation.Caching; @@ -31,17 +45,16 @@ namespace Telerik.JustMock.AutoMock.Ninject /// public abstract class KernelBase : BindingRoot, IKernel { - /// - /// Lock used when adding missing bindings. - /// - protected readonly object HandleMissingBindingLockObject = new object(); - + private readonly object handleMissingBindingLockObject = new object(); + private readonly Multimap bindings = new Multimap(); - private readonly Multimap bindingCache = new Multimap(); + private readonly Dictionary> bindingCache = new Dictionary>(); private readonly Dictionary modules = new Dictionary(); + private readonly IBindingPrecedenceComparer bindingPrecedenceComparer; + /// /// Initializes a new instance of the class. /// @@ -88,15 +101,16 @@ protected KernelBase(IComponentContainer components, INinjectSettings settings, this.AddComponents(); + this.bindingPrecedenceComparer = this.Components.Get(); + this.Bind().ToConstant(this).InTransientScope(); this.Bind().ToConstant(this).InTransientScope(); -#if !NO_ASSEMBLY_SCANNING if (this.Settings.LoadExtensions) { this.Load(this.Settings.ExtensionSearchPatterns); } -#endif + this.Load(modules); } @@ -113,9 +127,10 @@ protected KernelBase(IComponentContainer components, INinjectSettings settings, /// /// Releases resources held by the object. /// + /// True if called manually, otherwise by GC. public override void Dispose(bool disposing) { - if (disposing && !IsDisposed) + if (disposing && !this.IsDisposed) { if (this.Components != null) { @@ -168,7 +183,9 @@ public override void RemoveBinding(IBinding binding) this.bindings.Remove(binding.Service, binding); lock (this.bindingCache) + { this.bindingCache.Clear(); + } } /// @@ -206,10 +223,8 @@ public void Load(IEnumerable m) { throw new NotSupportedException(ExceptionFormatter.ModulesWithNullOrEmptyNamesAreNotSupported()); } - - INinjectModule existingModule; - if (this.modules.TryGetValue(module.Name, out existingModule)) + if (this.modules.TryGetValue(module.Name, out INinjectModule existingModule)) { throw new NotSupportedException(ExceptionFormatter.ModuleWithSameNameIsAlreadyLoaded(module, existingModule)); } @@ -225,7 +240,6 @@ public void Load(IEnumerable m) } } -#if !NO_ASSEMBLY_SCANNING /// /// Loads modules from the files that match the specified pattern(s). /// @@ -244,7 +258,6 @@ public void Load(IEnumerable assemblies) { this.Load(assemblies.SelectMany(asm => asm.GetNinjectModules())); } -#endif //!NO_ASSEMBLY_SCANNING /// /// Unloads the plugin with the specified name. @@ -254,9 +267,7 @@ public void Unload(string name) { Ensure.ArgumentNotNullOrEmpty(name, "name"); - INinjectModule module; - - if (!this.modules.TryGetValue(name, out module)) + if (!this.modules.TryGetValue(name, out INinjectModule module)) { throw new NotSupportedException(ExceptionFormatter.NoModuleLoadedWithTheSpecifiedName(name)); } @@ -337,57 +348,7 @@ public virtual bool CanResolve(IRequest request, bool ignoreImplicitBindings) /// An enumerator of instances that match the request. public virtual IEnumerable Resolve(IRequest request) { - Ensure.ArgumentNotNull(request, "request"); - - var bindingPrecedenceComparer = this.GetBindingPrecedenceComparer(); - var resolveBindings = Enumerable.Empty(); - - if (this.CanResolve(request) || this.HandleMissingBinding(request)) - { - resolveBindings = this.GetBindings(request.Service) - .Where(this.SatifiesRequest(request)); - - } - - if (!resolveBindings.Any()) - { - if (request.IsOptional) - { - return Enumerable.Empty(); - } - - throw new ActivationException(ExceptionFormatter.CouldNotResolveBinding(request)); - } - - if (request.IsUnique) - { - resolveBindings = resolveBindings.OrderByDescending(b => b, bindingPrecedenceComparer).ToList(); - var model = resolveBindings.First(); // the type (conditonal, implicit, etc) of binding we'll return - resolveBindings = - resolveBindings.TakeWhile(binding => bindingPrecedenceComparer.Compare(binding, model) == 0); - - if (resolveBindings.Count() > 1) - { - if (request.IsOptional) - { - return Enumerable.Empty(); - } - - var formattedBindings = - from binding in resolveBindings - let context = this.CreateContext(request, binding) - select binding.Format(context); - throw new ActivationException(ExceptionFormatter.CouldNotUniquelyResolveBinding(request, formattedBindings.ToArray())); - } - } - - if(resolveBindings.Any(binding => !binding.IsImplicit)) - { - resolveBindings = resolveBindings.Where(binding => !binding.IsImplicit); - } - - return resolveBindings - .Select(binding => this.CreateContext(request, binding).Resolve()); + return this.Resolve(request, true, false); } /// @@ -431,9 +392,12 @@ public virtual IEnumerable GetBindings(Type service) { var resolvers = this.Components.GetAll(); - resolvers + var compiledBindings = resolvers .SelectMany(resolver => resolver.Resolve(this.bindings, service)) - .Map(binding => this.bindingCache.Add(service, binding)); + .OrderByDescending(b => b, this.bindingPrecedenceComparer).ToList(); + this.bindingCache.Add(service, compiledBindings); + + return compiledBindings; } return this.bindingCache[service]; @@ -441,12 +405,15 @@ public virtual IEnumerable GetBindings(Type service) } /// - /// Returns an IComparer that is used to determine resolution precedence. + /// Gets the service object of the specified type. /// - /// An IComparer that is used to determine resolution precedence. - protected virtual IComparer GetBindingPrecedenceComparer() + /// The service type. + /// The service object + object IServiceProvider.GetService(Type service) { - return new BindingPrecedenceComparer(); + return this.Settings.ThrowOnGetServiceNotFound + ? this.Get(service) + : this.TryGet(service); } /// @@ -464,17 +431,6 @@ protected virtual Func SatifiesRequest(IRequest request) /// protected abstract void AddComponents(); - /// - /// Attempts to handle a missing binding for a service. - /// - /// The service. - /// True if the missing binding can be handled; otherwise false. - [Obsolete] - protected virtual bool HandleMissingBinding(Type service) - { - return false; - } - /// /// Attempts to handle a missing binding for a request. /// @@ -484,15 +440,8 @@ protected virtual bool HandleMissingBinding(IRequest request) { Ensure.ArgumentNotNull(request, "request"); -#pragma warning disable 612,618 - if (this.HandleMissingBinding(request.Service)) - { - return true; - } -#pragma warning restore 612,618 - var components = this.Components.GetAll(); - + // Take the first set of bindings that resolve. var bindings = components .Select(c => c.Resolve(this.bindings, request).ToList()) @@ -503,7 +452,7 @@ protected virtual bool HandleMissingBinding(IRequest request) return false; } - lock (this.HandleMissingBindingLockObject) + lock (this.handleMissingBindingLockObject) { if (!this.CanResolve(request)) { @@ -515,21 +464,6 @@ protected virtual bool HandleMissingBinding(IRequest request) return true; } - /// - /// Returns a value indicating whether the specified service is self-bindable. - /// - /// The service. - /// if the type is self-bindable; otherwise . - [Obsolete] - protected virtual bool TypeIsSelfBindable(Type service) - { - return !service.IsInterface - && !service.IsAbstract - && !service.IsValueType - && service != typeof(string) - && !service.ContainsGenericParameters; - } - /// /// Creates a context for the specified request and binding. /// @@ -544,46 +478,122 @@ protected virtual IContext CreateContext(IRequest request, IBinding binding) return new Context(this, request, binding, this.Components.Get(), this.Components.Get(), this.Components.Get()); } - private void AddBindings(IEnumerable bindings) + private IEnumerable Resolve(IRequest request, bool handleMissingBindings, bool filterImplicitBindings) { - bindings.Map(binding => this.bindings.Add(binding.Service, binding)); + void UpdateRequest(Type service) + { + if (request.ParentRequest == null) + { + request = this.CreateRequest(service, null, request.Parameters.Where(p => p.ShouldInherit), true, false); + } + else + { + request = request.ParentRequest.CreateChild(service, request.ParentContext, request.Target); + request.IsOptional = true; + } + } - lock (this.bindingCache) - this.bindingCache.Clear(); - } + if (request.Service.IsArray) + { + var service = request.Service.GetElementType(); - object IServiceProvider.GetService(Type service) - { - return this.Get(service); - } + UpdateRequest(service); - private class BindingPrecedenceComparer : IComparer - { - public int Compare(IBinding x, IBinding y) + return new[] { this.Resolve(request, false, true).CastSlow(service).ToArraySlow(service) }; + } + + if (request.Service.IsGenericType) + { + var gtd = request.Service.GetGenericTypeDefinition(); + + if (gtd == typeof(List<>) || gtd == typeof(IList<>) || gtd == typeof(ICollection<>)) + { + var service = request.Service.GenericTypeArguments[0]; + + UpdateRequest(service); + + return new[] { this.Resolve(request, false, true).CastSlow(service).ToListSlow(service) }; + } + + if (gtd == typeof(IEnumerable<>)) + { + var service = request.Service.GenericTypeArguments[0]; + + UpdateRequest(service); + + return new[] { this.Resolve(request, false, true).CastSlow(service) }; + } + } + + var satisfiedBindings = this.GetBindings(request.Service) + .Where(this.SatifiesRequest(request)); + + if (filterImplicitBindings) + { + satisfiedBindings = satisfiedBindings.Where(binding => binding.IsImplicit == false); + } + + var satisfiedBindingEnumerator = satisfiedBindings.GetEnumerator(); + + if (!satisfiedBindingEnumerator.MoveNext()) + { + if (handleMissingBindings && this.HandleMissingBinding(request)) + { + return this.Resolve(request, false, false); + } + + if (request.IsOptional) + { + return Enumerable.Empty(); + } + + throw new ActivationException(ExceptionFormatter.CouldNotResolveBinding(request)); + } + + if (request.IsUnique) { - if (x == y) + var selectedBinding = satisfiedBindingEnumerator.Current; + + if (satisfiedBindingEnumerator.MoveNext() && + this.bindingPrecedenceComparer.Compare(selectedBinding, satisfiedBindingEnumerator.Current) == 0) { - return 0; + if (request.IsOptional && !request.ForceUnique) + { + return Enumerable.Empty(); + } + + var formattedBindings = + from binding in satisfiedBindings + let context = this.CreateContext(request, binding) + select binding.Format(context); + + throw new ActivationException(ExceptionFormatter.CouldNotUniquelyResolveBinding( + request, + formattedBindings.ToArray())); + } + + return new[] { this.CreateContext(request, selectedBinding).Resolve() }; + } + else + { + if (satisfiedBindings.Any(binding => !binding.IsImplicit)) + { + satisfiedBindings = satisfiedBindings.Where(binding => !binding.IsImplicit); } - // Each function represents a level of precedence. - var funcs = new List> - { - b => b != null, // null bindings should never happen, but just in case - b => b.IsConditional, // conditional bindings > unconditional - b => !b.Service.ContainsGenericParameters, // closed generics > open generics - b => !b.IsImplicit, // explicit bindings > implicit - }; - - var q = from func in funcs - let xVal = func(x) - where xVal != func(y) - select xVal ? 1 : -1; - - // returns the value of the first function that represents a difference - // between the bindings, or else returns 0 (equal) - return q.FirstOrDefault(); + return satisfiedBindings + .Select(binding => this.CreateContext(request, binding).Resolve()); + } + } + + private void AddBindings(IEnumerable bindings) + { + bindings.Map(binding => this.bindings.Add(binding.Service, binding)); + + lock (this.bindingCache) + { + this.bindingCache.Clear(); } } } -} \ No newline at end of file +} diff --git a/Telerik.JustMock/AutoMock/Ninject/Modules/AssemblyNameRetriever.cs b/Telerik.JustMock/AutoMock/Ninject/Modules/AssemblyNameRetriever.cs index 56e1d835..ec4584f9 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Modules/AssemblyNameRetriever.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Modules/AssemblyNameRetriever.cs @@ -1,10 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -17,9 +17,8 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- -#if !NO_ASSEMBLY_SCANNING namespace Telerik.JustMock.AutoMock.Ninject.Modules { using System; @@ -43,7 +42,7 @@ public class AssemblyNameRetriever : NinjectComponent, IAssemblyNameRetriever /// All assembly names of the assemblies in the given files that match the filter. public IEnumerable GetAssemblyNames(IEnumerable filenames, Predicate filter) { -#if !NO_APPDOMAIN_ISOLATION +#if !NO_ASSEMBLY_SCANNING var assemblyCheckerType = typeof(AssemblyChecker); var temporaryDomain = CreateTemporaryAppDomain(); try @@ -60,10 +59,10 @@ public IEnumerable GetAssemblyNames(IEnumerable filenames, } #else return new AssemblyChecker().GetAssemblyNames(filenames, filter); -#endif // !NO_APPDOMAIN_ISOLATION +#endif } -#if !NO_APPDOMAIN_ISOLATION +#if !NO_ASSEMBLY_SCANNING /// /// Creates a temporary app domain. /// @@ -75,7 +74,7 @@ private static AppDomain CreateTemporaryAppDomain() AppDomain.CurrentDomain.Evidence, AppDomain.CurrentDomain.SetupInformation); } -#endif // !NO_APPDOMAIN_ISOLATION +#endif /// /// This class is loaded into the temporary appdomain to load and check if the assemblies match the filter. @@ -98,20 +97,33 @@ public IEnumerable GetAssemblyNames(IEnumerable filenames, { try { - // .NET Core -> creates a new (anonymous) load context to load the assembly into. - // https://github.com/dotnet/coreclr/blob/master/Documentation/design-docs/assemblyloadcontext.md#assembly-load-apis-and-loadcontext - assembly = Assembly.LoadFile(filename); + assembly = Assembly.LoadFrom(filename); } catch (BadImageFormatException) { continue; } - - if (filter(assembly)) + } + else + { + try { - result.Add(assembly.GetName(false)); + assembly = Assembly.Load(filename); + } + catch (FileLoadException) + { + continue; + } + catch (FileNotFoundException) + { + continue; } } + + if (filter(assembly)) + { + result.Add(assembly.GetName(false)); + } } return result; @@ -119,4 +131,3 @@ public IEnumerable GetAssemblyNames(IEnumerable filenames, } } } -#endif diff --git a/Telerik.JustMock/AutoMock/Ninject/Modules/CompiledModuleLoaderPlugin.cs b/Telerik.JustMock/AutoMock/Ninject/Modules/CompiledModuleLoaderPlugin.cs index fba18a39..1423c8b9 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Modules/CompiledModuleLoaderPlugin.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Modules/CompiledModuleLoaderPlugin.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,9 +17,8 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- -#if !NO_ASSEMBLY_SCANNING namespace Telerik.JustMock.AutoMock.Ninject.Modules { using System.Collections.Generic; @@ -31,21 +28,21 @@ namespace Telerik.JustMock.AutoMock.Ninject.Modules using Telerik.JustMock.AutoMock.Ninject.Components; using Telerik.JustMock.AutoMock.Ninject.Infrastructure; using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; - + /// /// Loads modules from compiled assemblies. /// public class CompiledModuleLoaderPlugin : NinjectComponent, IModuleLoaderPlugin { /// - /// The assembly name retriever. + /// The file extensions that are supported. /// - private readonly IAssemblyNameRetriever assemblyNameRetriever; + private static readonly string[] Extensions = { ".dll" }; /// - /// The file extensions that are supported. + /// The assembly name retriever. /// - private static readonly string[] Extensions = new[] { ".dll" }; + private readonly IAssemblyNameRetriever assemblyNameRetriever; /// /// Initializes a new instance of the class. @@ -55,6 +52,8 @@ public class CompiledModuleLoaderPlugin : NinjectComponent, IModuleLoaderPlugin public CompiledModuleLoaderPlugin(IKernel kernel, IAssemblyNameRetriever assemblyNameRetriever) { Ensure.ArgumentNotNull(kernel, "kernel"); + Ensure.ArgumentNotNull(assemblyNameRetriever, "assemblyNameRetriever"); + this.Kernel = kernel; this.assemblyNameRetriever = assemblyNameRetriever; } @@ -82,5 +81,4 @@ public void LoadModules(IEnumerable filenames) this.Kernel.Load(assembliesWithModules.Select(asm => Assembly.Load(asm))); } } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Modules/IAssemblyNameRetriever.cs b/Telerik.JustMock/AutoMock/Ninject/Modules/IAssemblyNameRetriever.cs index f0615207..3201ba63 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Modules/IAssemblyNameRetriever.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Modules/IAssemblyNameRetriever.cs @@ -1,10 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -17,9 +17,8 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- -#if !NO_ASSEMBLY_SCANNING namespace Telerik.JustMock.AutoMock.Ninject.Modules { using System; @@ -41,5 +40,4 @@ public interface IAssemblyNameRetriever : INinjectComponent /// All assembly names of the assemblies in the given files that match the filter. IEnumerable GetAssemblyNames(IEnumerable filenames, Predicate filter); } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Modules/IModuleLoader.cs b/Telerik.JustMock/AutoMock/Ninject/Modules/IModuleLoader.cs index ed7131a7..9142ebba 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Modules/IModuleLoader.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Modules/IModuleLoader.cs @@ -1,21 +1,30 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#if !SILVERLIGHT -#region Using Directives -using System; -using System.Collections.Generic; -using Telerik.JustMock.AutoMock.Ninject.Components; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Modules { + using System.Collections.Generic; + + using Telerik.JustMock.AutoMock.Ninject.Components; + /// /// Finds modules defined in external files. /// @@ -27,5 +36,4 @@ public interface IModuleLoader : INinjectComponent /// The patterns to search. void LoadModules(IEnumerable patterns); } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Modules/IModuleLoaderPlugin.cs b/Telerik.JustMock/AutoMock/Ninject/Modules/IModuleLoaderPlugin.cs index c04f8c7b..b1044eca 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Modules/IModuleLoaderPlugin.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Modules/IModuleLoaderPlugin.cs @@ -1,21 +1,30 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#if !SILVERLIGHT -#region Using Directives -using System; -using System.Collections.Generic; -using Telerik.JustMock.AutoMock.Ninject.Components; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Modules { + using System.Collections.Generic; + + using Telerik.JustMock.AutoMock.Ninject.Components; + /// /// Loads modules at runtime by searching external files. /// @@ -32,5 +41,4 @@ public interface IModuleLoaderPlugin : INinjectComponent /// The names of the files to load modules from. void LoadModules(IEnumerable filenames); } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Modules/INinjectModule.cs b/Telerik.JustMock/AutoMock/Ninject/Modules/INinjectModule.cs index d66516bc..b163b031 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Modules/INinjectModule.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Modules/INinjectModule.cs @@ -1,20 +1,28 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Syntax; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Modules { + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + /// /// A pluggable unit that can be loaded into an . /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Modules/ModuleLoader.cs b/Telerik.JustMock/AutoMock/Ninject/Modules/ModuleLoader.cs index cf1f10d4..4c0e39a1 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Modules/ModuleLoader.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Modules/ModuleLoader.cs @@ -1,34 +1,39 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#if !NO_ASSEMBLY_SCANNING -#region Using Directives -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using Telerik.JustMock.AutoMock.Ninject.Components; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Modules { + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + + using Telerik.JustMock.AutoMock.Ninject.Components; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + /// /// Automatically finds and loads modules from assemblies. /// public class ModuleLoader : NinjectComponent, IModuleLoader { - /// - /// Gets or sets the kernel into which modules will be loaded. - /// - public IKernel Kernel { get; private set; } - /// /// Initializes a new instance of the class. /// @@ -36,16 +41,22 @@ public class ModuleLoader : NinjectComponent, IModuleLoader public ModuleLoader(IKernel kernel) { Ensure.ArgumentNotNull(kernel, "kernel"); - Kernel = kernel; + + this.Kernel = kernel; } + /// + /// Gets the kernel into which modules will be loaded. + /// + public IKernel Kernel { get; private set; } + /// /// Loads any modules found in the files that match the specified patterns. /// /// The patterns to search. public void LoadModules(IEnumerable patterns) { - var plugins = Kernel.Components.GetAll(); + var plugins = this.Kernel.Components.GetAll(); var fileGroups = patterns .SelectMany(pattern => GetFilesMatchingPattern(pattern)) @@ -53,11 +64,13 @@ public void LoadModules(IEnumerable patterns) foreach (var fileGroup in fileGroups) { - string extension = fileGroup.Key; - IModuleLoaderPlugin plugin = plugins.Where(p => p.SupportedExtensions.Contains(extension)).FirstOrDefault(); + var extension = fileGroup.Key; + var plugin = plugins.Where(p => p.SupportedExtensions.Contains(extension)).FirstOrDefault(); if (plugin != null) + { plugin.LoadModules(fileGroup); + } } } @@ -71,7 +84,8 @@ private static IEnumerable NormalizePaths(string path) { return Path.IsPathRooted(path) ? new[] { Path.GetFullPath(path) } - : GetBaseDirectories().Select(baseDirectory => Path.Combine(baseDirectory, path)); + : GetBaseDirectories().Select(baseDirectory => Path.Combine(baseDirectory, path)) + .Where(Directory.Exists); } private static IEnumerable GetBaseDirectories() @@ -79,11 +93,10 @@ private static IEnumerable GetBaseDirectories() var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; var searchPath = AppDomain.CurrentDomain.RelativeSearchPath; - return String.IsNullOrEmpty(searchPath) - ? new[] {baseDirectory} - : searchPath.Split(new[] {Path.PathSeparator}, StringSplitOptions.RemoveEmptyEntries) + return string.IsNullOrEmpty(searchPath) + ? new[] { baseDirectory } + : searchPath.Split(new[] { Path.PathSeparator }, StringSplitOptions.RemoveEmptyEntries) .Select(path => Path.Combine(baseDirectory, path)); } } } -#endif //!NO_ASSEMBLY_SCANNING \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Modules/NinjectModule.cs b/Telerik.JustMock/AutoMock/Ninject/Modules/NinjectModule.cs index c74859dd..a61bafde 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Modules/NinjectModule.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Modules/NinjectModule.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Modules { @@ -54,7 +52,7 @@ protected NinjectModule() /// public virtual string Name { - get { return GetType().FullName; } + get { return this.GetType().FullName; } } /// @@ -73,7 +71,7 @@ protected override IKernel KernelInstance return this.Kernel; } } - + /// /// Called when the module is loaded into a kernel. /// @@ -81,6 +79,7 @@ protected override IKernel KernelInstance public void OnLoad(IKernel kernel) { Ensure.ArgumentNotNull(kernel, "kernel"); + this.Kernel = kernel; this.Load(); } @@ -92,6 +91,7 @@ public void OnLoad(IKernel kernel) public void OnUnload(IKernel kernel) { Ensure.ArgumentNotNull(kernel, "kernel"); + this.Unload(); this.Bindings.Map(this.Kernel.RemoveBinding); this.Kernel = null; diff --git a/Telerik.JustMock/AutoMock/Ninject/NinjectSettings.cs b/Telerik.JustMock/AutoMock/Ninject/NinjectSettings.cs index 894c198e..478efbad 100644 --- a/Telerik.JustMock/AutoMock/Ninject/NinjectSettings.cs +++ b/Telerik.JustMock/AutoMock/Ninject/NinjectSettings.cs @@ -1,36 +1,46 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using Telerik.JustMock.AutoMock.Ninject.Activation; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; - -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject { + using System; + using System.Collections.Generic; + + using Telerik.JustMock.AutoMock.Ninject.Activation; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + /// /// Contains configuration options for Ninject. /// public class NinjectSettings : INinjectSettings { - private readonly Dictionary _values = new Dictionary(); + private readonly Dictionary values = new Dictionary(); /// /// Gets or sets the attribute that indicates that a member should be injected. /// public Type InjectAttribute { - get { return Get("InjectAttribute", typeof(InjectAttribute)); } - set { Set("InjectAttribute", value); } + get { return this.Get("InjectAttribute", typeof(InjectAttribute)); } + set { this.Set("InjectAttribute", value); } } /// @@ -38,8 +48,8 @@ public Type InjectAttribute /// public TimeSpan CachePruningInterval { - get { return Get("CachePruningInterval", TimeSpan.FromSeconds(30)); } - set { Set("CachePruningInterval", value); } + get { return this.Get("CachePruningInterval", TimeSpan.FromSeconds(30)); } + set { this.Set("CachePruningInterval", value); } } /// @@ -47,18 +57,17 @@ public TimeSpan CachePruningInterval /// public Func DefaultScopeCallback { - get { return Get("DefaultScopeCallback", StandardScopeCallbacks.Transient); } - set { Set("DefaultScopeCallback", value); } + get { return this.Get("DefaultScopeCallback", StandardScopeCallbacks.Transient); } + set { this.Set("DefaultScopeCallback", value); } } - #if !NO_ASSEMBLY_SCANNING /// /// Gets or sets a value indicating whether the kernel should automatically load extensions at startup. /// public bool LoadExtensions { - get { return Get("LoadExtensions", true); } - set { Set("LoadExtensions", value); } + get { return this.Get("LoadExtensions", true); } + set { this.Set("LoadExtensions", value); } } /// @@ -66,38 +75,36 @@ public bool LoadExtensions /// public string[] ExtensionSearchPatterns { - get { return Get("ExtensionSearchPatterns", new [] { "Ninject.Extensions.*.dll", "Ninject.Web*.dll" }); } - set { Set("ExtensionSearchPatterns", value); } + get { return this.Get("ExtensionSearchPatterns", new[] { "Ninject.Extensions.*.dll", "Ninject.Web*.dll" }); } + set { this.Set("ExtensionSearchPatterns", value); } } - #endif //!NO_ASSEMBLY_SCANNING - #if !NO_LCG +#if !NO_LCG /// - /// Gets a value indicating whether Ninject should use reflection-based injection instead of + /// Gets or sets a value indicating whether Ninject should use reflection-based injection instead of /// the (usually faster) lightweight code generation system. /// public bool UseReflectionBasedInjection { - get { return Get("UseReflectionBasedInjection", false); } - set { Set("UseReflectionBasedInjection", value); } + get { return this.Get("UseReflectionBasedInjection", false); } + set { this.Set("UseReflectionBasedInjection", value); } } - #endif //!NO_LCG +#endif //!NO_LCG - #if !SILVERLIGHT /// - /// Gets a value indicating whether Ninject should inject non public members. + /// Gets or sets a value indicating whether Ninject should inject non public members. /// public bool InjectNonPublic { - get { return Get("InjectNonPublic", false); } - set { Set("InjectNonPublic", value); } + get { return this.Get("InjectNonPublic", false); } + set { this.Set("InjectNonPublic", value); } } /// - /// Gets a value indicating whether Ninject should inject private properties of base classes. + /// Gets or sets a value indicating whether Ninject should inject private properties of base classes. /// /// - /// Activating this setting has an impact on the performance. It is recomended not + /// Activating this setting has an impact on the performance. It is recommended not /// to use this feature and use constructor injection instead. /// public bool InjectParentPrivateProperties @@ -105,7 +112,6 @@ public bool InjectParentPrivateProperties get { return this.Get("InjectParentPrivateProperties", false); } set { this.Set("InjectParentPrivateProperties", value); } } - #endif //!SILVERLIGHT /// /// Gets or sets a value indicating whether the activation cache is disabled. @@ -115,7 +121,7 @@ public bool InjectParentPrivateProperties /// Bind{IA}().ToMethod(ctx => kernel.Get{IA}(); /// /// - /// true if activation cache is disabled; otherwise, false. + /// true if activation cache is disabled; otherwise, false. /// public bool ActivationCacheDisabled { @@ -128,7 +134,7 @@ public bool ActivationCacheDisabled /// By default this is disabled and whenever a provider returns null an exception is thrown. /// /// - /// true if null is allowed as injected value otherwise false. + /// true if null is allowed as injected value otherwise false. /// public bool AllowNullInjection { @@ -136,6 +142,18 @@ public bool AllowNullInjection set { this.Set("AllowNullInjection", value); } } + /// + /// Gets or sets a value indicating whether the old (<= 3.3.4) behavior of + /// should be used which throws an exception if the requested service cannot be found. Note that the documentation + /// of that method https://docs.microsoft.com/en-us/dotnet/api/system.iserviceprovider.getservice?view=netframework-4.6.2 + /// states that the method should return if there is no such service. + /// + public bool ThrowOnGetServiceNotFound + { + get { return this.Get("ThrowOnGetServiceNotFound", false); } + set { this.Set("ThrowOnGetServiceNotFound", value); } + } + /// /// Gets the value for the specified key. /// @@ -145,8 +163,7 @@ public bool AllowNullInjection /// The value, or the default value if none was found. public T Get(string key, T defaultValue) { - object value; - return _values.TryGetValue(key, out value) ? (T)value : defaultValue; + return this.values.TryGetValue(key, out object value) ? (T)value : defaultValue; } /// @@ -156,7 +173,7 @@ public T Get(string key, T defaultValue) /// The setting's value. public void Set(string key, object value) { - _values[key] = value; + this.values[key] = value; } } -} +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Parameters/ConstructorArgument.cs b/Telerik.JustMock/AutoMock/Ninject/Parameters/ConstructorArgument.cs index 0a853c79..3b07f685 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Parameters/ConstructorArgument.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Parameters/ConstructorArgument.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Parameters { @@ -102,7 +100,7 @@ public ConstructorArgument(string name, Func valueCal /// The context. /// The target. /// - /// Tre if the parameter applies in the specified context to the specified target. + /// True if the parameter applies in the specified context to the specified target. /// /// /// Only one parameter may return true. diff --git a/Telerik.JustMock/AutoMock/Ninject/Parameters/IConstructorArgument.cs b/Telerik.JustMock/AutoMock/Ninject/Parameters/IConstructorArgument.cs index 38c2c6e0..bc2f09cc 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Parameters/IConstructorArgument.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Parameters/IConstructorArgument.cs @@ -1,4 +1,25 @@ -namespace Telerik.JustMock.AutoMock.Ninject.Parameters +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- + +namespace Telerik.JustMock.AutoMock.Ninject.Parameters { using Telerik.JustMock.AutoMock.Ninject.Activation; using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; @@ -16,7 +37,7 @@ public interface IConstructorArgument : IParameter /// /// The context. /// The target. - /// Tre if the parameter applies in the specified context to the specified target. + /// True if the parameter applies in the specified context to the specified target. bool AppliesToTarget(IContext context, ITarget target); } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Parameters/IParameter.cs b/Telerik.JustMock/AutoMock/Ninject/Parameters/IParameter.cs index 741271f9..09a0bbe7 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Parameters/IParameter.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Parameters/IParameter.cs @@ -1,19 +1,29 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Activation; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Parameters { + using System; + + using Telerik.JustMock.AutoMock.Ninject.Activation; using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Parameters/IPropertyValue.cs b/Telerik.JustMock/AutoMock/Ninject/Parameters/IPropertyValue.cs index d7436908..3f698ec9 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Parameters/IPropertyValue.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Parameters/IPropertyValue.cs @@ -1,10 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2009-2013 Ninject Project Contributors -// Authors: Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -17,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Parameters { @@ -25,6 +25,6 @@ namespace Telerik.JustMock.AutoMock.Ninject.Parameters /// Overrides the injected value of a property. /// public interface IPropertyValue : IParameter - { + { } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Parameters/Parameter.cs b/Telerik.JustMock/AutoMock/Ninject/Parameters/Parameter.cs index 942919b0..f942a5c1 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Parameters/Parameter.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Parameters/Parameter.cs @@ -1,20 +1,30 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Activation; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Parameters { + using System; + + using Telerik.JustMock.AutoMock.Ninject.Activation; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; /// @@ -22,28 +32,16 @@ namespace Telerik.JustMock.AutoMock.Ninject.Parameters /// public class Parameter : IParameter { - /// - /// Gets the name of the parameter. - /// - public string Name { get; private set; } - - /// - /// Gets a value indicating whether the parameter should be inherited into child requests. - /// - public bool ShouldInherit { get; private set; } - - /// - /// Gets or sets the callback that will be triggered to get the parameter's value. - /// - public Func ValueCallback { get; internal set; } - /// /// Initializes a new instance of the class. /// /// The name of the parameter. /// The value of the parameter. /// Whether the parameter should be inherited into child requests. - public Parameter(string name, object value, bool shouldInherit) : this(name, (ctx, target) => value, shouldInherit) { } + public Parameter(string name, object value, bool shouldInherit) + : this(name, (ctx, target) => value, shouldInherit) + { + } /// /// Initializes a new instance of the class. @@ -56,9 +54,9 @@ public Parameter(string name, Func valueCallback, bool shouldI Ensure.ArgumentNotNullOrEmpty(name, "name"); Ensure.ArgumentNotNull(valueCallback, "valueCallback"); - Name = name; - ValueCallback = (ctx, target) => valueCallback(ctx); - ShouldInherit = shouldInherit; + this.Name = name; + this.ValueCallback = (ctx, target) => valueCallback(ctx); + this.ShouldInherit = shouldInherit; } /// @@ -72,11 +70,26 @@ public Parameter(string name, Func valueCallback, boo Ensure.ArgumentNotNullOrEmpty(name, "name"); Ensure.ArgumentNotNull(valueCallback, "valueCallback"); - Name = name; - ValueCallback = valueCallback; - ShouldInherit = shouldInherit; + this.Name = name; + this.ValueCallback = valueCallback; + this.ShouldInherit = shouldInherit; } - + + /// + /// Gets the name of the parameter. + /// + public string Name { get; private set; } + + /// + /// Gets a value indicating whether the parameter should be inherited into child requests. + /// + public bool ShouldInherit { get; private set; } + + /// + /// Gets the callback that will be triggered to get the parameter's value. + /// + public Func ValueCallback { get; internal set; } + /// /// Gets the value for the parameter within the specified context. /// @@ -86,7 +99,8 @@ public Parameter(string name, Func valueCallback, boo public object GetValue(IContext context, ITarget target) { Ensure.ArgumentNotNull(context, "context"); - return ValueCallback(context, target); + + return this.ValueCallback(context, target); } /// @@ -97,7 +111,7 @@ public object GetValue(IContext context, ITarget target) public override bool Equals(object obj) { var parameter = obj as IParameter; - return parameter != null ? Equals(parameter) : base.Equals(obj); + return parameter != null ? this.Equals(parameter) : base.Equals(obj); } /// @@ -106,7 +120,7 @@ public override bool Equals(object obj) /// A hash code for the object. public override int GetHashCode() { - return GetType().GetHashCode() ^ Name.GetHashCode(); + return this.GetType().GetHashCode() ^ this.Name.GetHashCode(); } /// @@ -116,7 +130,7 @@ public override int GetHashCode() /// True if the objects are equal; otherwise false public bool Equals(IParameter other) { - return other.GetType() == GetType() && other.Name.Equals(Name); + return other.GetType() == this.GetType() && other.Name.Equals(this.Name); } } -} \ No newline at end of file +} diff --git a/Telerik.JustMock/AutoMock/Ninject/Parameters/PropertyValue.cs b/Telerik.JustMock/AutoMock/Ninject/Parameters/PropertyValue.cs index b0905048..2ac164d5 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Parameters/PropertyValue.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Parameters/PropertyValue.cs @@ -1,19 +1,29 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Activation; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Parameters { + using System; + + using Telerik.JustMock.AutoMock.Ninject.Activation; using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; /// @@ -26,20 +36,29 @@ public class PropertyValue : Parameter, IPropertyValue /// /// The name of the property to override. /// The value to inject into the property. - public PropertyValue(string name, object value) : base(name, value, false) { } + public PropertyValue(string name, object value) + : base(name, value, false) + { + } /// /// Initializes a new instance of the class. /// /// The name of the property to override. /// The callback to invoke to get the value that should be injected. - public PropertyValue(string name, Func valueCallback) : base(name, valueCallback, false) { } + public PropertyValue(string name, Func valueCallback) + : base(name, valueCallback, false) + { + } /// /// Initializes a new instance of the class. /// /// The name of the property to override. /// The callback to invoke to get the value that should be injected. - public PropertyValue(string name, Func valueCallback) : base(name, valueCallback, false) { } + public PropertyValue(string name, Func valueCallback) + : base(name, valueCallback, false) + { + } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Parameters/TypeMatchingConstructorArgument.cs b/Telerik.JustMock/AutoMock/Ninject/Parameters/TypeMatchingConstructorArgument.cs new file mode 100644 index 00000000..81d1d13a --- /dev/null +++ b/Telerik.JustMock/AutoMock/Ninject/Parameters/TypeMatchingConstructorArgument.cs @@ -0,0 +1,144 @@ +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- + +namespace Telerik.JustMock.AutoMock.Ninject.Parameters +{ + using System; + + using Telerik.JustMock.AutoMock.Ninject.Activation; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; + + /// + /// Overrides the injected value of a constructor argument. + /// + public class TypeMatchingConstructorArgument : IConstructorArgument + { + private readonly Type type; + + /// + /// Initializes a new instance of the class. + /// + /// The type of the argument to override. + /// The callback that will be triggered to get the parameter's value. + public TypeMatchingConstructorArgument(Type type, Func valueCallback) + : this(type, valueCallback, false) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The type of the argument to override. + /// The callback that will be triggered to get the parameter's value. + /// Whether the parameter should be inherited into child requests. + public TypeMatchingConstructorArgument(Type type, Func valueCallback, bool shouldInherit) + { + Ensure.ArgumentNotNull(type, "type"); + Ensure.ArgumentNotNull(valueCallback, "valueCallback"); + + this.ValueCallback = valueCallback; + this.ShouldInherit = shouldInherit; + this.type = type; + } + + /// + /// Gets the name of the parameter. + /// + public string Name + { + get + { + throw new NotImplementedException(); + } + } + + /// + /// Gets a value indicating whether the parameter should be inherited into child requests. + /// + public bool ShouldInherit { get; private set; } + + /// + /// Gets or sets the callback that will be triggered to get the parameter's value. + /// + private Func ValueCallback { get; set; } + + /// + /// Determines if the parameter applies to the given target. + /// + /// The context. + /// The target. + /// + /// True if the parameter applies in the specified context to the specified target. + /// + /// + /// Only one parameter may return true. + /// + public bool AppliesToTarget(IContext context, ITarget target) + { + return target.Type == this.type; + } + + /// + /// Gets the value for the parameter within the specified context. + /// + /// The context. + /// The target. + /// The value for the parameter. + public object GetValue(IContext context, ITarget target) + { + Ensure.ArgumentNotNull(context, "context"); + + return this.ValueCallback(context, target); + } + + /// + /// Indicates whether the current object is equal to another object of the same type. + /// + /// An object to compare with this object. + /// True if the objects are equal; otherwise false + public bool Equals(IParameter other) + { + var argument = other as TypeMatchingConstructorArgument; + return argument != null && argument.type == this.type; + } + + /// + /// Determines whether the object equals the specified object. + /// + /// An object to compare with this object. + /// True if the objects are equal; otherwise false + public override bool Equals(object obj) + { + var parameter = obj as IParameter; + return parameter != null ? this.Equals(parameter) : ReferenceEquals(this, obj); + } + + /// + /// Serves as a hash function for a particular type. + /// + /// A hash code for the object. + public override int GetHashCode() + { + return this.GetType().GetHashCode() ^ this.type.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Parameters/WeakConstructorArgument.cs b/Telerik.JustMock/AutoMock/Ninject/Parameters/WeakConstructorArgument.cs index 5c3ffac1..862e70f2 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Parameters/WeakConstructorArgument.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Parameters/WeakConstructorArgument.cs @@ -1,10 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2009-2013 Ninject Project Contributors -// Authors: Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -17,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Parameters { @@ -37,7 +37,7 @@ public class WeakConstructorArgument : Parameter, IConstructorArgument private readonly WeakReference weakReference; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The name of the argument to override. /// The value to inject into the property. @@ -47,7 +47,7 @@ public WeakConstructorArgument(string name, object value) } /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The name of the argument to override. /// The value to inject into the property. @@ -65,7 +65,7 @@ public WeakConstructorArgument(string name, object value, bool shouldInherit) /// The context. /// The target. /// - /// Tre if the parameter applies in the specified context to the specified target. + /// True if the parameter applies in the specified context to the specified target. /// /// /// Only one parameter may return true. @@ -75,4 +75,4 @@ public bool AppliesToTarget(IContext context, ITarget target) return string.Equals(this.Name, target.Name); } } -} \ No newline at end of file +} diff --git a/Telerik.JustMock/AutoMock/Ninject/Parameters/WeakPropertyValue.cs b/Telerik.JustMock/AutoMock/Ninject/Parameters/WeakPropertyValue.cs index ca5d2fb2..f404e716 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Parameters/WeakPropertyValue.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Parameters/WeakPropertyValue.cs @@ -1,10 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2009-2013 Ninject Project Contributors -// Authors: Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -17,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Parameters { @@ -40,8 +40,7 @@ public WeakPropertyValue(string name, object value) : base(name, (object)null, false) { this.weakReference = new WeakReference(value); - this.ValueCallback = (ctx, target) => this.weakReference.Target; + this.ValueCallback = (ctx, target) => this.weakReference.Target; } - } -} \ No newline at end of file +} diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Binding.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Binding.cs index e4707d31..b8244963 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Binding.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Binding.cs @@ -1,22 +1,33 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using Telerik.JustMock.AutoMock.Ninject.Activation; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Parameters; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings { + using System; + using System.Collections.Generic; + + using Telerik.JustMock.AutoMock.Ninject.Activation; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Parameters; + /// /// Contains information about a service registration. /// @@ -49,7 +60,7 @@ public Binding(Type service, IBindingConfiguration configuration) } /// - /// Gets or sets the binding configuration. + /// Gets the binding configuration. /// /// The binding configuration. public IBindingConfiguration BindingConfiguration { get; private set; } @@ -62,7 +73,6 @@ public Binding(Type service, IBindingConfiguration configuration) /// /// Gets the binding's metadata. /// - /// public IBindingMetadata Metadata { get @@ -74,7 +84,6 @@ public IBindingMetadata Metadata /// /// Gets or sets the type of target for the binding. /// - /// public BindingTarget Target { get @@ -91,7 +100,6 @@ public BindingTarget Target /// /// Gets or sets a value indicating whether the binding was implicitly registered. /// - /// public bool IsImplicit { get @@ -108,7 +116,6 @@ public bool IsImplicit /// /// Gets a value indicating whether the binding has a condition associated with it. /// - /// public bool IsConditional { get @@ -120,13 +127,13 @@ public bool IsConditional /// /// Gets or sets the condition defined for the binding. /// - /// public Func Condition { get { return this.BindingConfiguration.Condition; } + set { this.BindingConfiguration.Condition = value; @@ -136,7 +143,6 @@ public Func Condition /// /// Gets or sets the callback that returns the provider that should be used by the binding. /// - /// public Func ProviderCallback { get @@ -153,13 +159,13 @@ public Func ProviderCallback /// /// Gets or sets the callback that returns the object that will act as the binding's scope. /// - /// public Func ScopeCallback { get { return this.BindingConfiguration.ScopeCallback; } + set { this.BindingConfiguration.ScopeCallback = value; @@ -169,7 +175,6 @@ public Func ScopeCallback /// /// Gets the parameters defined for the binding. /// - /// public ICollection Parameters { get @@ -181,7 +186,6 @@ public ICollection Parameters /// /// Gets the actions that should be called after instances are activated via the binding. /// - /// public ICollection> ActivationActions { get @@ -193,7 +197,6 @@ public ICollection> ActivationActions /// /// Gets the actions that should be called before instances are deactivated via the binding. /// - /// public ICollection> DeactivationActions { get diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder.cs index 784bdfd8..52567a70 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,14 +17,13 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings { using System; -#if !NETCF using System.Linq.Expressions; -#endif + using Telerik.JustMock.AutoMock.Ninject.Activation; using Telerik.JustMock.AutoMock.Ninject.Activation.Providers; using Telerik.JustMock.AutoMock.Ninject.Infrastructure; @@ -93,14 +90,14 @@ protected IBindingWhenInNamedWithOrOnSyntax InternalTo(Type implementation return new BindingConfigurationBuilder(this.BindingConfiguration, this.ServiceNames, this.Kernel); } - + /// /// Indicates that the service should be bound to the specified constant value. /// /// The type of the implementation. /// The constant value. /// The fluent syntax. - protected IBindingWhenInNamedWithOrOnSyntax InternalToConfiguration(TImplementation value) + protected IBindingWhenInNamedWithOrOnSyntax InternalToConfiguration(TImplementation value) { this.BindingConfiguration.ProviderCallback = ctx => new ConstantProvider(value); this.BindingConfiguration.Target = BindingTarget.Constant; @@ -157,7 +154,7 @@ protected IBindingWhenInNamedWithOrOnSyntax ToProviderInternal< /// Indicates that the service should be bound to an instance of the specified provider type. /// The instance will be activated via the kernel when an instance of the service is activated. /// - /// The type of the returned fleunt syntax + /// The type of the returned fluent syntax /// The type of provider to activate. /// The fluent syntax. protected IBindingWhenInNamedWithOrOnSyntax ToProviderInternal(Type providerType) @@ -168,9 +165,8 @@ protected IBindingWhenInNamedWithOrOnSyntax ToProviderInternal(Type provid return new BindingConfigurationBuilder(this.BindingConfiguration, this.ServiceNames, this.Kernel); } -#if !NETCF /// - /// Indicates that the service should be bound to the speecified constructor. + /// Indicates that the service should be bound to the specified constructor. /// /// The type of the implementation. /// The expression that specifies the constructor. @@ -263,6 +259,5 @@ public T1 Inject() throw new InvalidOperationException("This method is for declaration that a parameter shall be injected only! Never call it directly."); } } -#endif } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder{T1,T2,T3,T4}.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder{T1,T2,T3,T4}.cs index 5855d344..72918baa 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder{T1,T2,T3,T4}.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder{T1,T2,T3,T4}.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,14 +17,13 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings { using System; -#if !NETCF using System.Linq.Expressions; -#endif + using Telerik.JustMock.AutoMock.Ninject.Activation; using Telerik.JustMock.AutoMock.Ninject.Syntax; @@ -39,7 +36,6 @@ namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings /// The fourth service type. public class BindingBuilder : BindingBuilder, IBindingToSyntax { -#pragma warning disable 1584 //mono compiler bug /// /// Initializes a new instance of the class. /// @@ -50,7 +46,6 @@ public BindingBuilder(IBindingConfiguration bindingConfigurationConfiguration, I : base(bindingConfigurationConfiguration, kernel, serviceNames) { } -#pragma warning restore 1584 /// /// Indicates that the service should be bound to the specified implementation type. @@ -73,9 +68,8 @@ public IBindingWhenInNamedWithOrOnSyntax To(Type implementation) return this.InternalTo(implementation); } - #if !NETCF /// - /// Indicates that the service should be bound to the speecified constructor. + /// Indicates that the service should be bound to the specified constructor. /// /// The type of the implementation. /// The expression that specifies the constructor. @@ -86,7 +80,6 @@ public IBindingWhenInNamedWithOrOnSyntax ToConstructor /// Indicates that the service should be bound to an instance of the specified provider type. @@ -107,8 +100,8 @@ public IBindingWhenInNamedWithOrOnSyntax ToProvider() /// The type of provider to activate. /// The type of the implementation. /// The fluent syntax. - public IBindingWhenInNamedWithOrOnSyntax ToProvider() - where TProvider : IProvider + public IBindingWhenInNamedWithOrOnSyntax ToProvider() + where TProvider : IProvider where TImplementation : T1, T2, T3, T4 { return this.ToProviderInternal(); diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder{T1,T2,T3}.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder{T1,T2,T3}.cs index 24a16e39..634337dc 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder{T1,T2,T3}.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder{T1,T2,T3}.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,14 +17,13 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings { using System; -#if !NETCF using System.Linq.Expressions; -#endif + using Telerik.JustMock.AutoMock.Ninject.Activation; using Telerik.JustMock.AutoMock.Ninject.Syntax; @@ -38,7 +35,6 @@ namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings /// The third service type. public class BindingBuilder : BindingBuilder, IBindingToSyntax { -#pragma warning disable 1584 //mono compiler bug /// /// Initializes a new instance of the class. /// @@ -49,7 +45,6 @@ public BindingBuilder(IBindingConfiguration bindingConfigurationConfiguration, I : base(bindingConfigurationConfiguration, kernel, serviceNames) { } -#pragma warning restore 1584 /// /// Indicates that the service should be bound to the specified implementation type. @@ -72,9 +67,8 @@ public IBindingWhenInNamedWithOrOnSyntax To(Type implementation) return this.InternalTo(implementation); } - #if !NETCF /// - /// Indicates that the service should be bound to the speecified constructor. + /// Indicates that the service should be bound to the specified constructor. /// /// The type of the implementation. /// The expression that specifies the constructor. @@ -85,7 +79,6 @@ public IBindingWhenInNamedWithOrOnSyntax ToConstructor /// Indicates that the service should be bound to an instance of the specified provider type. @@ -106,13 +99,13 @@ public IBindingWhenInNamedWithOrOnSyntax ToProvider() /// The type of provider to activate. /// The type of the implementation. /// The fluent syntax. - public IBindingWhenInNamedWithOrOnSyntax ToProvider() - where TProvider : IProvider + public IBindingWhenInNamedWithOrOnSyntax ToProvider() + where TProvider : IProvider where TImplementation : T1, T2, T3 { return this.ToProviderInternal(); } - + /// /// Indicates that the service should be bound to an instance of the specified provider type. /// The instance will be activated via the kernel when an instance of the service is activated. diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder{T1,T2}.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder{T1,T2}.cs index 8cecdf2a..6eb3ad4a 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder{T1,T2}.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder{T1,T2}.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,14 +17,13 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings { using System; -#if !NETCF using System.Linq.Expressions; -#endif + using Telerik.JustMock.AutoMock.Ninject.Activation; using Telerik.JustMock.AutoMock.Ninject.Syntax; @@ -37,7 +34,6 @@ namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings /// The second service type. public class BindingBuilder : BindingBuilder, IBindingToSyntax { -#pragma warning disable 1584 //mono compiler bug /// /// Initializes a new instance of the class. /// @@ -48,7 +44,6 @@ public BindingBuilder(IBindingConfiguration bindingConfigurationConfiguration, I : base(bindingConfigurationConfiguration, kernel, serviceNames) { } -#pragma warning restore 1584 /// /// Indicates that the service should be bound to the specified implementation type. @@ -71,9 +66,8 @@ public IBindingWhenInNamedWithOrOnSyntax To(Type implementation) return this.InternalTo(implementation); } -#if !NETCF /// - /// Indicates that the service should be bound to the speecified constructor. + /// Indicates that the service should be bound to the specified constructor. /// /// The type of the implementation. /// The expression that specifies the constructor. @@ -84,7 +78,6 @@ public IBindingWhenInNamedWithOrOnSyntax ToConstructor /// Indicates that the service should be bound to an instance of the specified provider type. @@ -105,13 +98,13 @@ public IBindingWhenInNamedWithOrOnSyntax ToProvider() /// The type of provider to activate. /// The type of the implementation. /// The fluent syntax. - public IBindingWhenInNamedWithOrOnSyntax ToProvider() - where TProvider : IProvider + public IBindingWhenInNamedWithOrOnSyntax ToProvider() + where TProvider : IProvider where TImplementation : T1, T2 { return this.ToProviderInternal(); } - + /// /// Indicates that the service should be bound to an instance of the specified provider type. /// The instance will be activated via the kernel when an instance of the service is activated. diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder{T1}.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder{T1}.cs index aa289bd1..89fb9bc7 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder{T1}.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingBuilder{T1}.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,14 +17,13 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings { using System; -#if !NETCF using System.Linq.Expressions; -#endif + using Telerik.JustMock.AutoMock.Ninject.Activation; using Telerik.JustMock.AutoMock.Ninject.Activation.Providers; using Telerik.JustMock.AutoMock.Ninject.Infrastructure; @@ -38,7 +35,6 @@ namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings /// The service type. public class BindingBuilder : BindingBuilder, IBindingToSyntax { -#pragma warning disable 1584 //mono compiler bug /// /// Initializes a new instance of the class. /// @@ -50,15 +46,15 @@ public BindingBuilder(IBinding binding, IKernel kernel, string serviceNames) { Ensure.ArgumentNotNull(binding, "binding"); Ensure.ArgumentNotNull(kernel, "kernel"); + this.Binding = binding; } -#pragma warning restore 1584 /// /// Gets the binding being built. /// public IBinding Binding { get; private set; } - + /// /// Indicates that the service should be self-bound. /// @@ -92,9 +88,8 @@ public IBindingWhenInNamedWithOrOnSyntax To(Type implementation) return this.InternalTo(implementation); } -#if !NETCF /// - /// Indicates that the service should be bound to the speecified constructor. + /// Indicates that the service should be bound to the specified constructor. /// /// The type of the implementation. /// The expression that specifies the constructor. @@ -105,7 +100,6 @@ public IBindingWhenInNamedWithOrOnSyntax ToConstructor /// Indicates that the service should be bound to an instance of the specified provider type. diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingConfiguration.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingConfiguration.cs index 422b7de1..d16efb1d 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingConfiguration.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingConfiguration.cs @@ -1,10 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -17,14 +17,16 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings { using System; using System.Collections.Generic; + using Telerik.JustMock.AutoMock.Ninject.Activation; using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Introspection; using Telerik.JustMock.AutoMock.Ninject.Parameters; /// @@ -32,8 +34,6 @@ namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings /// public class BindingConfiguration : IBindingConfiguration { - private IBindingMetadata metadata; - /// /// Initializes a new instance of the class. /// @@ -49,17 +49,7 @@ public BindingConfiguration() /// /// Gets the binding's metadata. /// - public IBindingMetadata Metadata - { - get - { - return this.metadata; - } - private set - { - this.metadata = value; - } - } + public IBindingMetadata Metadata { get; private set; } /// /// Gets or sets a value indicating whether the binding was implicitly registered. @@ -117,6 +107,12 @@ public bool IsConditional public IProvider GetProvider(IContext context) { Ensure.ArgumentNotNull(context, "context"); + + if (this.ProviderCallback == null) + { + throw new ActivationException(ExceptionFormatter.ProviderCallbackIsNull(context)); + } + return this.ProviderCallback(context); } @@ -128,6 +124,7 @@ public IProvider GetProvider(IContext context) public object GetScope(IContext context) { Ensure.ArgumentNotNull(context, "context"); + return this.ScopeCallback(context); } @@ -139,7 +136,8 @@ public object GetScope(IContext context) public bool Matches(IRequest request) { Ensure.ArgumentNotNull(request, "request"); + return this.Condition == null || this.Condition(request); - } + } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingConfigurationBuilder.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingConfigurationBuilder.cs index 8854be13..2ba9ef6c 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingConfigurationBuilder.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingConfigurationBuilder.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings { @@ -46,17 +44,7 @@ public class BindingConfigurationBuilder : IBindingConfigurationSyntax private readonly string serviceNames; /// - /// Gets the binding being built. - /// - public IBindingConfiguration BindingConfiguration { get; private set; } - - /// - /// Gets the kernel. - /// - public IKernel Kernel { get; private set; } - - /// - /// Initializes a new instance of the BindingBuilder<T> class. + /// Initializes a new instance of the class. /// /// The binding configuration to build. /// The names of the configured services. @@ -65,11 +53,22 @@ public BindingConfigurationBuilder(IBindingConfiguration bindingConfiguration, s { Ensure.ArgumentNotNull(bindingConfiguration, "bindingConfiguration"); Ensure.ArgumentNotNull(kernel, "kernel"); + this.BindingConfiguration = bindingConfiguration; this.Kernel = kernel; this.serviceNames = serviceNames; } + /// + /// Gets the binding being built. + /// + public IBindingConfiguration BindingConfiguration { get; private set; } + + /// + /// Gets the kernel. + /// + public IKernel Kernel { get; private set; } + /// /// Indicates that the binding should be used only for requests that support the specified condition. /// @@ -89,7 +88,7 @@ public IBindingInNamedWithOrOnSyntax When(Func condition) /// The fluent syntax. public IBindingInNamedWithOrOnSyntax WhenInjectedInto() { - return WhenInjectedInto(typeof(TParent)); + return this.WhenInjectedInto(typeof(TParent)); } /// @@ -106,13 +105,13 @@ public IBindingInNamedWithOrOnSyntax WhenInjectedInto(Type parent) { this.BindingConfiguration.Condition = r => r.Target != null && - r.Target.Member.ReflectedType.GetInterfaces().Any(i => + r.Target.Member.ReflectedType.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == parent); } else { - this.BindingConfiguration.Condition = r => + this.BindingConfiguration.Condition = r => r.Target != null && r.Target.Member.ReflectedType.GetAllBaseTypes().Any(i => i.IsGenericType && @@ -164,7 +163,10 @@ public IBindingInNamedWithOrOnSyntax WhenInjectedInto(params Type[] parents) matches = r.Target != null && parent.IsAssignableFrom(r.Target.Member.ReflectedType); } - if (matches) return true; + if (matches) + { + return true; + } } return false; @@ -176,19 +178,19 @@ public IBindingInNamedWithOrOnSyntax WhenInjectedInto(params Type[] parents) /// /// Indicates that the binding should be used only for injections on the specified type. /// The type must match exactly the specified type. Types that derive from the specified type - /// will not be considered as valid target. + /// will not be considered as valid target. /// /// The type. /// The fluent syntax. public IBindingInNamedWithOrOnSyntax WhenInjectedExactlyInto() { - return WhenInjectedExactlyInto(typeof(TParent)); + return this.WhenInjectedExactlyInto(typeof(TParent)); } /// /// Indicates that the binding should be used only for injections on the specified type. /// The type must match exactly the specified type. Types that derive from the specified type - /// will not be considered as valid target. + /// will not be considered as valid target. /// /// The type. /// The fluent syntax. @@ -205,20 +207,22 @@ public IBindingInNamedWithOrOnSyntax WhenInjectedExactlyInto(Type parent) { this.BindingConfiguration.Condition = r => r.Target != null && r.Target.Member.ReflectedType == parent; } + return this; } /// /// Indicates that the binding should be used only for injections on the specified type. /// The type must match exactly the specified type. Types that derive from the specified type - /// will not be considered as valid target. + /// will not be considered as valid target. /// Should match at least one of the specified targets /// /// The types. /// The fluent syntax. public IBindingInNamedWithOrOnSyntax WhenInjectedExactlyInto(params Type[] parents) { - this.BindingConfiguration.Condition = r => { + this.BindingConfiguration.Condition = r => + { foreach (var parent in parents) { bool matches = false; @@ -234,7 +238,10 @@ public IBindingInNamedWithOrOnSyntax WhenInjectedExactlyInto(params Type[] pa matches = r.Target != null && r.Target.Member.ReflectedType == parent; } - if(matches) return true; + if (matches) + { + return true; + } } return false; @@ -249,9 +256,10 @@ public IBindingInNamedWithOrOnSyntax WhenInjectedExactlyInto(params Type[] pa /// /// The type of attribute. /// The fluent syntax. - public IBindingInNamedWithOrOnSyntax WhenClassHas() where TAttribute : Attribute + public IBindingInNamedWithOrOnSyntax WhenClassHas() + where TAttribute : Attribute { - return WhenClassHas(typeof(TAttribute)); + return this.WhenClassHas(typeof(TAttribute)); } /// @@ -260,9 +268,10 @@ public IBindingInNamedWithOrOnSyntax WhenClassHas() where TAttrib /// /// The type of attribute. /// The fluent syntax. - public IBindingInNamedWithOrOnSyntax WhenMemberHas() where TAttribute : Attribute + public IBindingInNamedWithOrOnSyntax WhenMemberHas() + where TAttribute : Attribute { - return WhenMemberHas(typeof(TAttribute)); + return this.WhenMemberHas(typeof(TAttribute)); } /// @@ -271,9 +280,10 @@ public IBindingInNamedWithOrOnSyntax WhenMemberHas() where TAttri /// /// The type of attribute. /// The fluent syntax. - public IBindingInNamedWithOrOnSyntax WhenTargetHas() where TAttribute : Attribute + public IBindingInNamedWithOrOnSyntax WhenTargetHas() + where TAttribute : Attribute { - return WhenTargetHas(typeof(TAttribute)); + return this.WhenTargetHas(typeof(TAttribute)); } /// @@ -322,7 +332,7 @@ public IBindingInNamedWithOrOnSyntax WhenTargetHas(Type attributeType) { if (!typeof(Attribute).IsAssignableFrom(attributeType)) { - throw new InvalidOperationException(ExceptionFormatter.InvalidAttributeTypeUsedInBindingCondition(this.serviceNames, "WhenTargetHas", attributeType)); + throw new InvalidOperationException(ExceptionFormatter.InvalidAttributeTypeUsedInBindingCondition(this.serviceNames, "WhenTargetHas", attributeType)); } this.BindingConfiguration.Condition = r => r.Target != null && r.Target.HasAttribute(attributeType); @@ -338,7 +348,7 @@ public IBindingInNamedWithOrOnSyntax WhenTargetHas(Type attributeType) /// The fluent syntax. public IBindingInNamedWithOrOnSyntax WhenParentNamed(string name) { - String.Intern(name); + string.Intern(name); this.BindingConfiguration.Condition = r => r.ParentContext != null && string.Equals(r.ParentContext.Binding.Metadata.Name, name, StringComparison.Ordinal); return this; } @@ -488,7 +498,75 @@ public IBindingWithOrOnSyntax WithConstructorArgument(string name, Func + /// Indicates that the specified constructor argument should be overridden with the specified value. + /// + /// Specifies the argument type to override. + /// The value for the argument. + /// The fluent syntax. + public IBindingWithOrOnSyntax WithConstructorArgument(TValue value) + { + return this.WithConstructorArgument(typeof(TValue), (context, target) => value); + } + + /// + /// Indicates that the specified constructor argument should be overridden with the specified value. + /// + /// The type of the argument to override. + /// The value for the argument. + /// The fluent syntax. + public IBindingWithOrOnSyntax WithConstructorArgument(Type type, object value) + { + return this.WithConstructorArgument(type, (context, target) => value); + } + + /// + /// Indicates that the specified constructor argument should be overridden with the specified value. + /// + /// The type of the argument to override. + /// The callback to invoke to get the value for the argument. + /// The fluent syntax. + public IBindingWithOrOnSyntax WithConstructorArgument(Type type, Func callback) + { + return this.WithConstructorArgument(type, (context, target) => callback(context)); + } + + /// + /// Indicates that the specified constructor argument should be overridden with the specified value. + /// + /// The type of the argument type to override. + /// The callback to invoke to get the value for the argument. + /// The fluent syntax. + public IBindingWithOrOnSyntax WithConstructorArgument(Func callback) + { + return this.WithConstructorArgument(typeof(TValue), (context, target) => callback(context)); + } + + /// + /// Indicates that the specified constructor argument should be overridden with the specified value. + /// + /// The type of the argument to override. + /// The callback to invoke to get the value for the argument. + /// The fluent syntax. + public IBindingWithOrOnSyntax WithConstructorArgument(Type type, Func callback) + { + this.BindingConfiguration.Parameters.Add(new TypeMatchingConstructorArgument(type, callback)); + return this; + } + + /// + /// Indicates that the specified constructor argument should be overridden with the specified value. + /// + /// The type of the argument type to override. + /// The callback to invoke to get the value for the argument. + /// The fluent syntax. + public IBindingWithOrOnSyntax WithConstructorArgument(Func callback) + { + this.WithConstructorArgument(typeof(TValue), callback); + return this; + } + /// /// Indicates that the specified property should be injected with the specified value. /// @@ -524,7 +602,7 @@ public IBindingWithOrOnSyntax WithPropertyValue(string name, Func /// Adds a custom parameter to the binding. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingMetadata.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingMetadata.cs index 28c0cd75..56c921be 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingMetadata.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingMetadata.cs @@ -1,27 +1,37 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings { + using System.Collections.Generic; + + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + /// /// Additional information available about a binding, which can be used in constraints /// to select bindings to use in activation. /// public class BindingMetadata : IBindingMetadata { - private readonly Dictionary _values = new Dictionary(); + private readonly Dictionary values = new Dictionary(); /// /// Gets or sets the binding's name. @@ -36,7 +46,8 @@ public class BindingMetadata : IBindingMetadata public bool Has(string key) { Ensure.ArgumentNotNullOrEmpty(key, "key"); - return _values.ContainsKey(key); + + return this.values.ContainsKey(key); } /// @@ -48,19 +59,22 @@ public bool Has(string key) public T Get(string key) { Ensure.ArgumentNotNullOrEmpty(key, "key"); - return Get(key, default(T)); + + return this.Get(key, default(T)); } /// /// Gets the value of metadata defined with the specified key. /// + /// The type of value to expect. /// The metadata key. /// The value to return if the binding has no metadata set with the specified key. /// The metadata value, or the default value if none was set. public T Get(string key, T defaultValue) { Ensure.ArgumentNotNullOrEmpty(key, "key"); - return _values.ContainsKey(key) ? (T)_values[key] : defaultValue; + + return this.values.ContainsKey(key) ? (T)this.values[key] : defaultValue; } /// @@ -71,7 +85,8 @@ public T Get(string key, T defaultValue) public void Set(string key, object value) { Ensure.ArgumentNotNullOrEmpty(key, "key"); - _values[key] = value; + + this.values[key] = value; } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingPrecedenceComparer.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingPrecedenceComparer.cs new file mode 100644 index 00000000..a2aea777 --- /dev/null +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingPrecedenceComparer.cs @@ -0,0 +1,67 @@ +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- + +namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings +{ + using System; + using System.Collections.Generic; + using System.Linq; + + using Telerik.JustMock.AutoMock.Ninject.Components; + + /// + /// Implements the binding precedence comparer interface + /// + public class BindingPrecedenceComparer : NinjectComponent, IBindingPrecedenceComparer + { + /// + /// Compares the two bindings. + /// + /// The first binding. + /// The second binding. + /// Less than zero if x is less than y; Zero is x equals y; Greater than zero if x is greater than y. + public int Compare(IBinding x, IBinding y) + { + if (x == y) + { + return 0; + } + + // Each function represents a level of precedence. + var funcs = new List> + { + b => b != null, // null bindings should never happen, but just in case + b => b.IsConditional, // conditional bindings > unconditional + b => !b.Service.ContainsGenericParameters, // closed generics > open generics + b => !b.IsImplicit, // explicit bindings > implicit + }; + + var q = from func in funcs + let xVal = func(x) + where xVal != func(y) + select xVal ? 1 : -1; + + // returns the value of the first function that represents a difference + // between the bindings, or else returns 0 (equal) + return q.FirstOrDefault(); + } + } +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingTarget.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingTarget.cs index 5352f483..ca28a7fd 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingTarget.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/BindingTarget.cs @@ -1,18 +1,23 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using Telerik.JustMock.AutoMock.Ninject.Activation; -using Telerik.JustMock.AutoMock.Ninject.Parameters; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings { @@ -44,6 +49,6 @@ public enum BindingTarget /// /// Indicates that the binding is from a type to a constant value. /// - Constant + Constant, } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBinding.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBinding.cs index 2a389baf..7c28eb35 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBinding.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBinding.cs @@ -1,21 +1,28 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using Telerik.JustMock.AutoMock.Ninject.Activation; -using Telerik.JustMock.AutoMock.Ninject.Parameters; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings { + using System; + /// /// Contains information about a service registration. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBindingConfiguration.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBindingConfiguration.cs index aec66f4c..fb188c3d 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBindingConfiguration.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBindingConfiguration.cs @@ -1,10 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -17,12 +17,13 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings { using System; using System.Collections.Generic; + using Telerik.JustMock.AutoMock.Ninject.Activation; using Telerik.JustMock.AutoMock.Ninject.Parameters; @@ -101,6 +102,6 @@ public interface IBindingConfiguration /// /// The request. /// True if the request satisfies the condition; otherwise false. - bool Matches(IRequest request); + bool Matches(IRequest request); } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBindingConfigurationSyntax.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBindingConfigurationSyntax.cs new file mode 100644 index 00000000..21fa80fe --- /dev/null +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBindingConfigurationSyntax.cs @@ -0,0 +1,37 @@ +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- + +namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings +{ + using Telerik.JustMock.AutoMock.Ninject.Syntax; + + /// + /// The syntax to define bindings. + /// + /// The type of the service. + public interface IBindingConfigurationSyntax : + IBindingWhenInNamedWithOrOnSyntax, + IBindingInNamedWithOrOnSyntax, + IBindingNamedWithOrOnSyntax, + IBindingWithOrOnSyntax + { + } +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBindingMetadata.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBindingMetadata.cs index 3a0f235d..0cf75d30 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBindingMetadata.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBindingMetadata.cs @@ -1,15 +1,23 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings { @@ -42,6 +50,7 @@ public interface IBindingMetadata /// /// Gets the value of metadata defined with the specified key. /// + /// The type of value to expect. /// The metadata key. /// The value to return if the binding has no metadata set with the specified key. /// The metadata value, or the default value if none was set. diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBindingPrecedenceComparer.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBindingPrecedenceComparer.cs new file mode 100644 index 00000000..bf6987c7 --- /dev/null +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBindingPrecedenceComparer.cs @@ -0,0 +1,34 @@ +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- + +namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings +{ + using System.Collections.Generic; + + using Telerik.JustMock.AutoMock.Ninject.Components; + + /// + /// The binding precedence comparer interface + /// + public interface IBindingPrecedenceComparer : INinjectComponent, IComparer + { + } +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBindingSyntax.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBindingSyntax.cs deleted file mode 100644 index 07294829..00000000 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/IBindingSyntax.cs +++ /dev/null @@ -1,24 +0,0 @@ -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// - -namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings -{ - using Telerik.JustMock.AutoMock.Ninject.Syntax; - - /// - /// The syntax to define bindings. - /// - /// The type of the service. - public interface IBindingConfigurationSyntax : - IBindingWhenInNamedWithOrOnSyntax, - IBindingInNamedWithOrOnSyntax, - IBindingNamedWithOrOnSyntax, - IBindingWithOrOnSyntax - { - } -} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/DefaultValueBindingResolver.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/DefaultValueBindingResolver.cs index edf3bddb..2fad3835 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/DefaultValueBindingResolver.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/DefaultValueBindingResolver.cs @@ -1,29 +1,38 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives - -using System; -using System.Collections.Generic; -using System.Linq; -using Telerik.JustMock.AutoMock.Ninject.Activation; -using Telerik.JustMock.AutoMock.Ninject.Components; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; - -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings.Resolvers { + using System; + using System.Collections.Generic; + using System.Linq; + + using Telerik.JustMock.AutoMock.Ninject.Activation; + using Telerik.JustMock.AutoMock.Ninject.Components; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; - /// - /// + /// + /// Represents a binding resolver that takes the target default value as the resolved object. + /// public class DefaultValueBindingResolver : NinjectComponent, IMissingBindingResolver { /// @@ -42,7 +51,7 @@ public IEnumerable Resolve(Multimap bindings, IRequest { Condition = r => HasDefaultValue(r.Target), ProviderCallback = _ => new DefaultParameterValueProvider(service), - } + }, } : Enumerable.Empty(); } @@ -56,7 +65,7 @@ private class DefaultParameterValueProvider : IProvider { public DefaultParameterValueProvider(Type type) { - Type = type; + this.Type = type; } public Type Type { get; private set; } @@ -64,7 +73,7 @@ public DefaultParameterValueProvider(Type type) public object Create(IContext context) { var target = context.Request.Target; - return (target == null) ? null : target.DefaultValue; + return target?.DefaultValue; } } } diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/IBindingResolver.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/IBindingResolver.cs index 07be3cae..7ce0630e 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/IBindingResolver.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/IBindingResolver.cs @@ -1,23 +1,32 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using Telerik.JustMock.AutoMock.Ninject.Activation; -using Telerik.JustMock.AutoMock.Ninject.Components; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Parameters; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings.Resolvers { + using System; + using System.Collections.Generic; + + using Telerik.JustMock.AutoMock.Ninject.Components; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + /// /// Contains logic about which bindings to use for a given service request. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/IMissingBindingResolver.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/IMissingBindingResolver.cs index b7fa47b0..1727a201 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/IMissingBindingResolver.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/IMissingBindingResolver.cs @@ -1,28 +1,37 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives - -using System; -using System.Collections.Generic; -using Telerik.JustMock.AutoMock.Ninject.Activation; -using Telerik.JustMock.AutoMock.Ninject.Components; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; - -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings.Resolvers { - /// + using System; + using System.Collections.Generic; + + using Telerik.JustMock.AutoMock.Ninject.Activation; + using Telerik.JustMock.AutoMock.Ninject.Components; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + + /// /// Contains logic about which bindings to use for a given service request /// when other attempts have failed. - /// + /// public interface IMissingBindingResolver : INinjectComponent { /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/OpenGenericBindingResolver.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/OpenGenericBindingResolver.cs index 27ffeb45..3d416e46 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/OpenGenericBindingResolver.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/OpenGenericBindingResolver.cs @@ -1,24 +1,34 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using System.Linq; -using Telerik.JustMock.AutoMock.Ninject.Components; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; - -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings.Resolvers { + using System; + using System.Collections.Generic; + using System.Linq; + + using Telerik.JustMock.AutoMock.Ninject.Components; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; + /// /// Resolves bindings for open generic types. /// @@ -33,7 +43,9 @@ public class OpenGenericBindingResolver : NinjectComponent, IBindingResolver public IEnumerable Resolve(Multimap bindings, Type service) { if (!service.IsGenericType || service.IsGenericTypeDefinition || !bindings.ContainsKey(service.GetGenericTypeDefinition())) + { return Enumerable.Empty(); + } return bindings[service.GetGenericTypeDefinition()].ToEnumerable(); } diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/SelfBindingResolver.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/SelfBindingResolver.cs index efb03e34..300280fd 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/SelfBindingResolver.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/SelfBindingResolver.cs @@ -1,28 +1,38 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives - -using System; -using System.Collections.Generic; -using System.Linq; -using Telerik.JustMock.AutoMock.Ninject.Activation; -using Telerik.JustMock.AutoMock.Ninject.Activation.Providers; -using Telerik.JustMock.AutoMock.Ninject.Components; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; - -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings.Resolvers { - /// - /// + using System; + using System.Collections.Generic; + using System.Linq; + + using Telerik.JustMock.AutoMock.Ninject.Activation; + using Telerik.JustMock.AutoMock.Ninject.Activation.Providers; + using Telerik.JustMock.AutoMock.Ninject.Components; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + + /// + /// Represents a binding resolver that use the service in question itself as the target to activate. + /// public class SelfBindingResolver : NinjectComponent, IMissingBindingResolver { /// @@ -34,16 +44,17 @@ public class SelfBindingResolver : NinjectComponent, IMissingBindingResolver public IEnumerable Resolve(Multimap bindings, IRequest request) { var service = request.Service; - if (!TypeIsSelfBindable(service)) + if (!this.TypeIsSelfBindable(service)) { return Enumerable.Empty(); } + return new[] { new Binding(service) { - ProviderCallback = StandardProvider.GetCreationCallback(service) - } + ProviderCallback = StandardProvider.GetCreationCallback(service), + }, }; } diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/StandardBindingResolver.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/StandardBindingResolver.cs index d8a933d3..01b5aa14 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/StandardBindingResolver.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Bindings/Resolvers/StandardBindingResolver.cs @@ -1,23 +1,33 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using Telerik.JustMock.AutoMock.Ninject.Components; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; - -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Bindings.Resolvers { + using System; + using System.Collections.Generic; + + using Telerik.JustMock.AutoMock.Ninject.Components; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; + /// /// Resolves bindings that have been registered directly for the service. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/ConstructorInjectionDirective.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/ConstructorInjectionDirective.cs index 1e069ffe..e2472404 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/ConstructorInjectionDirective.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/ConstructorInjectionDirective.cs @@ -1,31 +1,35 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Injection; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Directives { + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Injection; + /// /// Describes the injection of a constructor. /// public class ConstructorInjectionDirective : MethodInjectionDirectiveBase { - /// - /// The base .ctor definition. - /// - public ConstructorInfo Constructor { get; set; } - /// /// Initializes a new instance of the class. /// @@ -34,7 +38,24 @@ public class ConstructorInjectionDirective : MethodInjectionDirectiveBase + /// Gets or sets the base .ctor definition. + /// + public ConstructorInfo Constructor { get; set; } + + /// + /// Gets or sets a value indicating whether this constructor has an inject attribute. + /// + /// true if this constructor has an inject attribute; otherwise, false. + public bool HasInjectAttribute { get; set; } + + /// + /// Gets or sets a value indicating whether this contructor has an obsolete attribute. + /// + /// true if this constructor has an obsolete attribute; otherwise, false. + public bool HasObsoleteAttribute { get; set; } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/IDirective.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/IDirective.cs index ea2e127d..ae5a6e7b 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/IDirective.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/IDirective.cs @@ -1,20 +1,30 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Directives { /// /// A piece of information used in an . (Just a marker.) /// - public interface IDirective { } + public interface IDirective + { + } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/MethodInjectionDirective.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/MethodInjectionDirective.cs index 144081fd..ffbe2351 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/MethodInjectionDirective.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/MethodInjectionDirective.cs @@ -1,21 +1,30 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Injection; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Directives { + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Injection; + /// /// Describes the injection of a method. /// @@ -27,6 +36,8 @@ public class MethodInjectionDirective : MethodInjectionDirectiveBaseThe method described by the directive. /// The injector that will be triggered. public MethodInjectionDirective(MethodInfo method, MethodInjector injector) - : base(method, injector) { } + : base(method, injector) + { + } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/MethodInjectionDirectiveBase.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/MethodInjectionDirectiveBase.cs index b706f286..4b0899dc 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/MethodInjectionDirectiveBase.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/MethodInjectionDirectiveBase.cs @@ -1,40 +1,42 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Linq; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Directives { + using System.Linq; + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; + /// /// Describes the injection of a method or constructor. /// + /// The method info. + /// The injector. public abstract class MethodInjectionDirectiveBase : IDirective where TMethod : MethodBase { /// - /// Gets or sets the injector that will be triggered. - /// - public TInjector Injector { get; private set; } - - /// - /// Gets or sets the targets for the directive. - /// - public ITarget[] Targets { get; private set; } - - /// - /// Initializes a new instance of the MethodInjectionDirectiveBase<TMethod, TInjector> class. + /// Initializes a new instance of the class. /// /// The method this directive represents. /// The injector that will be triggered. @@ -43,10 +45,20 @@ protected MethodInjectionDirectiveBase(TMethod method, TInjector injector) Ensure.ArgumentNotNull(method, "method"); Ensure.ArgumentNotNull(injector, "injector"); - Injector = injector; - Targets = CreateTargetsFromParameters(method); + this.Injector = injector; + this.Targets = this.CreateTargetsFromParameters(method); } + /// + /// Gets the injector that will be triggered. + /// + public TInjector Injector { get; private set; } + + /// + /// Gets the targets for the directive. + /// + public ITarget[] Targets { get; private set; } + /// /// Creates targets for the parameters of the method. /// @@ -57,4 +69,4 @@ protected virtual ITarget[] CreateTargetsFromParameters(TMethod method) return method.GetParameters().Select(parameter => new ParameterTarget(method, parameter)).ToArray(); } } -} +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/PropertyInjectionDirective.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/PropertyInjectionDirective.cs index 442406de..3eb52252 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/PropertyInjectionDirective.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Directives/PropertyInjectionDirective.cs @@ -1,36 +1,36 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Injection; -using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Directives { + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Injection; + using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; + /// /// Describes the injection of a property. /// public class PropertyInjectionDirective : IDirective { - /// - /// Gets or sets the injector that will be triggered. - /// - public PropertyInjector Injector { get; private set; } - - /// - /// Gets or sets the injection target for the directive. - /// - public ITarget Target { get; private set; } - /// /// Initializes a new instance of the class. /// @@ -38,10 +38,20 @@ public class PropertyInjectionDirective : IDirective /// The injector that will be triggered. public PropertyInjectionDirective(PropertyInfo member, PropertyInjector injector) { - Injector = injector; - Target = CreateTarget(member); + this.Injector = injector; + this.Target = this.CreateTarget(member); } + /// + /// Gets the injector that will be triggered. + /// + public PropertyInjector Injector { get; private set; } + + /// + /// Gets the injection target for the directive. + /// + public ITarget Target { get; private set; } + /// /// Creates a target for the property. /// @@ -52,4 +62,4 @@ protected virtual ITarget CreateTarget(PropertyInfo propertyInfo) return new PropertyTarget(propertyInfo); } } -} +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/IPlan.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/IPlan.cs index a7e103de..327afe80 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/IPlan.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/IPlan.cs @@ -1,20 +1,31 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning { + using System; + using System.Collections.Generic; + + using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; + /// /// Describes the means by which a type should be activated. /// @@ -25,6 +36,12 @@ public interface IPlan /// Type Type { get; } + /// + /// Gets the constructor injection directives. + /// + /// The constructor injection directives. + IList ConstructorInjectionDirectives { get; } + /// /// Adds the specified directive to the plan. /// @@ -36,20 +53,23 @@ public interface IPlan /// /// The type of directive. /// True if the plan has one or more directives of the type; otherwise, false. - bool Has() where TDirective : IDirective; + bool Has() + where TDirective : IDirective; /// /// Gets the first directive of the specified type from the plan. /// /// The type of directive. /// The first directive, or if no matching directives exist. - TDirective GetOne() where TDirective : IDirective; + TDirective GetOne() + where TDirective : IDirective; /// /// Gets all directives of the specified type that exist in the plan. /// /// The type of directive. /// A series of directives of the specified type. - IEnumerable GetAll() where TDirective : IDirective; + IEnumerable GetAll() + where TDirective : IDirective; } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/IPlanner.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/IPlanner.cs index 7c87b549..5b26a4b8 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/IPlanner.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/IPlanner.cs @@ -1,21 +1,32 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using Telerik.JustMock.AutoMock.Ninject.Components; -using Telerik.JustMock.AutoMock.Ninject.Planning.Strategies; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning { + using System; + using System.Collections.Generic; + + using Telerik.JustMock.AutoMock.Ninject.Components; + using Telerik.JustMock.AutoMock.Ninject.Planning.Strategies; + /// /// Generates plans for how to activate instances. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Plan.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Plan.cs index ae2dfd20..677d5145 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Plan.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Plan.cs @@ -1,27 +1,51 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using System.Linq; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning { + using System; + using System.Collections.Generic; + using System.Linq; + + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; + /// /// Describes the means by which a type should be activated. /// public class Plan : IPlan { + /// + /// Initializes a new instance of the class. + /// + /// The type the plan describes. + public Plan(Type type) + { + Ensure.ArgumentNotNull(type, "type"); + + this.Type = type; + this.Directives = new List(); + this.ConstructorInjectionDirectives = new List(); + } + /// /// Gets the type that the plan describes. /// @@ -33,16 +57,9 @@ public class Plan : IPlan public ICollection Directives { get; private set; } /// - /// Initializes a new instance of the class. + /// Gets the constructor injection directives defined in the plan. /// - /// The type the plan describes. - public Plan(Type type) - { - Ensure.ArgumentNotNull(type, "type"); - - Type = type; - Directives = new List(); - } + public IList ConstructorInjectionDirectives { get; private set; } /// /// Adds the specified directive to the plan. @@ -51,7 +68,13 @@ public Plan(Type type) public void Add(IDirective directive) { Ensure.ArgumentNotNull(directive, "directive"); - Directives.Add(directive); + + if (directive is ConstructorInjectionDirective constructorInjectionDirective) + { + this.ConstructorInjectionDirectives.Add(constructorInjectionDirective); + } + + this.Directives.Add(directive); } /// @@ -62,7 +85,7 @@ public void Add(IDirective directive) public bool Has() where TDirective : IDirective { - return GetAll().Count() > 0; + return this.GetAll().Any(); } /// @@ -73,7 +96,7 @@ public bool Has() public TDirective GetOne() where TDirective : IDirective { - return GetAll().SingleOrDefault(); + return this.GetAll().SingleOrDefault(); } /// @@ -84,7 +107,7 @@ public TDirective GetOne() public IEnumerable GetAll() where TDirective : IDirective { - return Directives.OfType(); + return this.Directives.OfType(); } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Planner.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Planner.cs index 406c7db9..e7cf6018 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Planner.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Planner.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning { @@ -27,6 +25,7 @@ namespace Telerik.JustMock.AutoMock.Ninject.Planning using System.Collections.Generic; using System.Linq; using System.Threading; + using Telerik.JustMock.AutoMock.Ninject.Components; using Telerik.JustMock.AutoMock.Ninject.Infrastructure; using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; @@ -37,7 +36,7 @@ namespace Telerik.JustMock.AutoMock.Ninject.Planning /// public class Planner : NinjectComponent, IPlanner { - private readonly ReaderWriterLock plannerLock = new ReaderWriterLock(); + private readonly ReaderWriterLockSlim plannerLock = new ReaderWriterLockSlim(); private readonly Dictionary plans = new Dictionary(); /// @@ -47,6 +46,7 @@ public class Planner : NinjectComponent, IPlanner public Planner(IEnumerable strategies) { Ensure.ArgumentNotNull(strategies, "strategies"); + this.Strategies = strategies.ToList(); } @@ -54,7 +54,7 @@ public Planner(IEnumerable strategies) /// Gets the strategies that contribute to the planning process. /// public IList Strategies { get; private set; } - + /// /// Gets or creates an activation plan for the specified type. /// @@ -64,15 +64,15 @@ public IPlan GetPlan(Type type) { Ensure.ArgumentNotNull(type, "type"); - this.plannerLock.AcquireReaderLock(Timeout.Infinite); + this.plannerLock.EnterUpgradeableReadLock(); + try { - IPlan plan; - return this.plans.TryGetValue(type, out plan) ? plan : this.CreateNewPlan(type); + return this.plans.TryGetValue(type, out IPlan plan) ? plan : this.CreateNewPlan(type); } finally { - this.plannerLock.ReleaseReaderLock(); + this.plannerLock.ExitUpgradeableReadLock(); } } @@ -84,6 +84,7 @@ public IPlan GetPlan(Type type) protected virtual IPlan CreateEmptyPlan(Type type) { Ensure.ArgumentNotNull(type, "type"); + return new Plan(type); } @@ -95,11 +96,11 @@ protected virtual IPlan CreateEmptyPlan(Type type) /// The newly created plan. private IPlan CreateNewPlan(Type type) { - var lockCooki = this.plannerLock.UpgradeToWriterLock(Timeout.Infinite); + this.plannerLock.EnterWriteLock(); + try { - IPlan plan; - if (this.plans.TryGetValue(type, out plan)) + if (this.plans.TryGetValue(type, out IPlan plan)) { return plan; } @@ -112,7 +113,7 @@ private IPlan CreateNewPlan(Type type) } finally { - this.plannerLock.DowngradeFromWriterLock(ref lockCooki); + this.plannerLock.ExitWriteLock(); } } } diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Strategies/ConstructorReflectionStrategy.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Strategies/ConstructorReflectionStrategy.cs index ae487e86..77ee90c5 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Strategies/ConstructorReflectionStrategy.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Strategies/ConstructorReflectionStrategy.cs @@ -1,40 +1,41 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Components; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Injection; -using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; -using Telerik.JustMock.AutoMock.Ninject.Selection; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Strategies { + using System; + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Components; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; + using Telerik.JustMock.AutoMock.Ninject.Injection; + using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; + using Telerik.JustMock.AutoMock.Ninject.Selection; + /// /// Adds a directive to plans indicating which constructor should be injected during activation. /// public class ConstructorReflectionStrategy : NinjectComponent, IPlanningStrategy { - /// - /// Gets the selector component. - /// - public ISelector Selector { get; private set; } - - /// - /// Gets the injector factory component. - /// - public IInjectorFactory InjectorFactory { get; set; } - /// /// Initializes a new instance of the class. /// @@ -45,10 +46,20 @@ public ConstructorReflectionStrategy(ISelector selector, IInjectorFactory inject Ensure.ArgumentNotNull(selector, "selector"); Ensure.ArgumentNotNull(injectorFactory, "injectorFactory"); - Selector = selector; - InjectorFactory = injectorFactory; + this.Selector = selector; + this.InjectorFactory = injectorFactory; } + /// + /// Gets the selector component. + /// + public ISelector Selector { get; private set; } + + /// + /// Gets or sets the injector factory component. + /// + public IInjectorFactory InjectorFactory { get; set; } + /// /// Adds a to the plan for the constructor /// that should be injected. @@ -58,13 +69,23 @@ public void Execute(IPlan plan) { Ensure.ArgumentNotNull(plan, "plan"); - IEnumerable constructors = Selector.SelectConstructorsForInjection(plan.Type); - if(constructors == null) + var constructors = this.Selector.SelectConstructorsForInjection(plan.Type); + if (constructors == null) + { return; + } - foreach(ConstructorInfo constructor in constructors) + foreach (ConstructorInfo constructor in constructors) { - plan.Add(new ConstructorInjectionDirective(constructor, InjectorFactory.Create(constructor))); + var hasInjectAttribute = constructor.HasAttribute(this.Settings.InjectAttribute); + var hasObsoleteAttribute = constructor.HasAttribute(typeof(ObsoleteAttribute)); + var directive = new ConstructorInjectionDirective(constructor, this.InjectorFactory.Create(constructor)) + { + HasInjectAttribute = hasInjectAttribute, + HasObsoleteAttribute = hasObsoleteAttribute, + }; + + plan.Add(directive); } } } diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Strategies/IPlanningStrategy.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Strategies/IPlanningStrategy.cs index 797614c3..68ba81c3 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Strategies/IPlanningStrategy.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Strategies/IPlanningStrategy.cs @@ -1,19 +1,28 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using Telerik.JustMock.AutoMock.Ninject.Components; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Strategies { + using Telerik.JustMock.AutoMock.Ninject.Components; + /// /// Contributes to the generation of a . /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Strategies/MethodReflectionStrategy.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Strategies/MethodReflectionStrategy.cs index de31cc9a..966f247a 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Strategies/MethodReflectionStrategy.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Strategies/MethodReflectionStrategy.cs @@ -1,39 +1,39 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Components; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Injection; -using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; -using Telerik.JustMock.AutoMock.Ninject.Selection; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Strategies { + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Components; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Injection; + using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; + using Telerik.JustMock.AutoMock.Ninject.Selection; + /// /// Adds directives to plans indicating which methods should be injected during activation. /// public class MethodReflectionStrategy : NinjectComponent, IPlanningStrategy { - /// - /// Gets the selector component. - /// - public ISelector Selector { get; private set; } - - /// - /// Gets the injector factory component. - /// - public IInjectorFactory InjectorFactory { get; set; } - /// /// Initializes a new instance of the class. /// @@ -44,10 +44,20 @@ public MethodReflectionStrategy(ISelector selector, IInjectorFactory injectorFac Ensure.ArgumentNotNull(selector, "selector"); Ensure.ArgumentNotNull(injectorFactory, "injectorFactory"); - Selector = selector; - InjectorFactory = injectorFactory; + this.Selector = selector; + this.InjectorFactory = injectorFactory; } + /// + /// Gets the selector component. + /// + public ISelector Selector { get; private set; } + + /// + /// Gets or sets the injector factory component. + /// + public IInjectorFactory InjectorFactory { get; set; } + /// /// Adds a to the plan for each method /// that should be injected. @@ -57,8 +67,10 @@ public void Execute(IPlan plan) { Ensure.ArgumentNotNull(plan, "plan"); - foreach (MethodInfo method in Selector.SelectMethodsForInjection(plan.Type)) - plan.Add(new MethodInjectionDirective(method, InjectorFactory.Create(method))); + foreach (MethodInfo method in this.Selector.SelectMethodsForInjection(plan.Type)) + { + plan.Add(new MethodInjectionDirective(method, this.InjectorFactory.Create(method))); + } } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Strategies/PropertyReflectionStrategy.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Strategies/PropertyReflectionStrategy.cs index 31ac72a4..fa1605e9 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Strategies/PropertyReflectionStrategy.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Strategies/PropertyReflectionStrategy.cs @@ -1,39 +1,39 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Components; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Injection; -using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; -using Telerik.JustMock.AutoMock.Ninject.Selection; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Strategies { + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Components; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Injection; + using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; + using Telerik.JustMock.AutoMock.Ninject.Selection; + /// /// Adds directives to plans indicating which properties should be injected during activation. /// public class PropertyReflectionStrategy : NinjectComponent, IPlanningStrategy { - /// - /// Gets the selector component. - /// - public ISelector Selector { get; private set; } - - /// - /// Gets the injector factory component. - /// - public IInjectorFactory InjectorFactory { get; set; } - /// /// Initializes a new instance of the class. /// @@ -44,10 +44,20 @@ public PropertyReflectionStrategy(ISelector selector, IInjectorFactory injectorF Ensure.ArgumentNotNull(selector, "selector"); Ensure.ArgumentNotNull(injectorFactory, "injectorFactory"); - Selector = selector; - InjectorFactory = injectorFactory; + this.Selector = selector; + this.InjectorFactory = injectorFactory; } + /// + /// Gets the selector component. + /// + public ISelector Selector { get; private set; } + + /// + /// Gets or sets the injector factory component. + /// + public IInjectorFactory InjectorFactory { get; set; } + /// /// Adds a to the plan for each property /// that should be injected. @@ -57,8 +67,10 @@ public void Execute(IPlan plan) { Ensure.ArgumentNotNull(plan, "plan"); - foreach (PropertyInfo property in Selector.SelectPropertiesForInjection(plan.Type)) - plan.Add(new PropertyInjectionDirective(property, InjectorFactory.Create(property))); + foreach (PropertyInfo property in this.Selector.SelectPropertiesForInjection(plan.Type)) + { + plan.Add(new PropertyInjectionDirective(property, this.InjectorFactory.Create(property))); + } } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Targets/ITarget.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Targets/ITarget.cs index 3d8ba6c0..b6113e3b 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Targets/ITarget.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Targets/ITarget.cs @@ -1,21 +1,32 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Activation; -using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Targets { + using System; + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Activation; + using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; + /// /// Represents a site on a type where a value will be injected. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Targets/ParameterTarget.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Targets/ParameterTarget.cs index 806e2650..c560a5c2 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Targets/ParameterTarget.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Targets/ParameterTarget.cs @@ -1,33 +1,50 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Targets { + using System; + using System.Reflection; + /// /// Represents an injection target for a . /// public class ParameterTarget : Target { - private readonly Future defaultValue; + /// + /// Initializes a new instance of the class. + /// + /// The method that defines the parameter. + /// The parameter that this target represents. + public ParameterTarget(MethodBase method, ParameterInfo site) + : base(method, site) + { + } /// /// Gets the name of the target. /// public override string Name { - get { return Site.Name; } + get { return this.Site.Name; } } /// @@ -35,37 +52,24 @@ public override string Name /// public override Type Type { - get { return Site.ParameterType; } + get { return this.Site.ParameterType; } } -// Windows Phone doesn't support default values and returns null instead of DBNull. -#if !WINDOWS_PHONE /// /// Gets a value indicating whether the target has a default value. /// public override bool HasDefaultValue { - get { return defaultValue.Value != DBNull.Value; } + get { return this.Site.HasDefaultValue; } } /// /// Gets the default value for the target. /// - /// If the item does not have a default value. + /// If the item does not have a default value. public override object DefaultValue { - get { return HasDefaultValue ? defaultValue.Value : base.DefaultValue; } - } -#endif - - /// - /// Initializes a new instance of the class. - /// - /// The method that defines the parameter. - /// The parameter that this target represents. - public ParameterTarget(MethodBase method, ParameterInfo site) : base(method, site) - { - defaultValue = new Future(() => site.DefaultValue); + get { return this.HasDefaultValue ? this.Site.DefaultValue : base.DefaultValue; } } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Targets/PropertyTarget.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Targets/PropertyTarget.cs index 34a34fa8..4ac0ab3c 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Targets/PropertyTarget.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Targets/PropertyTarget.cs @@ -1,30 +1,49 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Reflection; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Targets { + using System; + using System.Reflection; + /// /// Represents an injection target for a . /// public class PropertyTarget : Target { + /// + /// Initializes a new instance of the class. + /// + /// The property that this target represents. + public PropertyTarget(PropertyInfo site) + : base(site, site) + { + } + /// /// Gets the name of the target. /// public override string Name { - get { return Site.Name; } + get { return this.Site.Name; } } /// @@ -32,13 +51,7 @@ public override string Name /// public override Type Type { - get { return Site.PropertyType; } + get { return this.Site.PropertyType; } } - - /// - /// Initializes a new instance of the class. - /// - /// The property that this target represents. - public PropertyTarget(PropertyInfo site) : base(site, site) { } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Planning/Targets/Target.cs b/Telerik.JustMock/AutoMock/Ninject/Planning/Targets/Target.cs index f6584c2d..107e950a 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Planning/Targets/Target.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Planning/Targets/Target.cs @@ -1,26 +1,37 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Activation; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Introspection; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; -using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Planning.Targets { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Activation; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Introspection; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; + using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; + /// /// Represents a site on a type where a value can be injected. /// @@ -28,8 +39,25 @@ namespace Telerik.JustMock.AutoMock.Ninject.Planning.Targets public abstract class Target : ITarget where T : ICustomAttributeProvider { - private readonly Future> _constraint; - private readonly Future _isOptional; + private readonly Lazy> constraint; + private readonly Lazy isOptional; + + /// + /// Initializes a new instance of the class. + /// + /// The member that contains the target. + /// The site represented by the target. + protected Target(MemberInfo member, T site) + { + Ensure.ArgumentNotNull(member, "member"); + Ensure.ArgumentNotNull(site, "site"); + + this.Member = member; + this.Site = site; + + this.constraint = new Lazy>(this.ReadConstraintFromTarget); + this.isOptional = new Lazy(this.ReadOptionalFromTarget); + } /// /// Gets the member that contains the target. @@ -37,7 +65,7 @@ public abstract class Target : ITarget public MemberInfo Member { get; private set; } /// - /// Gets or sets the site (property, parameter, etc.) represented by the target. + /// Gets the site (property, parameter, etc.) represented by the target. /// public T Site { get; private set; } @@ -56,7 +84,7 @@ public abstract class Target : ITarget /// public Func Constraint { - get { return _constraint; } + get { return this.constraint.Value; } } /// @@ -64,7 +92,7 @@ public Func Constraint /// public bool IsOptional { - get { return _isOptional; } + get { return this.isOptional.Value; } } /// @@ -84,23 +112,6 @@ public virtual object DefaultValue get { throw new InvalidOperationException(ExceptionFormatter.TargetDoesNotHaveADefaultValue(this)); } } - /// - /// Initializes a new instance of the Target<T> class. - /// - /// The member that contains the target. - /// The site represented by the target. - protected Target(MemberInfo member, T site) - { - Ensure.ArgumentNotNull(member, "member"); - Ensure.ArgumentNotNull(site, "site"); - - Member = member; - Site = site; - - _constraint = new Future>(ReadConstraintFromTarget); - _isOptional = new Future(ReadOptionalFromTarget); - } - /// /// Returns an array of custom attributes of a specified type defined on the target. /// @@ -110,7 +121,8 @@ protected Target(MemberInfo member, T site) public object[] GetCustomAttributes(Type attributeType, bool inherit) { Ensure.ArgumentNotNull(attributeType, "attributeType"); - return Site.GetCustomAttributesExtended(attributeType, inherit); + + return this.Site.GetCustomAttributesExtended(attributeType, inherit); } /// @@ -120,7 +132,7 @@ public object[] GetCustomAttributes(Type attributeType, bool inherit) /// An array of custom attributes. public object[] GetCustomAttributes(bool inherit) { - return Site.GetCustomAttributes(inherit); + return this.Site.GetCustomAttributes(inherit); } /// @@ -132,7 +144,8 @@ public object[] GetCustomAttributes(bool inherit) public bool IsDefined(Type attributeType, bool inherit) { Ensure.ArgumentNotNull(attributeType, "attributeType"); - return Site.IsDefined(attributeType, inherit); + + return this.Site.IsDefined(attributeType, inherit); } /// @@ -144,25 +157,9 @@ public object ResolveWithin(IContext parent) { Ensure.ArgumentNotNull(parent, "parent"); - if (Type.IsArray) - { - Type service = Type.GetElementType(); - return GetValues(service, parent).CastSlow(service).ToArraySlow(service); - } - - if (Type.IsGenericType) - { - Type gtd = Type.GetGenericTypeDefinition(); - Type service = Type.GetGenericArguments()[0]; - - if (gtd == typeof(List<>) || gtd == typeof(IList<>) || gtd == typeof(ICollection<>)) - return GetValues(service, parent).CastSlow(service).ToListSlow(service); - - if (gtd == typeof(IEnumerable<>)) - return GetValues(service, parent).CastSlow(service); - } - - return GetValue(Type, parent); + var request = parent.Request.CreateChild(this.Type, parent, this); + request.IsUnique = true; + return parent.Kernel.Resolve(request).SingleOrDefault(); } /// @@ -171,6 +168,7 @@ public object ResolveWithin(IContext parent) /// The service that the target is requesting. /// The parent context in which the target is being injected. /// A series of values that are available for injection. + [Obsolete] protected virtual IEnumerable GetValues(Type service, IContext parent) { Ensure.ArgumentNotNull(service, "service"); @@ -187,6 +185,7 @@ protected virtual IEnumerable GetValues(Type service, IContext parent) /// The service that the target is requesting. /// The parent context in which the target is being injected. /// The value that is to be injected. + [Obsolete] protected virtual object GetValue(Type service, IContext parent) { Ensure.ArgumentNotNull(service, "service"); @@ -203,7 +202,7 @@ protected virtual object GetValue(Type service, IContext parent) /// if it is optional; otherwise . protected virtual bool ReadOptionalFromTarget() { - return Site.HasAttribute(typeof(OptionalAttribute)); + return this.Site.HasAttribute(typeof(OptionalAttribute)); } /// @@ -215,10 +214,14 @@ protected virtual Func ReadConstraintFromTarget() var attributes = this.GetCustomAttributes(typeof(ConstraintAttribute), true) as ConstraintAttribute[]; if (attributes == null || attributes.Length == 0) + { return null; + } if (attributes.Length == 1) + { return attributes[0].Matches; + } return metadata => attributes.All(attribute => attribute.Matches(metadata)); } diff --git a/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/IConstructorScorer.cs b/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/IConstructorScorer.cs index aaf63c70..511ba632 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/IConstructorScorer.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/IConstructorScorer.cs @@ -1,23 +1,30 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Activation; -using Telerik.JustMock.AutoMock.Ninject.Components; -using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; - -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Selection.Heuristics { + using Telerik.JustMock.AutoMock.Ninject.Activation; + using Telerik.JustMock.AutoMock.Ninject.Components; + using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; + /// /// Generates scores for constructors, to determine which is the best one to call during activation. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/IInjectionHeuristic.cs b/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/IInjectionHeuristic.cs index 92a81d72..6e3c5a42 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/IInjectionHeuristic.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/IInjectionHeuristic.cs @@ -1,20 +1,30 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Components; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Selection.Heuristics { + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Components; + /// /// Determines whether members should be injected during activation. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/SpecificConstructorSelector.cs b/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/SpecificConstructorSelector.cs index 54fbebe7..9406aa68 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/SpecificConstructorSelector.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/SpecificConstructorSelector.cs @@ -1,13 +1,15 @@ -//------------------------------------------------------------------------------- -// -// Copyright (c) 2011 Ninject Project Contributors -// Author: Remo Gloor (remo.gloor@gmail.com) +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. // -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, @@ -15,11 +17,12 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Selection.Heuristics { using System.Reflection; + using Telerik.JustMock.AutoMock.Ninject.Activation; using Telerik.JustMock.AutoMock.Ninject.Components; using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; @@ -48,7 +51,7 @@ public SpecificConstructorSelector(ConstructorInfo constructorInfo) /// The constructor's score. public virtual int Score(IContext context, ConstructorInjectionDirective directive) { - return directive.Constructor.Equals(constructorInfo) ? 1 : 0; + return directive.Constructor.Equals(this.constructorInfo) ? 1 : 0; } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/StandardConstructorScorer.cs b/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/StandardConstructorScorer.cs index 9161a108..5612f829 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/StandardConstructorScorer.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/StandardConstructorScorer.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Selection.Heuristics { @@ -30,7 +28,6 @@ namespace Telerik.JustMock.AutoMock.Ninject.Selection.Heuristics using Telerik.JustMock.AutoMock.Ninject.Activation; using Telerik.JustMock.AutoMock.Ninject.Components; using Telerik.JustMock.AutoMock.Ninject.Infrastructure; - using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; using Telerik.JustMock.AutoMock.Ninject.Parameters; using Telerik.JustMock.AutoMock.Ninject.Planning.Directives; using Telerik.JustMock.AutoMock.Ninject.Planning.Targets; @@ -50,23 +47,28 @@ public class StandardConstructorScorer : NinjectComponent, IConstructorScorer public virtual int Score(IContext context, ConstructorInjectionDirective directive) { Ensure.ArgumentNotNull(context, "context"); - Ensure.ArgumentNotNull(directive, "constructor"); + Ensure.ArgumentNotNull(directive, "directive"); - if (directive.Constructor.HasAttribute(Settings.InjectAttribute)) + if (directive.HasInjectAttribute) { return int.MaxValue; } + if (directive.HasObsoleteAttribute) + { + return int.MinValue; + } + var score = 1; foreach (ITarget target in directive.Targets) { - if (ParameterExists(context, target)) + if (this.ParameterExists(context, target)) { score++; continue; } - - if (BindingExists(context, target)) + + if (this.BindingExists(context, target)) { score++; continue; @@ -78,7 +80,7 @@ public virtual int Score(IContext context, ConstructorInjectionDirective directi score += int.MinValue; } } - + return score; } @@ -90,8 +92,8 @@ public virtual int Score(IContext context, ConstructorInjectionDirective directi /// Whether a binding exists for the target in the given context. protected virtual bool BindingExists(IContext context, ITarget target) { - return this.BindingExists(context.Kernel, context, target); - } + return this.BindingExists(context.Kernel, context, target); + } /// /// Checkes whether a binding exists for a given target on the specified kernel. @@ -102,14 +104,30 @@ protected virtual bool BindingExists(IContext context, ITarget target) /// Whether a binding exists for the target in the given context. protected virtual bool BindingExists(IKernel kernel, IContext context, ITarget target) { - var targetType = GetTargetType(target); - return kernel.GetBindings(targetType).Any(b => !b.IsImplicit) + var targetType = this.GetTargetType(target); + var request = context.Request.CreateChild(targetType, context, target); + + return kernel.GetBindings(targetType).Any(b => !b.IsImplicit && b.Matches(request)) || target.HasDefaultValue; } + /// + /// Checks whether any parameters exist for the given target.. + /// + /// The context. + /// The target. + /// Whether a parameter exists for the target in the given context. + protected virtual bool ParameterExists(IContext context, ITarget target) + { + return context + .Parameters.OfType() + .Any(parameter => parameter.AppliesToTarget(context, target)); + } + private Type GetTargetType(ITarget target) { var targetType = target.Type; + if (targetType.IsArray) { targetType = targetType.GetElementType(); @@ -122,18 +140,5 @@ private Type GetTargetType(ITarget target) return targetType; } - - /// - /// Checks whether any parameters exist for the geiven target.. - /// - /// The context. - /// The target. - /// Whether a parameter exists for the target in the given context. - protected virtual bool ParameterExists(IContext context, ITarget target) - { - return context - .Parameters.OfType() - .Any(parameter => parameter.AppliesToTarget(context, target)); - } } -} +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/StandardInjectionHeuristic.cs b/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/StandardInjectionHeuristic.cs index 0a1c6228..53e6a687 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/StandardInjectionHeuristic.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Selection/Heuristics/StandardInjectionHeuristic.cs @@ -1,22 +1,32 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Components; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Selection.Heuristics { + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Components; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; + /// /// Determines whether members should be injected during activation by checking /// if they are decorated with an injection marker attribute. @@ -32,22 +42,16 @@ public virtual bool ShouldInject(MemberInfo member) { Ensure.ArgumentNotNull(member, "member"); - var propertyInfo = member as PropertyInfo; - - if (propertyInfo != null) + if (member is PropertyInfo propertyInfo) { -#if !SILVERLIGHT - bool injectNonPublic = Settings.InjectNonPublic; -#else - const bool injectNonPublic = false; -#endif // !SILVERLIGHT + var injectNonPublic = this.Settings.InjectNonPublic; var setMethod = propertyInfo.GetSetMethod(injectNonPublic); - return member.HasAttribute(Settings.InjectAttribute) && setMethod != null; + return member.HasAttribute(this.Settings.InjectAttribute) && setMethod != null; } - return member.HasAttribute(Settings.InjectAttribute); + return member.HasAttribute(this.Settings.InjectAttribute); } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Selection/ISelector.cs b/Telerik.JustMock/AutoMock/Ninject/Selection/ISelector.cs index 7f5d85bd..f9ea787a 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Selection/ISelector.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Selection/ISelector.cs @@ -1,31 +1,42 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Components; -using Telerik.JustMock.AutoMock.Ninject.Selection.Heuristics; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Selection { + using System; + using System.Collections.Generic; + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Components; + using Telerik.JustMock.AutoMock.Ninject.Selection.Heuristics; + /// /// Selects members for injection. /// public interface ISelector : INinjectComponent { /// - /// Gets or sets the constructor scorer. + /// Gets the constructor scorer. /// - IConstructorScorer ConstructorScorer { get; set; } + IConstructorScorer ConstructorScorer { get; } /// /// Gets the heuristics used to determine which members should be injected. diff --git a/Telerik.JustMock/AutoMock/Ninject/Selection/Selector.cs b/Telerik.JustMock/AutoMock/Ninject/Selection/Selector.cs index 063be910..66fbb92f 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Selection/Selector.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Selection/Selector.cs @@ -1,25 +1,35 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Components; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Selection.Heuristics; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Selection { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Components; + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language; + using Telerik.JustMock.AutoMock.Ninject.Selection.Heuristics; /// /// Selects members for injection. @@ -29,24 +39,23 @@ public class Selector : NinjectComponent, ISelector private const BindingFlags DefaultFlags = BindingFlags.Public | BindingFlags.Instance; /// - /// Gets the default binding flags. + /// Initializes a new instance of the class. /// - protected virtual BindingFlags Flags + /// The constructor scorer. + /// The injection heuristics. + public Selector(IConstructorScorer constructorScorer, IEnumerable injectionHeuristics) { - get - { - #if !NO_LCG && !SILVERLIGHT - return Settings.InjectNonPublic ? (DefaultFlags | BindingFlags.NonPublic) : DefaultFlags; - #else - return DefaultFlags; - #endif - } + Ensure.ArgumentNotNull(constructorScorer, "constructorScorer"); + Ensure.ArgumentNotNull(injectionHeuristics, "injectionHeuristics"); + + this.ConstructorScorer = constructorScorer; + this.InjectionHeuristics = injectionHeuristics.ToList(); } /// - /// Gets or sets the constructor scorer. + /// Gets the constructor scorer. /// - public IConstructorScorer ConstructorScorer { get; set; } + public IConstructorScorer ConstructorScorer { get; private set; } /// /// Gets the property injection heuristics. @@ -54,17 +63,18 @@ protected virtual BindingFlags Flags public ICollection InjectionHeuristics { get; private set; } /// - /// Initializes a new instance of the class. + /// Gets the default binding flags. /// - /// The constructor scorer. - /// The injection heuristics. - public Selector(IConstructorScorer constructorScorer, IEnumerable injectionHeuristics) + protected virtual BindingFlags Flags { - Ensure.ArgumentNotNull(constructorScorer, "constructorScorer"); - Ensure.ArgumentNotNull(injectionHeuristics, "injectionHeuristics"); - - ConstructorScorer = constructorScorer; - InjectionHeuristics = injectionHeuristics.ToList(); + get + { +#if !NO_LCG + return this.Settings.InjectNonPublic ? (DefaultFlags | BindingFlags.NonPublic) : DefaultFlags; +#else + return DefaultFlags; +#endif + } } /// @@ -72,11 +82,16 @@ public Selector(IConstructorScorer constructorScorer, IEnumerable /// The type. /// The selected constructor, or if none were available. - public virtual IEnumerable SelectConstructorsForInjection(Type type) + public virtual IEnumerable SelectConstructorsForInjection(Type type) { Ensure.ArgumentNotNull(type, "type"); - var constructors = type.GetConstructors( Flags ); + if (type.IsSubclassOf(typeof(MulticastDelegate))) + { + return null; + } + + var constructors = type.GetConstructors(this.Flags); return constructors.Length == 0 ? null : constructors; } @@ -88,30 +103,24 @@ public virtual IEnumerable SelectConstructorsForInjection(Type public virtual IEnumerable SelectPropertiesForInjection(Type type) { Ensure.ArgumentNotNull(type, "type"); - List properties = new List(); + + var properties = new List(); properties.AddRange( type.GetProperties(this.Flags) - .Select(p => p.GetPropertyFromDeclaredType(p, this.Flags)) - .Where(p => this.InjectionHeuristics.Any(h => h.ShouldInject(p)))); -#if !SILVERLIGHT + .Select(p => p.GetPropertyFromDeclaredType(p, this.Flags)) + .Where(p => this.InjectionHeuristics.Any(h => p != null && h.ShouldInject(p)))); + if (this.Settings.InjectParentPrivateProperties) { for (Type parentType = type.BaseType; parentType != null; parentType = parentType.BaseType) { - properties.AddRange(this.GetPrivateProperties(type.BaseType)); + properties.AddRange(this.GetPrivateProperties(parentType)); } } -#endif return properties; } - private IEnumerable GetPrivateProperties(Type type) - { - return type.GetProperties(this.Flags).Where(p => p.DeclaringType == type && p.IsPrivate()) - .Where(p => this.InjectionHeuristics.Any(h => h.ShouldInject(p))); - } - /// /// Selects methods that should be injected. /// @@ -120,7 +129,14 @@ private IEnumerable GetPrivateProperties(Type type) public virtual IEnumerable SelectMethodsForInjection(Type type) { Ensure.ArgumentNotNull(type, "type"); - return type.GetMethods(Flags).Where(m => InjectionHeuristics.Any(h => h.ShouldInject(m))); + + return type.GetMethods(this.Flags).Where(m => this.InjectionHeuristics.Any(h => h.ShouldInject(m))); + } + + private IEnumerable GetPrivateProperties(Type type) + { + return type.GetProperties(this.Flags).Where(p => p.DeclaringType == type && p.IsPrivate()) + .Where(p => this.InjectionHeuristics.Any(h => h.ShouldInject(p))); } } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/StandardKernel.cs b/Telerik.JustMock/AutoMock/Ninject/StandardKernel.cs index 214b84f6..59ba111d 100644 --- a/Telerik.JustMock/AutoMock/Ninject/StandardKernel.cs +++ b/Telerik.JustMock/AutoMock/Ninject/StandardKernel.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject { @@ -31,6 +29,7 @@ namespace Telerik.JustMock.AutoMock.Ninject using Telerik.JustMock.AutoMock.Ninject.Injection; using Telerik.JustMock.AutoMock.Ninject.Modules; using Telerik.JustMock.AutoMock.Ninject.Planning; + using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings.Resolvers; using Telerik.JustMock.AutoMock.Ninject.Planning.Strategies; using Telerik.JustMock.AutoMock.Ninject.Selection; @@ -45,7 +44,8 @@ public class StandardKernel : KernelBase /// Initializes a new instance of the class. /// /// The modules to load into the kernel. - public StandardKernel(params INinjectModule[] modules) : base(modules) + public StandardKernel(params INinjectModule[] modules) + : base(modules) { } @@ -54,7 +54,8 @@ public StandardKernel(params INinjectModule[] modules) : base(modules) /// /// The configuration to use. /// The modules to load into the kernel. - public StandardKernel(INinjectSettings settings, params INinjectModule[] modules) : base(settings, modules) + public StandardKernel(INinjectSettings settings, params INinjectModule[] modules) + : base(settings, modules) { } @@ -84,7 +85,7 @@ private void AddComponent() Components.Add(); } } - + /// /// Adds components to the kernel during startup. /// @@ -100,7 +101,7 @@ protected override void AddComponents() AddComponent(); AddComponent(); - if (!Settings.ActivationCacheDisabled) + if (!this.Settings.ActivationCacheDisabled) { AddComponent(); } @@ -112,6 +113,8 @@ protected override void AddComponents() AddComponent(); AddComponent(); + AddComponent(); + AddComponent(); AddComponent(); @@ -119,7 +122,7 @@ protected override void AddComponents() AddComponent(); #if !NO_LCG - if (!Settings.UseReflectionBasedInjection) + if (!this.Settings.UseReflectionBasedInjection) { AddComponent(); } @@ -133,11 +136,9 @@ protected override void AddComponents() AddComponent(); AddComponent(); -#if !NO_ASSEMBLY_SCANNING AddComponent(); AddComponent(); AddComponent(); -#endif } } } diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/BindingRoot.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/BindingRoot.cs index e03ce6e1..524ae87a 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/BindingRoot.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/BindingRoot.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Syntax { @@ -49,7 +47,7 @@ public abstract class BindingRoot : DisposableObject, IBindingRoot /// The fluent syntax public IBindingToSyntax Bind() { - Type service = typeof(T); + var service = typeof(T); var binding = new Binding(service); this.AddBinding(binding); @@ -68,9 +66,9 @@ public IBindingToSyntax Bind() var firstBinding = new Binding(typeof(T1)); this.AddBinding(firstBinding); this.AddBinding(new Binding(typeof(T2), firstBinding.BindingConfiguration)); - var servceNames = new[] { typeof(T1).Format(), typeof(T2).Format() }; + var serviceNames = new[] { typeof(T1).Format(), typeof(T2).Format() }; - return new BindingBuilder(firstBinding.BindingConfiguration, this.KernelInstance, string.Join(", ", servceNames)); + return new BindingBuilder(firstBinding.BindingConfiguration, this.KernelInstance, string.Join(", ", serviceNames)); } /// @@ -86,9 +84,9 @@ public IBindingToSyntax Bind() this.AddBinding(firstBinding); this.AddBinding(new Binding(typeof(T2), firstBinding.BindingConfiguration)); this.AddBinding(new Binding(typeof(T3), firstBinding.BindingConfiguration)); - var servceNames = new[] { typeof(T1).Format(), typeof(T2).Format(), typeof(T3).Format() }; + var serviceNames = new[] { typeof(T1).Format(), typeof(T2).Format(), typeof(T3).Format() }; - return new BindingBuilder(firstBinding.BindingConfiguration, this.KernelInstance, string.Join(", ", servceNames)); + return new BindingBuilder(firstBinding.BindingConfiguration, this.KernelInstance, string.Join(", ", serviceNames)); } /// @@ -106,9 +104,9 @@ public IBindingToSyntax Bind() this.AddBinding(new Binding(typeof(T2), firstBinding.BindingConfiguration)); this.AddBinding(new Binding(typeof(T3), firstBinding.BindingConfiguration)); this.AddBinding(new Binding(typeof(T4), firstBinding.BindingConfiguration)); - var servceNames = new[] { typeof(T1).Format(), typeof(T2).Format(), typeof(T3).Format(), typeof(T4).Format() }; + var serviceNames = new[] { typeof(T1).Format(), typeof(T2).Format(), typeof(T3).Format(), typeof(T4).Format() }; - return new BindingBuilder(firstBinding.BindingConfiguration, this.KernelInstance, string.Join(", ", servceNames)); + return new BindingBuilder(firstBinding.BindingConfiguration, this.KernelInstance, string.Join(", ", serviceNames)); } /// @@ -121,7 +119,7 @@ public IBindingToSyntax Bind(params Type[] services) Ensure.ArgumentNotNull(services, "service"); if (services.Length == 0) { - throw new ArgumentException("The services must contain at least one type", "services"); + throw new ArgumentException("The services must contain at least one type", "services"); } var firstBinding = new Binding(services[0]); @@ -129,7 +127,7 @@ public IBindingToSyntax Bind(params Type[] services) foreach (var service in services.Skip(1)) { - this.AddBinding(new Binding(service, firstBinding.BindingConfiguration)); + this.AddBinding(new Binding(service, firstBinding.BindingConfiguration)); } return new BindingBuilder(firstBinding, this.KernelInstance, string.Join(", ", services.Select(service => service.Format()).ToArray())); @@ -141,7 +139,7 @@ public IBindingToSyntax Bind(params Type[] services) /// The service to unbind. public void Unbind() { - Unbind(typeof(T)); + this.Unbind(typeof(T)); } /// @@ -157,8 +155,8 @@ public void Unbind() /// The fluent syntax public IBindingToSyntax Rebind() { - Unbind(); - return Bind(); + this.Unbind(); + return this.Bind(); } /// @@ -169,9 +167,9 @@ public IBindingToSyntax Rebind() /// The fluent syntax. public IBindingToSyntax Rebind() { - Unbind(); - Unbind(); - return Bind(); + this.Unbind(); + this.Unbind(); + return this.Bind(); } /// @@ -183,10 +181,10 @@ public IBindingToSyntax Rebind() /// The fluent syntax. public IBindingToSyntax Rebind() { - Unbind(); - Unbind(); - Unbind(); - return Bind(); + this.Unbind(); + this.Unbind(); + this.Unbind(); + return this.Bind(); } /// @@ -199,11 +197,11 @@ public IBindingToSyntax Rebind() /// The fluent syntax. public IBindingToSyntax Rebind() { - Unbind(); - Unbind(); - Unbind(); - Unbind(); - return Bind(); + this.Unbind(); + this.Unbind(); + this.Unbind(); + this.Unbind(); + return this.Bind(); } /// @@ -215,10 +213,10 @@ public IBindingToSyntax Rebind(params Type[] services) { foreach (var service in services) { - Unbind(service); + this.Unbind(service); } - return Bind(services); + return this.Bind(services); } /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingInNamedWithOrOnSyntax.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingInNamedWithOrOnSyntax.cs index 40ffb731..9c44b693 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingInNamedWithOrOnSyntax.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingInNamedWithOrOnSyntax.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Syntax { @@ -27,10 +25,10 @@ namespace Telerik.JustMock.AutoMock.Ninject.Syntax /// Used to set the scope, name, or add additional information or actions to a binding. /// /// The service being bound. - public interface IBindingInNamedWithOrOnSyntax : - IBindingInSyntax, - IBindingNamedSyntax, - IBindingWithSyntax, + public interface IBindingInNamedWithOrOnSyntax : + IBindingInSyntax, + IBindingNamedSyntax, + IBindingWithSyntax, IBindingOnSyntax { } diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingInSyntax.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingInSyntax.cs index 5d3e270f..53aac015 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingInSyntax.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingInSyntax.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Syntax { diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingNamedSyntax.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingNamedSyntax.cs index bccda9f1..fec05ae8 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingNamedSyntax.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingNamedSyntax.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Syntax { diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingNamedWithOrOnSyntax.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingNamedWithOrOnSyntax.cs index 83dc4626..f5e1a83b 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingNamedWithOrOnSyntax.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingNamedWithOrOnSyntax.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Syntax { diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingOnSyntax.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingOnSyntax.cs index 57f7bfad..a2b3e93b 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingOnSyntax.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingOnSyntax.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Syntax { diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingRoot.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingRoot.cs index 79e48e79..951bcb93 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingRoot.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingRoot.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,11 +17,12 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Syntax { using System; + using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingSyntax.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingSyntax.cs index 70686a43..cc92b471 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingSyntax.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingSyntax.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Syntax { diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingToSyntax{T1,T2,T3,T4}.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingToSyntax{T1,T2,T3,T4}.cs index d5f52cfe..43b3fb45 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingToSyntax{T1,T2,T3,T4}.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingToSyntax{T1,T2,T3,T4}.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,14 +17,13 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Syntax { using System; -#if !NETCF using System.Linq.Expressions; -#endif + using Telerik.JustMock.AutoMock.Ninject.Activation; /// @@ -43,7 +40,7 @@ public interface IBindingToSyntax : IBindingSyntax /// /// The implementation type. /// The fluent syntax. - IBindingWhenInNamedWithOrOnSyntax To() + IBindingWhenInNamedWithOrOnSyntax To() where TImplementation : T1, T2, T3, T4; /// @@ -59,7 +56,8 @@ IBindingWhenInNamedWithOrOnSyntax To() /// /// The type of provider to activate. /// The fluent syntax. - IBindingWhenInNamedWithOrOnSyntax ToProvider() where TProvider : IProvider; + IBindingWhenInNamedWithOrOnSyntax ToProvider() + where TProvider : IProvider; /// /// Indicates that the service should be bound to an instance of the specified provider type. @@ -68,7 +66,7 @@ IBindingWhenInNamedWithOrOnSyntax To() /// The type of provider to activate. /// The type of the implementation. /// The fluent syntax. - IBindingWhenInNamedWithOrOnSyntax ToProvider() + IBindingWhenInNamedWithOrOnSyntax ToProvider() where TProvider : IProvider where TImplementation : T1, T2, T3, T4; @@ -108,9 +106,8 @@ IBindingWhenInNamedWithOrOnSyntax ToMethod( IBindingWhenInNamedWithOrOnSyntax ToConstant(TImplementation value) where TImplementation : T1, T2, T3, T4; -#if !NETCF /// - /// Indicates that the service should be bound to the speecified constructor. + /// Indicates that the service should be bound to the specified constructor. /// /// The type of the implementation. /// The expression that specifies the constructor. @@ -118,6 +115,5 @@ IBindingWhenInNamedWithOrOnSyntax ToConstant(T IBindingWhenInNamedWithOrOnSyntax ToConstructor( Expression> newExpression) where TImplementation : T1, T2, T3, T4; -#endif } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingToSyntax{T1,T2,T3}.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingToSyntax{T1,T2,T3}.cs index fca0ca92..79fb9a7f 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingToSyntax{T1,T2,T3}.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingToSyntax{T1,T2,T3}.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,14 +17,13 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Syntax { using System; -#if !NETCF using System.Linq.Expressions; -#endif + using Telerik.JustMock.AutoMock.Ninject.Activation; /// @@ -42,7 +39,7 @@ public interface IBindingToSyntax : IBindingSyntax /// /// The implementation type. /// The fluent syntax. - IBindingWhenInNamedWithOrOnSyntax To() + IBindingWhenInNamedWithOrOnSyntax To() where TImplementation : T1, T2, T3; /// @@ -58,7 +55,8 @@ IBindingWhenInNamedWithOrOnSyntax To() /// /// The type of provider to activate. /// The fluent syntax. - IBindingWhenInNamedWithOrOnSyntax ToProvider() where TProvider : IProvider; + IBindingWhenInNamedWithOrOnSyntax ToProvider() + where TProvider : IProvider; /// /// Indicates that the service should be bound to an instance of the specified provider type. @@ -67,7 +65,7 @@ IBindingWhenInNamedWithOrOnSyntax To() /// The type of provider to activate. /// The type of the implementation. /// The fluent syntax. - IBindingWhenInNamedWithOrOnSyntax ToProvider() + IBindingWhenInNamedWithOrOnSyntax ToProvider() where TProvider : IProvider where TImplementation : T1, T2, T3; @@ -107,9 +105,8 @@ IBindingWhenInNamedWithOrOnSyntax ToMethod( IBindingWhenInNamedWithOrOnSyntax ToConstant(TImplementation value) where TImplementation : T1, T2, T3; -#if !NETCF /// - /// Indicates that the service should be bound to the speecified constructor. + /// Indicates that the service should be bound to the specified constructor. /// /// The type of the implementation. /// The expression that specifies the constructor. @@ -117,6 +114,5 @@ IBindingWhenInNamedWithOrOnSyntax ToConstant(T IBindingWhenInNamedWithOrOnSyntax ToConstructor( Expression> newExpression) where TImplementation : T1, T2, T3; -#endif } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingToSyntax{T1,T2}.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingToSyntax{T1,T2}.cs index f1bae6a9..0cfbb04b 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingToSyntax{T1,T2}.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingToSyntax{T1,T2}.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,14 +17,13 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Syntax { using System; -#if !NETCF using System.Linq.Expressions; -#endif + using Telerik.JustMock.AutoMock.Ninject.Activation; /// @@ -41,7 +38,7 @@ public interface IBindingToSyntax : IBindingSyntax /// /// The implementation type. /// The fluent syntax. - IBindingWhenInNamedWithOrOnSyntax To() + IBindingWhenInNamedWithOrOnSyntax To() where TImplementation : T1, T2; /// @@ -57,7 +54,8 @@ IBindingWhenInNamedWithOrOnSyntax To() /// /// The type of provider to activate. /// The fluent syntax. - IBindingWhenInNamedWithOrOnSyntax ToProvider() where TProvider : IProvider; + IBindingWhenInNamedWithOrOnSyntax ToProvider() + where TProvider : IProvider; /// /// Indicates that the service should be bound to an instance of the specified provider type. @@ -66,7 +64,7 @@ IBindingWhenInNamedWithOrOnSyntax To() /// The type of provider to activate. /// The type of the implementation. /// The fluent syntax. - IBindingWhenInNamedWithOrOnSyntax ToProvider() + IBindingWhenInNamedWithOrOnSyntax ToProvider() where TProvider : IProvider where TImplementation : T1, T2; @@ -106,9 +104,8 @@ IBindingWhenInNamedWithOrOnSyntax ToMethod( IBindingWhenInNamedWithOrOnSyntax ToConstant(TImplementation value) where TImplementation : T1, T2; -#if !NETCF /// - /// Indicates that the service should be bound to the speecified constructor. + /// Indicates that the service should be bound to the specified constructor. /// /// The type of the implementation. /// The expression that specifies the constructor. @@ -116,6 +113,5 @@ IBindingWhenInNamedWithOrOnSyntax ToConstant(T IBindingWhenInNamedWithOrOnSyntax ToConstructor( Expression> newExpression) where TImplementation : T1, T2; -#endif } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingToSyntax{T1}.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingToSyntax{T1}.cs index 1f4a53b7..149fabb5 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingToSyntax{T1}.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingToSyntax{T1}.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,14 +17,13 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Syntax { using System; -#if !NETCF using System.Linq.Expressions; -#endif + using Telerik.JustMock.AutoMock.Ninject.Activation; /// @@ -46,7 +43,7 @@ public interface IBindingToSyntax : IBindingSyntax /// /// The implementation type. /// The fluent syntax. - IBindingWhenInNamedWithOrOnSyntax To() + IBindingWhenInNamedWithOrOnSyntax To() where TImplementation : T1; /// @@ -62,7 +59,7 @@ IBindingWhenInNamedWithOrOnSyntax To() /// /// The type of provider to activate. /// The fluent syntax. - IBindingWhenInNamedWithOrOnSyntax ToProvider() + IBindingWhenInNamedWithOrOnSyntax ToProvider() where TProvider : IProvider; /// @@ -108,7 +105,6 @@ IBindingWhenInNamedWithOrOnSyntax ToMethod( IBindingWhenInNamedWithOrOnSyntax ToConstant(TImplementation value) where TImplementation : T1; -#if !NETCF /// /// Indicates that the service should be bound to the specified constructor. /// @@ -118,6 +114,5 @@ IBindingWhenInNamedWithOrOnSyntax ToConstant(T IBindingWhenInNamedWithOrOnSyntax ToConstructor( Expression> newExpression) where TImplementation : T1; -#endif } -} +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingWhenInNamedWithOrOnSyntax.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingWhenInNamedWithOrOnSyntax.cs index 20f03c7a..74a48d5d 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingWhenInNamedWithOrOnSyntax.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingWhenInNamedWithOrOnSyntax.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Syntax { diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingWhenSyntax.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingWhenSyntax.cs index 4968222d..fb262a47 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingWhenSyntax.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingWhenSyntax.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Syntax { @@ -68,7 +66,7 @@ public interface IBindingWhenSyntax : IBindingSyntax /// /// Indicates that the binding should be used only for injections on the specified type. /// The type must match exactly the specified type. Types that derive from the specified type - /// will not be considered as valid target. + /// will not be considered as valid target. /// /// The type. /// The fluent syntax. @@ -77,7 +75,7 @@ public interface IBindingWhenSyntax : IBindingSyntax /// /// Indicates that the binding should be used only for injections on the specified type. /// The type must match exactly the specified type. Types that derive from the specified type - /// will not be considered as valid target. + /// will not be considered as valid target. /// /// The type. /// The fluent syntax. @@ -86,7 +84,7 @@ public interface IBindingWhenSyntax : IBindingSyntax /// /// Indicates that the binding should be used only for injections on the specified type. /// The type must match one of the specified types exactly. Types that derive from one of the specified types - /// will not be considered as valid target. + /// will not be considered as valid target. /// Should match at least one of the specified targets /// /// The types. @@ -99,7 +97,8 @@ public interface IBindingWhenSyntax : IBindingSyntax /// /// The type of attribute. /// The fluent syntax. - IBindingInNamedWithOrOnSyntax WhenClassHas() where TAttribute : Attribute; + IBindingInNamedWithOrOnSyntax WhenClassHas() + where TAttribute : Attribute; /// /// Indicates that the binding should be used only when the member being injected has @@ -107,7 +106,8 @@ public interface IBindingWhenSyntax : IBindingSyntax /// /// The type of attribute. /// The fluent syntax. - IBindingInNamedWithOrOnSyntax WhenMemberHas() where TAttribute : Attribute; + IBindingInNamedWithOrOnSyntax WhenMemberHas() + where TAttribute : Attribute; /// /// Indicates that the binding should be used only when the target being injected has @@ -115,7 +115,8 @@ public interface IBindingWhenSyntax : IBindingSyntax /// /// The type of attribute. /// The fluent syntax. - IBindingInNamedWithOrOnSyntax WhenTargetHas() where TAttribute : Attribute; + IBindingInNamedWithOrOnSyntax WhenTargetHas() + where TAttribute : Attribute; /// /// Indicates that the binding should be used only when the class being injected has @@ -170,7 +171,7 @@ public interface IBindingWhenSyntax : IBindingSyntax /// The name to expect. /// The fluent syntax. IBindingInNamedWithOrOnSyntax WhenNoAncestorNamed(string name); - + /// /// Indicates that the binding should be used only when any ancestor matches the specified predicate. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingWithOrOnSyntax.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingWithOrOnSyntax.cs index 6e207a87..9aa97f44 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingWithOrOnSyntax.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingWithOrOnSyntax.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Syntax { diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingWithSyntax.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingWithSyntax.cs index a5bb2c2e..20cea215 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingWithSyntax.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/IBindingWithSyntax.cs @@ -1,12 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2007-2009, Enkari, Ltd. -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Nate Kohari (nate@enkari.com) -// Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -19,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Syntax { @@ -59,6 +57,54 @@ public interface IBindingWithSyntax : IBindingSyntax /// The fluent syntax. IBindingWithOrOnSyntax WithConstructorArgument(string name, Func callback); + /// + /// Indicates that the specified constructor argument should be overridden with the specified value. + /// + /// Specifies the argument type to override. + /// The value for the argument. + /// The fluent syntax. + IBindingWithOrOnSyntax WithConstructorArgument(TValue value); + + /// + /// Indicates that the specified constructor argument should be overridden with the specified value. + /// + /// The type of the argument to override. + /// The value for the argument. + /// The fluent syntax. + IBindingWithOrOnSyntax WithConstructorArgument(Type type, object value); + + /// + /// Indicates that the specified constructor argument should be overridden with the specified value. + /// + /// The type of the argument to override. + /// The callback to invoke to get the value for the argument. + /// The fluent syntax. + IBindingWithOrOnSyntax WithConstructorArgument(Func callback); + + /// + /// Indicates that the specified constructor argument should be overridden with the specified value. + /// + /// The type of the argument to override. + /// The callback to invoke to get the value for the argument. + /// The fluent syntax. + IBindingWithOrOnSyntax WithConstructorArgument(Type type, Func callback); + + /// + /// Indicates that the specified constructor argument should be overridden with the specified value. + /// + /// The type of the argument to override. + /// The callback to invoke to get the value for the argument. + /// The fluent syntax. + IBindingWithOrOnSyntax WithConstructorArgument(Func callback); + + /// + /// Indicates that the specified constructor argument should be overridden with the specified value. + /// + /// The type of the argument to override. + /// The callback to invoke to get the value for the argument. + /// The fluent syntax. + IBindingWithOrOnSyntax WithConstructorArgument(Type type, Func callback); + /// /// Indicates that the specified property should be injected with the specified value. /// diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/IConstructorArgumentSyntax.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/IConstructorArgumentSyntax.cs index f5130ac3..e976815b 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/IConstructorArgumentSyntax.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/IConstructorArgumentSyntax.cs @@ -1,10 +1,10 @@ -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// Copyright (c) 2009-2011 Ninject Project Contributors -// Authors: Remo Gloor (remo.gloor@gmail.com) -// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// you may not use this file except in compliance with one of the Licenses. +// You may not use this file except in compliance with one of the Licenses. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 @@ -17,7 +17,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -//------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Syntax { diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/IFluentSyntax.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/IFluentSyntax.cs index 426e17a3..aebf39db 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/IFluentSyntax.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/IFluentSyntax.cs @@ -1,19 +1,29 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.ComponentModel; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Syntax { + using System; + using System.ComponentModel; + /// /// A hack to hide methods defined on for IntelliSense /// on fluent interfaces. Credit to Daniel Cazzulino. @@ -32,28 +42,28 @@ public interface IFluentSyntax /// Returns a hash code for this instance. /// /// - /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// - [EditorBrowsable(EditorBrowsableState.Never)] + [EditorBrowsable(EditorBrowsableState.Never)] int GetHashCode(); /// - /// Returns a that represents this instance. + /// Returns a that represents this instance. /// /// - /// A that represents this instance. + /// A that represents this instance. /// - [EditorBrowsable(EditorBrowsableState.Never)] + [EditorBrowsable(EditorBrowsableState.Never)] string ToString(); /// - /// Determines whether the specified is equal to this instance. + /// Determines whether the specified is equal to this instance. /// - /// The to compare with this instance. + /// The to compare with this instance. /// - /// true if the specified is equal to this instance; otherwise, false. + /// true if the specified is equal to this instance; otherwise, false. /// - [EditorBrowsable(EditorBrowsableState.Never)] + [EditorBrowsable(EditorBrowsableState.Never)] bool Equals(object other); } } \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/IResolutionRoot.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/IResolutionRoot.cs index cac28bae..e0acc339 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/IResolutionRoot.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/IResolutionRoot.cs @@ -1,27 +1,45 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using Telerik.JustMock.AutoMock.Ninject.Activation; -using Telerik.JustMock.AutoMock.Ninject.Parameters; -using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject.Syntax { + using System; + using System.Collections.Generic; + + using Telerik.JustMock.AutoMock.Ninject.Activation; + using Telerik.JustMock.AutoMock.Ninject.Parameters; + using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; + /// /// Provides a path to resolve instances. /// public interface IResolutionRoot : IFluentSyntax { + /// + /// Injects the specified existing instance, without managing its lifecycle. + /// + /// The instance to inject. + /// The parameters to pass to the request. + void Inject(object instance, params IParameter[] parameters); + /// /// Determines whether the specified request can be resolved. /// @@ -38,7 +56,7 @@ public interface IResolutionRoot : IFluentSyntax /// True if the request can be resolved; otherwise, false. /// bool CanResolve(IRequest request, bool ignoreImplicitBindings); - + /// /// Resolves instances for the specified request. The instances are not actually resolved /// until a consumer iterates over the enumerator. diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/ModuleLoadExtensions.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/ModuleLoadExtensions.cs index 65810196..a306dbe4 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/ModuleLoadExtensions.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/ModuleLoadExtensions.cs @@ -1,21 +1,31 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Reflection; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Modules; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject { + using System.Reflection; + + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Modules; + /// /// Extension methods that enhance module loading. /// @@ -43,7 +53,6 @@ public static void Load(this IKernel kernel, params INinjectModule[] modules) kernel.Load(modules); } - #if !NO_ASSEMBLY_SCANNING /// /// Loads modules from the files that match the specified pattern(s). /// @@ -63,6 +72,5 @@ public static void Load(this IKernel kernel, params Assembly[] assemblies) { kernel.Load(assemblies); } - #endif } -} +} \ No newline at end of file diff --git a/Telerik.JustMock/AutoMock/Ninject/Syntax/ResolutionExtensions.cs b/Telerik.JustMock/AutoMock/Ninject/Syntax/ResolutionExtensions.cs index d980e18d..61418113 100644 --- a/Telerik.JustMock/AutoMock/Ninject/Syntax/ResolutionExtensions.cs +++ b/Telerik.JustMock/AutoMock/Ninject/Syntax/ResolutionExtensions.cs @@ -1,25 +1,35 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2010, Enkari, Ltd. -// -// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). -// See the file LICENSE.txt for details. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using System.Linq; -using Telerik.JustMock.AutoMock.Ninject.Activation; -using Telerik.JustMock.AutoMock.Ninject.Infrastructure; -using Telerik.JustMock.AutoMock.Ninject.Parameters; -using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; -using Telerik.JustMock.AutoMock.Ninject.Syntax; -#endregion +// ------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2007-2010 Enkari, Ltd. All rights reserved. +// Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. +// +// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). +// You may not use this file except in compliance with one of the Licenses. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// or +// http://www.microsoft.com/opensource/licenses.mspx +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ------------------------------------------------------------------------------------------------- namespace Telerik.JustMock.AutoMock.Ninject { + using System; + using System.Collections.Generic; + using System.Linq; + + using Telerik.JustMock.AutoMock.Ninject.Infrastructure; + using Telerik.JustMock.AutoMock.Ninject.Parameters; + using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings; + using Telerik.JustMock.AutoMock.Ninject.Syntax; + /// /// Extensions that enhance resolution of services. /// @@ -72,7 +82,7 @@ public static T Get(this IResolutionRoot root, Func c /// An instance of the service, or if no implementation was available. public static T TryGet(this IResolutionRoot root, params IParameter[] parameters) { - return TryGet(GetResolutionIterator(root, typeof(T), null, parameters, true, true).Cast()); + return TryGet(() => GetResolutionIterator(root, typeof(T), null, parameters, true, true).Cast()); } /// @@ -85,7 +95,7 @@ public static T TryGet(this IResolutionRoot root, params IParameter[] paramet /// An instance of the service, or if no implementation was available. public static T TryGet(this IResolutionRoot root, string name, params IParameter[] parameters) { - return TryGet(GetResolutionIterator(root, typeof(T), b => b.Name == name, parameters, true, true).Cast()); + return TryGet(() => GetResolutionIterator(root, typeof(T), b => b.Name == name, parameters, true, true).Cast()); } /// @@ -98,7 +108,45 @@ public static T TryGet(this IResolutionRoot root, string name, params IParame /// An instance of the service, or if no implementation was available. public static T TryGet(this IResolutionRoot root, Func constraint, params IParameter[] parameters) { - return TryGet(GetResolutionIterator(root, typeof(T), constraint, parameters, true, true).Cast()); + return TryGet(() => GetResolutionIterator(root, typeof(T), constraint, parameters, true, true).Cast()); + } + + /// + /// Tries to get an instance of the specified service. + /// + /// The service to resolve. + /// The resolution root. + /// The parameters to pass to the request. + /// An instance of the service, or if no implementation was available. + public static T TryGetAndThrowOnInvalidBinding(this IResolutionRoot root, params IParameter[] parameters) + { + return DoTryGetAndThrowOnInvalidBinding(root, null, parameters); + } + + /// + /// Tries to get an instance of the specified service by using the first binding with the specified name. + /// + /// The service to resolve. + /// The resolution root. + /// The name of the binding. + /// The parameters to pass to the request. + /// An instance of the service, or if no implementation was available. + public static T TryGetAndThrowOnInvalidBinding(this IResolutionRoot root, string name, params IParameter[] parameters) + { + return DoTryGetAndThrowOnInvalidBinding(root, b => b.Name == name, parameters); + } + + /// + /// Tries to get an instance of the specified service by using the first binding that matches the specified constraint. + /// + /// The service to resolve. + /// The resolution root. + /// The constraint to apply to the binding. + /// The parameters to pass to the request. + /// An instance of the service, or if no implementation was available. + public static T TryGetAndThrowOnInvalidBinding(this IResolutionRoot root, Func constraint, params IParameter[] parameters) + { + return DoTryGetAndThrowOnInvalidBinding(root, constraint, parameters); } /// @@ -186,7 +234,7 @@ public static object Get(this IResolutionRoot root, Type service, FuncAn instance of the service, or if no implementation was available. public static object TryGet(this IResolutionRoot root, Type service, params IParameter[] parameters) { - return TryGet(GetResolutionIterator(root, service, null, parameters, true, true)); + return TryGet(() => GetResolutionIterator(root, service, null, parameters, true, true)); } /// @@ -199,7 +247,7 @@ public static object TryGet(this IResolutionRoot root, Type service, params IPar /// An instance of the service, or if no implementation was available. public static object TryGet(this IResolutionRoot root, Type service, string name, params IParameter[] parameters) { - return TryGet(GetResolutionIterator(root, service, b => b.Name == name, parameters, true, false)); + return TryGet(() => GetResolutionIterator(root, service, b => b.Name == name, parameters, true, true)); } /// @@ -212,7 +260,7 @@ public static object TryGet(this IResolutionRoot root, Type service, string name /// An instance of the service, or if no implementation was available. public static object TryGet(this IResolutionRoot root, Type service, Func constraint, params IParameter[] parameters) { - return TryGet(GetResolutionIterator(root, service, constraint, parameters, true, false)); + return TryGet(() => GetResolutionIterator(root, service, constraint, parameters, true, true)); } /// @@ -259,7 +307,7 @@ public static IEnumerable GetAll(this IResolutionRoot root, Type service /// The service to resolve. /// The resolution root. /// The parameters to pass to the request. - /// An instance of the service. + /// True if the request can be resolved; otherwise, false. public static bool CanResolve(this IResolutionRoot root, params IParameter[] parameters) { return CanResolve(root, typeof(T), null, parameters, false, true); @@ -272,7 +320,7 @@ public static bool CanResolve(this IResolutionRoot root, params IParameter[] /// The resolution root. /// The name of the binding. /// The parameters to pass to the request. - /// An instance of the service. + /// True if the request can be resolved; otherwise, false. public static bool CanResolve(this IResolutionRoot root, string name, params IParameter[] parameters) { return CanResolve(root, typeof(T), b => b.Name == name, parameters, false, true); @@ -285,7 +333,7 @@ public static bool CanResolve(this IResolutionRoot root, string name, params /// The resolution root. /// The constraint to apply to the binding. /// The parameters to pass to the request. - /// An instance of the service. + /// True if the request can be resolved; otherwise, false. public static bool CanResolve(this IResolutionRoot root, Func constraint, params IParameter[] parameters) { return CanResolve(root, typeof(T), constraint, parameters, false, true); @@ -297,8 +345,8 @@ public static bool CanResolve(this IResolutionRoot root, FuncThe resolution root. /// The service to resolve. /// The parameters to pass to the request. - /// An instance of the service. - public static object CanResolve(this IResolutionRoot root, Type service, params IParameter[] parameters) + /// True if the request can be resolved; otherwise, false. + public static bool CanResolve(this IResolutionRoot root, Type service, params IParameter[] parameters) { return CanResolve(root, service, null, parameters, false, true); } @@ -310,8 +358,8 @@ public static object CanResolve(this IResolutionRoot root, Type service, params /// The service to resolve. /// The name of the binding. /// The parameters to pass to the request. - /// An instance of the service. - public static object CanResolve(this IResolutionRoot root, Type service, string name, params IParameter[] parameters) + /// True if the request can be resolved; otherwise, false. + public static bool CanResolve(this IResolutionRoot root, Type service, string name, params IParameter[] parameters) { return CanResolve(root, service, b => b.Name == name, parameters, false, true); } @@ -323,8 +371,8 @@ public static object CanResolve(this IResolutionRoot root, Type service, string /// The service to resolve. /// The constraint to apply to the binding. /// The parameters to pass to the request. - /// An instance of the service. - public static object CanResolve(this IResolutionRoot root, Type service, Func constraint, params IParameter[] parameters) + /// True if the request can be resolved; otherwise, false. + public static bool CanResolve(this IResolutionRoot root, Type service, Func constraint, params IParameter[] parameters) { return CanResolve(root, service, constraint, parameters, false, true); } @@ -335,7 +383,7 @@ private static bool CanResolve(IResolutionRoot root, Type service, Func GetResolutionIterator(IResolutionRoot root, T Ensure.ArgumentNotNull(service, "service"); Ensure.ArgumentNotNull(parameters, "parameters"); - IRequest request = root.CreateRequest(service, constraint, parameters, isOptional, isUnique); + var request = root.CreateRequest(service, constraint, parameters, isOptional, isUnique); + return root.Resolve(request); + } + + private static IEnumerable GetResolutionIterator(IResolutionRoot root, Type service, Func constraint, IEnumerable parameters, bool isOptional, bool isUnique, bool forceUnique) + { + Ensure.ArgumentNotNull(root, "root"); + Ensure.ArgumentNotNull(service, "service"); + Ensure.ArgumentNotNull(parameters, "parameters"); + + var request = root.CreateRequest(service, constraint, parameters, isOptional, isUnique); + request.ForceUnique = forceUnique; return root.Resolve(request); } - private static T TryGet(IEnumerable iterator) + private static T TryGet(Func> iterator) { try { - return iterator.SingleOrDefault(); + return iterator().SingleOrDefault(); } catch (ActivationException) { return default(T); } } + + private static T DoTryGetAndThrowOnInvalidBinding(IResolutionRoot root, Func constraint, IEnumerable parameters) + { + return GetResolutionIterator(root, typeof(T), constraint, parameters, true, true, true).Cast().SingleOrDefault(); + } } } diff --git a/Telerik.JustMock/AutoMock/Ninject/VERSION b/Telerik.JustMock/AutoMock/Ninject/VERSION new file mode 100644 index 00000000..eedb52ba --- /dev/null +++ b/Telerik.JustMock/AutoMock/Ninject/VERSION @@ -0,0 +1 @@ +3.3.6 \ No newline at end of file diff --git a/Telerik.JustMock/Telerik.JustMock.csproj b/Telerik.JustMock/Telerik.JustMock.csproj index caabf301..75251cf7 100644 --- a/Telerik.JustMock/Telerik.JustMock.csproj +++ b/Telerik.JustMock/Telerik.JustMock.csproj @@ -1,7 +1,8 @@  + net45;net472 netcoreapp2.0 - net45;net472;$(NetCoreSupportedVersions) + $(NetFxSupportedVersions);$(NetCoreSupportedVersions) Debug;Release;ReleaseFree;DebugFree Telerik.JustMock Telerik.JustMock @@ -10,7 +11,6 @@ false false - NO_EXCEPTION_SERIALIZATION Debug AnyCPU {0749EBC2-4E83-4960-BF28-1FF5C2DEB2B9} @@ -26,7 +26,7 @@ full false ..\..\..\Binaries\Debug\ - $(CommonConstants);TRACE;DEBUG + TRACE;DEBUG prompt 4 @@ -34,14 +34,14 @@ pdbonly true ..\..\..\Binaries\Release\ - $(CommonConstants);TRACE + TRACE prompt 4 ..\..\..\Binaries\Release\Telerik.JustMock.xml ..\..\..\Binaries\ReleaseFree\ - $(CommonConstants);TRACE;LITE_EDITION + TRACE;LITE_EDITION true pdbonly AnyCPU @@ -53,19 +53,19 @@ true ..\..\..\Binaries\DebugFree\ - $(CommonConstants);TRACE;DEBUG;LITE_EDITION + TRACE;DEBUG;LITE_EDITION full AnyCPU prompt MinimumRecommendedRules.ruleset - + $(DefineConstants);FEATURE_APPDOMAIN;FEATURE_ASSEMBLYBUILDER_SAVE - $(DefineConstants);NETCORE;NO_APPDOMAIN_ISOLATION + $(DefineConstants);NETCORE;NO_ASSEMBLY_SCANNING;NO_REMOTING - +