Added:
experiments/ninject2/src/Ninject.Tests/ShouldExtensions.cs
- copied, changed from r146,
/experiments/ninject2/src/Ninject.Tests/SpecExtensions.cs
experiments/ninject2/src/Ninject/Activation/Hook.cs
- copied, changed from r145,
/experiments/ninject2/src/Ninject/Activation/Hooks/IHook.cs
experiments/ninject2/src/Ninject/Planning/Bindings/BindingTarget.cs
- copied, changed from r146,
/experiments/ninject2/src/Ninject/Planning/Bindings/Binding.cs
Removed:
experiments/ninject2/src/Ninject.Tests/SpecExtensions.cs
experiments/ninject2/src/Ninject/Activation/Hooks/
Modified:
experiments/ninject2/src/Ninject.Tests/Integration/StandardKernelTests.cs
experiments/ninject2/src/Ninject.Tests/Ninject.Tests.csproj
experiments/ninject2/src/Ninject/Activation/Context.cs
experiments/ninject2/src/Ninject/Activation/IContext.cs
experiments/ninject2/src/Ninject/Activation/Scope/ActivationScope.cs
experiments/ninject2/src/Ninject/Infrastructure/Introspection/FormatExtensions.cs
experiments/ninject2/src/Ninject/KernelBase.cs
experiments/ninject2/src/Ninject/Ninject.csproj
experiments/ninject2/src/Ninject/Planning/Bindings/Binding.cs
experiments/ninject2/src/Ninject/Planning/Bindings/BindingBuilder.cs
experiments/ninject2/src/Ninject/Planning/Bindings/BindingMetadata.cs
experiments/ninject2/src/Ninject/Planning/Bindings/IBinding.cs
experiments/ninject2/src/Ninject/Planning/Bindings/IBindingMetadata.cs
experiments/ninject2/src/Ninject/Syntax/IResolutionRoot.cs
experiments/ninject2/src/Ninject/Syntax/ResolutionExtensions.cs
Log:
Modified:
experiments/ninject2/src/Ninject.Tests/Integration/StandardKernelTests.cs
==============================================================================
---
experiments/ninject2/src/Ninject.Tests/Integration/StandardKernelTests.cs
(original)
+++
experiments/ninject2/src/Ninject.Tests/Integration/StandardKernelTests.cs
Mon Feb 23 20:27:09 2009
@@ -136,6 +136,47 @@
}
}
+ public class WhenTryGetIsCalledForInterfaceBoundService :
StandardKernelContext
+ {
+ public void SingleInstanceIsReturnedWhenOneBindingIsRegistered()
+ {
+ kernel.Bind<IWeapon>().To<Sword>();
+
+ var weapon = kernel.TryGet<IWeapon>();
+
+ weapon.ShouldNotBeNull();
+ weapon.ShouldBeInstanceOf<Sword>();
+ }
+
+ public void FirstInstanceIsReturnedWhenMultipleBindingsAreRegistered()
+ {
+ kernel.Bind<IWeapon>().To<Sword>();
+ kernel.Bind<IWeapon>().To<Shuriken>();
+
+ var weapon = kernel.TryGet<IWeapon>();
+
+ weapon.ShouldNotBeNull();
+ weapon.ShouldBeInstanceOf<Sword>();
+ }
+ }
+
+ public class WhenTryGetIsCalledForUnboundService : StandardKernelContext
+ {
+ public void
ImplicitSelfBindingIsRegisteredAndActivatedIfTypeIsSelfBindable()
+ {
+ var weapon = kernel.TryGet<Sword>();
+
+ weapon.ShouldNotBeNull();
+ weapon.ShouldBeInstanceOf<Sword>();
+ }
+
+ public void ReturnsNullIfTypeIsNotSelfBindable()
+ {
+ var weapon = kernel.TryGet<IWeapon>();
+ weapon.ShouldBeNull();
+ }
+ }
+
public class WhenGetAllIsCalledForInterfaceBoundService :
StandardKernelContext
{
[Fact]
@@ -185,6 +226,26 @@
services.Length.ShouldBe(2);
services[0].ShouldBeInstanceOf<GenericService<int>>();
services[1].ShouldBeInstanceOf<GenericService2<int>>();
+ }
+ }
+
+ public class WhenGetAllIsCalledForUnboundService : StandardKernelContext
+ {
+ public void
ImplicitSelfBindingIsRegisteredAndActivatedIfTypeIsSelfBindable()
+ {
+ var weapons = kernel.GetAll<Sword>().ToArray();
+
+ weapons.ShouldNotBeNull();
+ weapons.Length.ShouldBe(1);
+ weapons[0].ShouldBeInstanceOf<Sword>();
+ }
+
+ public void ReturnsEmptyEnumerableIfTypeIsNotSelfBindable()
+ {
+ var weapons = kernel.GetAll<IWeapon>().ToArray();
+
+ weapons.ShouldNotBeNull();
+ weapons.Length.ShouldBe(0);
}
}
Modified: experiments/ninject2/src/Ninject.Tests/Ninject.Tests.csproj
==============================================================================
--- experiments/ninject2/src/Ninject.Tests/Ninject.Tests.csproj (original)
+++ experiments/ninject2/src/Ninject.Tests/Ninject.Tests.csproj Mon Feb 23
20:27:09 2009
@@ -62,7 +62,7 @@
<Compile Include="Integration\RequestScopeTests.cs" />
<Compile Include="Integration\ExternalInjectionTests.cs" />
<Compile Include="Fakes\NotifiesWhenDisposed.cs" />
- <Compile Include="SpecExtensions.cs" />
+ <Compile Include="ShouldExtensions.cs" />
<Compile Include="ExtensionsForIEnumerable.cs" />
<Compile Include="Integration\CircularDependenciesTests.cs" />
<Compile Include="Integration\TransientScopeTests.cs" />
Copied: experiments/ninject2/src/Ninject.Tests/ShouldExtensions.cs (from
r146, /experiments/ninject2/src/Ninject.Tests/SpecExtensions.cs)
==============================================================================
--- /experiments/ninject2/src/Ninject.Tests/SpecExtensions.cs (original)
+++ experiments/ninject2/src/Ninject.Tests/ShouldExtensions.cs Mon Feb 23
20:27:09 2009
@@ -5,7 +5,7 @@
namespace Ninject.Tests
{
- public static class SpecExtensions
+ public static class ShouldExtensions
{
public static void ShouldContain(this string self, string str)
{
Modified: experiments/ninject2/src/Ninject/Activation/Context.cs
==============================================================================
--- experiments/ninject2/src/Ninject/Activation/Context.cs (original)
+++ experiments/ninject2/src/Ninject/Activation/Context.cs Mon Feb 23
20:27:09 2009
@@ -18,6 +18,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using Ninject.Activation.Caching;
+using Ninject.Infrastructure.Introspection;
using Ninject.Parameters;
using Ninject.Planning;
using Ninject.Planning.Bindings;
@@ -71,18 +73,40 @@
public bool HasInferredGenericArguments { get; private set; }
/// <summary>
+ /// Gets or sets the cache component.
+ /// </summary>
+ public ICache Cache { get; private set; }
+
+ /// <summary>
+ /// Gets or sets the planner component.
+ /// </summary>
+ public IPlanner Planner { get; private set; }
+
+ /// <summary>
+ /// Gets or sets the pipeline component.
+ /// </summary>
+ public IPipeline Pipeline { get; private set; }
+
+ /// <summary>
/// Initializes a new instance of the <see cref="Context"/> class.
/// </summary>
- /// <param name="kernel">The kernel.</param>
- /// <param name="request">The request.</param>
- /// <param name="binding">The binding.</param>
- public Context(IKernel kernel, IRequest request, IBinding binding)
+ /// <param name="kernel">The kernel managing the resolution.</param>
+ /// <param name="request">The context's request.</param>
+ /// <param name="binding">The context's binding.</param>
+ /// <param name="cache">The cache component.</param>
+ /// <param name="planner">The planner component.</param>
+ /// <param name="pipeline">The pipeline component.</param>
+ public Context(IKernel kernel, IRequest request, IBinding binding,
ICache cache, IPlanner planner, IPipeline 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;
@@ -106,6 +130,40 @@
public IProvider GetProvider()
{
return Binding.GetProvider(this);
+ }
+
+ /// <summary>
+ /// Resolves the instance associated with this hook.
+ /// </summary>
+ /// <returns>The resolved instance.</returns>
+ public object Resolve()
+ {
+ lock (Binding)
+ {
+ if (Request.ActiveBindings.Contains(Binding))
+ throw new
ActivationException(ExceptionFormatter.CyclicalDependenciesDetected(this));
+
+ Request.ActiveBindings.Push(Binding);
+
+ Instance = Cache.TryGet(this);
+
+ if (Instance != null)
+ return Instance;
+
+ Instance = GetProvider().Create(this);
+
+ if (GetScope() != null)
+ Cache.Remember(this);
+
+ Request.ActiveBindings.Pop();
+
+ if (Plan == null)
+ Plan = Planner.GetPlan(Instance.GetType());
+
+ Pipeline.Activate(this);
+
+ return Instance;
+ }
}
}
}
Copied: experiments/ninject2/src/Ninject/Activation/Hook.cs (from r145,
/experiments/ninject2/src/Ninject/Activation/Hooks/IHook.cs)
==============================================================================
--- /experiments/ninject2/src/Ninject/Activation/Hooks/IHook.cs (original)
+++ experiments/ninject2/src/Ninject/Activation/Hook.cs Mon Feb 23 20:27:09
2009
@@ -18,17 +18,37 @@
using System;
#endregion
-namespace Ninject.Activation.Hooks
+namespace Ninject.Activation
{
/// <summary>
- /// A hook to resolve an instance via Ninject.
+ /// A placeholder for an instance of a service.
/// </summary>
- public interface IHook
+ public class Hook
{
+ private readonly Func<object> _callback;
+
/// <summary>
- /// Resolves the instance associated with this hook.
+ /// Initializes a new instance of the <see cref="Hook"/> class.
+ /// </summary>
+ /// <param name="callback">The callback that will be triggered to
resolve the hook's instance.</param>
+ public Hook(Func<object> callback)
+ {
+ _callback = callback;
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="Hook"/> class.
+ /// </summary>
+ /// <param name="instance">The instance value that will be
returned.</param>
+ public Hook(object instance) : this(() => instance) { }
+
+ /// <summary>
+ /// Resolves the instance for the hook.
/// </summary>
/// <returns>The resolved instance.</returns>
- object Resolve();
+ public object Resolve()
+ {
+ return _callback();
+ }
}
}
Modified: experiments/ninject2/src/Ninject/Activation/IContext.cs
==============================================================================
--- experiments/ninject2/src/Ninject/Activation/IContext.cs (original)
+++ experiments/ninject2/src/Ninject/Activation/IContext.cs Mon Feb 23
20:27:09 2009
@@ -80,5 +80,11 @@
/// </summary>
/// <returns>The object that acts as the scope.</returns>
object GetScope();
+
+ /// <summary>
+ /// Resolves this instance for this context.
+ /// </summary>
+ /// <returns>The resolved instance.</returns>
+ object Resolve();
}
}
Modified:
experiments/ninject2/src/Ninject/Activation/Scope/ActivationScope.cs
==============================================================================
--- experiments/ninject2/src/Ninject/Activation/Scope/ActivationScope.cs
(original)
+++ experiments/ninject2/src/Ninject/Activation/Scope/ActivationScope.cs
Mon Feb 23 20:27:09 2009
@@ -17,7 +17,6 @@
#region Using Directives
using System;
using System.Collections.Generic;
-using Ninject.Activation.Hooks;
using Ninject.Infrastructure.Disposal;
using Ninject.Parameters;
using Ninject.Planning.Bindings;
@@ -64,7 +63,7 @@
/// <param name="parameters">The parameters to pass to the
resolution.</param>
/// <param name="isOptional"><c>True</c> if the request is optional;
otherwise, <c>false</c>.</param>
/// <returns>A series of hooks that can be used to resolve instances
that match the request.</returns>
- public IEnumerable<IHook> Resolve(Type service, Func<IBindingMetadata,
bool> constraint, IEnumerable<IParameter> parameters, bool isOptional)
+ public IEnumerable<Hook> Resolve(Type service, Func<IBindingMetadata,
bool> constraint, IEnumerable<IParameter> parameters, bool isOptional)
{
return Resolve(CreateDirectRequest(service, constraint, parameters,
isOptional));
}
@@ -74,7 +73,7 @@
/// </summary>
/// <param name="request">The request to resolve.</param>
/// <returns>A series of hooks that can be used to resolve instances
that match the request.</returns>
- public IEnumerable<IHook> Resolve(IRequest request)
+ public IEnumerable<Hook> Resolve(IRequest request)
{
return Parent.Resolve(request);
}
Modified:
experiments/ninject2/src/Ninject/Infrastructure/Introspection/FormatExtensions.cs
==============================================================================
---
experiments/ninject2/src/Ninject/Infrastructure/Introspection/FormatExtensions.cs
(original)
+++
experiments/ninject2/src/Ninject/Infrastructure/Introspection/FormatExtensions.cs
Mon Feb 23 20:27:09 2009
@@ -54,20 +54,37 @@
if (binding.Condition != null)
sw.Write("conditional ");
- if (binding.Metadata.IsImplicit)
+ if (binding.IsImplicit)
sw.Write("implicit ");
IProvider provider = binding.GetProvider(context);
- if (binding.Service == provider.Type)
- sw.Write("self-binding of {0}", binding.Service.Format());
- else
- sw.Write("binding from {0} to {1}", binding.Service.Format(),
provider.Type.Format());
+ switch (binding.Target)
+ {
+ case BindingTarget.Self:
+ sw.Write("self-binding of {0}", binding.Service.Format());
+ break;
- Type providerType = provider.GetType();
+ case BindingTarget.Type:
+ sw.Write("binding from {0} to {1}", binding.Service.Format(),
provider.Type.Format());
+ break;
- if (providerType != typeof(StandardProvider))
- sw.Write(" (via {0})", providerType.Format());
+ case BindingTarget.Provider:
+ sw.Write("provider binding from {0} to {1} (via {2})",
binding.Service.Format(),
+ provider.Type.Format(), provider.GetType().Format());
+ break;
+
+ case BindingTarget.Method:
+ sw.Write("binding from {0} to method", binding.Service.Format());
+ break;
+
+ case BindingTarget.Constant:
+ sw.Write("binding from {0} to constant value",
binding.Service.Format());
+ break;
+
+ default:
+ throw new ArgumentOutOfRangeException();
+ }
return sw.ToString();
}
Modified: experiments/ninject2/src/Ninject/KernelBase.cs
==============================================================================
--- experiments/ninject2/src/Ninject/KernelBase.cs (original)
+++ experiments/ninject2/src/Ninject/KernelBase.cs Mon Feb 23 20:27:09 2009
@@ -20,7 +20,6 @@
using System.Linq;
using Ninject.Activation;
using Ninject.Activation.Caching;
-using Ninject.Activation.Hooks;
using Ninject.Activation.Providers;
using Ninject.Activation.Scope;
using Ninject.Components;
@@ -246,7 +245,7 @@
/// <param name="parameters">The parameters to pass to the
resolution.</param>
/// <param name="isOptional"><c>True</c> if the request is optional;
otherwise, <c>false</c>.</param>
/// <returns>A series of hooks that can be used to resolve instances
that match the request.</returns>
- public virtual IEnumerable<IHook> Resolve(Type service,
Func<IBindingMetadata, bool> constraint, IEnumerable<IParameter>
parameters, bool isOptional)
+ public virtual IEnumerable<Hook> Resolve(Type service,
Func<IBindingMetadata, bool> constraint, IEnumerable<IParameter>
parameters, bool isOptional)
{
return Resolve(CreateDirectRequest(service, constraint, parameters,
isOptional));
}
@@ -256,15 +255,15 @@
/// </summary>
/// <param name="request">The request to resolve.</param>
/// <returns>A series of hooks that can be used to resolve instances
that match the request.</returns>
- public virtual IEnumerable<IHook> Resolve(IRequest request)
+ public virtual IEnumerable<Hook> Resolve(IRequest request)
{
if (request.Service == typeof(IKernel))
- return new[] { new ConstantHook(this) };
+ return new[] { new Hook(this) };
if (!CanResolve(request)
&& !TryRegisterImplicitSelfBinding(request.Service))
{
if (request.IsOptional)
- return Enumerable.Empty<IHook>();
+ return Enumerable.Empty<Hook>();
else
throw new
ActivationException(ExceptionFormatter.CouldNotResolveBinding(request));
}
@@ -317,8 +316,11 @@
if (service.IsInterface || service.IsAbstract ||
service.ContainsGenericParameters)
return false;
- var binding = new Binding(service) { ProviderCallback =
StandardProvider.GetCreationCallback(service) };
- binding.Metadata.IsImplicit = true;
+ var binding = new Binding(service)
+ {
+ ProviderCallback = StandardProvider.GetCreationCallback(service),
+ IsImplicit = true
+ };
AddBinding(binding);
@@ -359,7 +361,7 @@
/// <returns>The created context.</returns>
protected virtual IContext CreateContext(IRequest request, IBinding
binding)
{
- return new Context(this, request, binding);
+ return new Context(this, request, binding, Components.Get<ICache>(),
Components.Get<IPlanner>(), Components.Get<IPipeline>());
}
/// <summary>
@@ -367,9 +369,9 @@
/// </summary>
/// <param name="context">The context.</param>
/// <returns>The created hook.</returns>
- protected virtual IHook CreateHook(IContext context)
+ protected virtual Hook CreateHook(IContext context)
{
- return new ContextHook(context, Components.Get<ICache>(),
Components.Get<IPlanner>(), Components.Get<IPipeline>());
+ return new Hook(() => context.Resolve());
}
object IServiceProvider.GetService(Type serviceType)
Modified: experiments/ninject2/src/Ninject/Ninject.csproj
==============================================================================
--- experiments/ninject2/src/Ninject/Ninject.csproj (original)
+++ experiments/ninject2/src/Ninject/Ninject.csproj Mon Feb 23 20:27:09 2009
@@ -69,8 +69,7 @@
<Compile Include="Activation\Context.cs">
<SubType>Code</SubType>
</Compile>
- <Compile Include="Activation\Hooks\ContextHook.cs" />
- <Compile Include="Activation\Hooks\IHook.cs" />
+ <Compile Include="Activation\Hook.cs" />
<Compile Include="Activation\Request.cs" />
<Compile Include="Activation\IContext.cs">
<SubType>Code</SubType>
@@ -106,12 +105,12 @@
<Compile Include="Attributes\ConstraintAttribute.cs" />
<Compile Include="Attributes\NamedAttribute.cs" />
<Compile Include="Attributes\InjectAttribute.cs" />
- <Compile Include="Activation\Hooks\ConstantHook.cs" />
<Compile Include="ActivationException.cs" />
<Compile Include="Infrastructure\Introspection\ExceptionFormatter.cs"
/>
<Compile Include="Infrastructure\Introspection\FormatExtensions.cs" />
<Compile Include="Infrastructure\Language\ExtensionsForIDictionary.cs"
/>
<Compile Include="Injection\Expressions\ExpressionHelper.cs" />
+ <Compile Include="Planning\Bindings\BindingTarget.cs" />
<Compile Include="Syntax\ModuleLoadExtensions.cs" />
<Compile Include="Events\BindingEventArgs.cs" />
<Compile Include="Events\ModuleEventArgs.cs" />
Modified: experiments/ninject2/src/Ninject/Planning/Bindings/Binding.cs
==============================================================================
--- experiments/ninject2/src/Ninject/Planning/Bindings/Binding.cs (original)
+++ experiments/ninject2/src/Ninject/Planning/Bindings/Binding.cs Mon Feb
23 20:27:09 2009
@@ -39,6 +39,16 @@
public IBindingMetadata Metadata { get; private set; }
/// <summary>
+ /// Gets or sets a value indicating whether the binding was implicitly
registered.
+ /// </summary>
+ public bool IsImplicit { get; set; }
+
+ /// <summary>
+ /// Gets or sets the type of target for the binding.
+ /// </summary>
+ public BindingTarget Target { get; set; }
+
+ /// <summary>
/// Gets or sets the condition defined for the binding.
/// </summary>
public Func<IRequest, bool> Condition { get; set; }
Modified:
experiments/ninject2/src/Ninject/Planning/Bindings/BindingBuilder.cs
==============================================================================
--- experiments/ninject2/src/Ninject/Planning/Bindings/BindingBuilder.cs
(original)
+++ experiments/ninject2/src/Ninject/Planning/Bindings/BindingBuilder.cs
Mon Feb 23 20:27:09 2009
@@ -55,6 +55,8 @@
public IBindingWhenInNamedWithOrOnSyntax<T> ToSelf()
{
Binding.ProviderCallback =
StandardProvider.GetCreationCallback(Binding.Service);
+ Binding.Target = BindingTarget.Self;
+
return this;
}
@@ -66,6 +68,8 @@
where TImplementation : T
{
Binding.ProviderCallback =
StandardProvider.GetCreationCallback(typeof(TImplementation));
+ Binding.Target = BindingTarget.Type;
+
return this;
}
@@ -76,6 +80,8 @@
public IBindingWhenInNamedWithOrOnSyntax<T> To(Type implementation)
{
Binding.ProviderCallback =
StandardProvider.GetCreationCallback(implementation);
+ Binding.Target = BindingTarget.Type;
+
return this;
}
@@ -88,6 +94,8 @@
where TProvider : IProvider
{
Binding.ProviderCallback = ctx => ctx.Kernel.Get<TProvider>();
+ Binding.Target = BindingTarget.Provider;
+
return this;
}
@@ -98,6 +106,8 @@
public IBindingWhenInNamedWithOrOnSyntax<T> ToProvider(IProvider
provider)
{
Binding.ProviderCallback = ctx => provider;
+ Binding.Target = BindingTarget.Provider;
+
return this;
}
@@ -108,6 +118,8 @@
public IBindingWhenInNamedWithOrOnSyntax<T> ToMethod(Func<IContext, T>
method)
{
Binding.ProviderCallback = ctx => new CallbackProvider<T>(method);
+ Binding.Target = BindingTarget.Method;
+
return this;
}
@@ -118,6 +130,8 @@
public IBindingWhenInNamedWithOrOnSyntax<T> ToConstant(T value)
{
Binding.ProviderCallback = ctx => new ConstantProvider<T>(value);
+ Binding.Target = BindingTarget.Constant;
+
return this;
}
Modified:
experiments/ninject2/src/Ninject/Planning/Bindings/BindingMetadata.cs
==============================================================================
--- experiments/ninject2/src/Ninject/Planning/Bindings/BindingMetadata.cs
(original)
+++ experiments/ninject2/src/Ninject/Planning/Bindings/BindingMetadata.cs
Mon Feb 23 20:27:09 2009
@@ -35,11 +35,6 @@
public string Name { get; set; }
/// <summary>
- /// Gets or sets a value indicating whether the binding was implicitly
registered.
- /// </summary>
- public bool IsImplicit { get; set; }
-
- /// <summary>
/// Determines whether a piece of metadata with the specified key has
been defined.
/// </summary>
/// <param name="key">The metadata key.</param>
Copied: experiments/ninject2/src/Ninject/Planning/Bindings/BindingTarget.cs
(from r146, /experiments/ninject2/src/Ninject/Planning/Bindings/Binding.cs)
==============================================================================
--- /experiments/ninject2/src/Ninject/Planning/Bindings/Binding.cs
(original)
+++ experiments/ninject2/src/Ninject/Planning/Bindings/BindingTarget.cs Mon
Feb 23 20:27:09 2009
@@ -24,98 +24,33 @@
namespace Ninject.Planning.Bindings
{
/// <summary>
- /// Contains information about a service registration.
+ /// Describes the target of a binding.
/// </summary>
- public class Binding : IBinding
+ public enum BindingTarget
{
/// <summary>
- /// Gets the service type that is controlled by the binding.
+ /// Indicates that the binding is from a type to itself.
/// </summary>
- public Type Service { get; private set; }
+ Self,
/// <summary>
- /// Gets the binding's metadata.
+ /// Indicates that the binding is from one type to another.
/// </summary>
- public IBindingMetadata Metadata { get; private set; }
+ Type,
/// <summary>
- /// Gets or sets the condition defined for the binding.
+ /// Indicates that the binding is from a type to a provider.
/// </summary>
- public Func<IRequest, bool> Condition { get; set; }
+ Provider,
/// <summary>
- /// Gets the parameters defined for the binding.
+ /// Indicates that the binding is from a type to a callback method.
/// </summary>
- public ICollection<IParameter> Parameters { get; private set; }
+ Method,
/// <summary>
- /// Gets the actions that should be called after instances are activated
via the binding.
+ /// Indicates that the binding is from a type to a constant value.
/// </summary>
- public ICollection<Action<IContext>> ActivationActions { get; private
set; }
-
- /// <summary>
- /// Gets the actions that should be called before instances are
deactivated via the binding.
- /// </summary>
- public ICollection<Action<IContext>> DeactivationActions { get; private
set; }
-
- /// <summary>
- /// Gets or sets the callback that returns the provider that should be
used by the binding.
- /// </summary>
- public Func<IContext, IProvider> ProviderCallback { get; set; }
-
- /// <summary>
- /// Gets or sets the callback that returns the object that will act as
the binding's scope.
- /// </summary>
- public Func<IContext, object> ScopeCallback { get; set; }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="Binding"/> class.
- /// </summary>
- /// <param name="service">The service that is controlled by the
binding.</param>
- public Binding(Type service) : this(service, new BindingMetadata()) { }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="Binding"/> class.
- /// </summary>
- /// <param name="service">The service that is controlled by the
binding.</param>
- /// <param name="metadata">The binding's metadata container.</param>
- public Binding(Type service, IBindingMetadata metadata)
- {
- Service = service;
- Metadata = metadata;
- Parameters = new List<IParameter>();
- ActivationActions = new List<Action<IContext>>();
- DeactivationActions = new List<Action<IContext>>();
- }
-
- /// <summary>
- /// Gets the provider for the binding.
- /// </summary>
- /// <param name="context">The context.</param>
- /// <returns>The provider to use.</returns>
- public IProvider GetProvider(IContext context)
- {
- return ProviderCallback(context);
- }
-
- /// <summary>
- /// Gets the scope for the binding, if any.
- /// </summary>
- /// <param name="context">The context.</param>
- /// <returns>The object that will act as the scope, or <see
langword="null"/> if the service is transient.</returns>
- public object GetScope(IContext context)
- {
- return ScopeCallback == null ? null : ScopeCallback(context);
- }
-
- /// <summary>
- /// Determines whether the specified request satisfies the conditions
defined on this binding.
- /// </summary>
- /// <param name="request">The request.</param>
- /// <returns><c>True</c> if the request satisfies the conditions;
otherwise <c>false</c>.</returns>
- public bool Matches(IRequest request)
- {
- return Condition == null || Condition(request);
- }
+ Constant
}
}
Modified: experiments/ninject2/src/Ninject/Planning/Bindings/IBinding.cs
==============================================================================
--- experiments/ninject2/src/Ninject/Planning/Bindings/IBinding.cs
(original)
+++ experiments/ninject2/src/Ninject/Planning/Bindings/IBinding.cs Mon Feb
23 20:27:09 2009
@@ -39,6 +39,16 @@
IBindingMetadata Metadata { get; }
/// <summary>
+ /// Gets or sets the type of target for the binding.
+ /// </summary>
+ BindingTarget Target { get; set; }
+
+ /// <summary>
+ /// Gets or sets a value indicating whether the binding was implicitly
registered.
+ /// </summary>
+ bool IsImplicit { get; set; }
+
+ /// <summary>
/// Gets or sets the condition defined for the binding.
/// </summary>
Func<IRequest, bool> Condition { get; set; }
Modified:
experiments/ninject2/src/Ninject/Planning/Bindings/IBindingMetadata.cs
==============================================================================
--- experiments/ninject2/src/Ninject/Planning/Bindings/IBindingMetadata.cs
(original)
+++ experiments/ninject2/src/Ninject/Planning/Bindings/IBindingMetadata.cs
Mon Feb 23 20:27:09 2009
@@ -32,11 +32,6 @@
string Name { get; set; }
/// <summary>
- /// Gets or sets a value indicating whether the binding was implicitly
registered.
- /// </summary>
- bool IsImplicit { get; set; }
-
- /// <summary>
/// Determines whether a piece of metadata with the specified key has
been defined.
/// </summary>
/// <param name="key">The metadata key.</param>
Modified: experiments/ninject2/src/Ninject/Syntax/IResolutionRoot.cs
==============================================================================
--- experiments/ninject2/src/Ninject/Syntax/IResolutionRoot.cs (original)
+++ experiments/ninject2/src/Ninject/Syntax/IResolutionRoot.cs Mon Feb 23
20:27:09 2009
@@ -18,7 +18,6 @@
using System;
using System.Collections.Generic;
using Ninject.Activation;
-using Ninject.Activation.Hooks;
using Ninject.Parameters;
using Ninject.Planning.Bindings;
#endregion
@@ -45,13 +44,13 @@
/// <param name="parameters">The parameters to pass to the
resolution.</param>
/// <param name="isOptional"><c>True</c> if the request is optional;
otherwise, <c>false</c>.</param>
/// <returns>A series of hooks that can be used to resolve instances
that match the request.</returns>
- IEnumerable<IHook> Resolve(Type service, Func<IBindingMetadata, bool>
constraint, IEnumerable<IParameter> parameters, bool isOptional);
+ IEnumerable<Hook> Resolve(Type service, Func<IBindingMetadata, bool>
constraint, IEnumerable<IParameter> parameters, bool isOptional);
/// <summary>
/// Resolves the specified request.
/// </summary>
/// <param name="request">The request to resolve.</param>
/// <returns>A series of hooks that can be used to resolve instances
that match the request.</returns>
- IEnumerable<IHook> Resolve(IRequest request);
+ IEnumerable<Hook> Resolve(IRequest request);
}
}
Modified: experiments/ninject2/src/Ninject/Syntax/ResolutionExtensions.cs
==============================================================================
--- experiments/ninject2/src/Ninject/Syntax/ResolutionExtensions.cs
(original)
+++ experiments/ninject2/src/Ninject/Syntax/ResolutionExtensions.cs Mon Feb
23 20:27:09 2009
@@ -69,6 +69,44 @@
}
/// <summary>
+ /// Tries to get an instance of the specified service.
+ /// </summary>
+ /// <typeparam name="T">The service to resolve.</typeparam>
+ /// <param name="root">The resolution root.</param>
+ /// <param name="parameters">The parameters to pass to the
request.</param>
+ /// <returns>An instance of the service, or <see langword="null"/> if no
implementation was available.</returns>
+ public static T TryGet<T>(this IResolutionRoot root, params IParameter[]
parameters)
+ {
+ return (T)root.TryGet(typeof(T), parameters);
+ }
+
+ /// <summary>
+ /// Tries to get an instance of the specified service by using the first
binding with the specified name.
+ /// </summary>
+ /// <typeparam name="T">The service to resolve.</typeparam>
+ /// <param name="root">The resolution root.</param>
+ /// <param name="name">The name of the binding.</param>
+ /// <param name="parameters">The parameters to pass to the
request.</param>
+ /// <returns>An instance of the service, or <see langword="null"/> if no
implementation was available.</returns>
+ public static T TryGet<T>(this IResolutionRoot root, string name, params
IParameter[] parameters)
+ {
+ return (T)root.TryGet(typeof(T), m => m.Name == name, parameters);
+ }
+
+ /// <summary>
+ /// Tries to get an instance of the specified service by using the first
binding that matches the specified constraint.
+ /// </summary>
+ /// <typeparam name="T">The service to resolve.</typeparam>
+ /// <param name="root">The resolution root.</param>
+ /// <param name="constraint">The constraint to apply to the
binding.</param>
+ /// <param name="parameters">The parameters to pass to the
request.</param>
+ /// <returns>An instance of the service, or <see langword="null"/> if no
implementation was available.</returns>
+ public static T TryGet<T>(this IResolutionRoot root,
Func<IBindingMetadata, bool> constraint, params IParameter[] parameters)
+ {
+ return (T)root.TryGet(typeof(T), constraint, parameters);
+ }
+
+ /// <summary>
/// Gets all available instances of the specified service.
/// </summary>
/// <typeparam name="T">The service to resolve.</typeparam>
@@ -142,6 +180,44 @@
public static object Get(this IResolutionRoot root, Type service,
Func<IBindingMetadata, bool> constraint, params IParameter[] parameters)
{
return root.GetAll(service, constraint, parameters).FirstOrDefault();
+ }
+
+ /// <summary>
+ /// Tries to get an instance of the specified service.
+ /// </summary>
+ /// <param name="root">The resolution root.</param>
+ /// <param name="service">The service to resolve.</param>
+ /// <param name="parameters">The parameters to pass to the
request.</param>
+ /// <returns>An instance of the service, or <see langword="null"/> if no
implementation was available.</returns>
+ public static object TryGet(this IResolutionRoot root, Type service,
params IParameter[] parameters)
+ {
+ return root.Resolve(service, null, parameters, true).Select(hook =>
hook.Resolve()).FirstOrDefault();
+ }
+
+ /// <summary>
+ /// Tries to get an instance of the specified service by using the first
binding with the specified name.
+ /// </summary>
+ /// <param name="root">The resolution root.</param>
+ /// <param name="service">The service to resolve.</param>
+ /// <param name="name">The name of the binding.</param>
+ /// <param name="parameters">The parameters to pass to the
request.</param>
+ /// <returns>An instance of the service, or <see langword="null"/> if no
implementation was available.</returns>
+ public static object TryGet(this IResolutionRoot root, Type service,
string name, params IParameter[] parameters)
+ {
+ return root.Resolve(service, m => m.Name == name, parameters,
true).Select(hook => hook.Resolve()).FirstOrDefault();
+ }
+
+ /// <summary>
+ /// Tries to get an instance of the specified service by using the first
binding that matches the specified constraint.
+ /// </summary>
+ /// <param name="root">The resolution root.</param>
+ /// <param name="service">The service to resolve.</param>
+ /// <param name="constraint">The constraint to apply to the
binding.</param>
+ /// <param name="parameters">The parameters to pass to the
request.</param>
+ /// <returns>An instance of the service, or <see langword="null"/> if no
implementation was available.</returns>
+ public static object TryGet(this IResolutionRoot root, Type service,
Func<IBindingMetadata, bool> constraint, params IParameter[] parameters)
+ {
+ return root.Resolve(service, constraint, parameters, true).Select(hook
=> hook.Resolve()).FirstOrDefault();
}
/// <summary>