Add ability to register instances

This commit is contained in:
Antony Male 2014-09-12 13:11:39 +01:00
parent 4eeb114981
commit 05f509fd47
9 changed files with 111 additions and 9 deletions

View File

@ -55,6 +55,8 @@
<Compile Include="StyletIoC\Internal\Builders\BuilderFactoryBinding.cs" />
<Compile Include="StyletIoC\Internal\Builders\BuilderToAllImplementationsBinding.cs" />
<Compile Include="StyletIoC\Internal\Builders\BuilderTypeBinding.cs" />
<Compile Include="StyletIoC\Internal\Builders\InstanceBinding.cs" />
<Compile Include="StyletIoC\Internal\Creators\InstanceCreator.cs" />
<Compile Include="StyletIoC\Internal\IRegistrationCollection.cs" />
<Compile Include="StyletIoC\Internal\Creators\AbstractFactoryCreator.cs" />
<Compile Include="StyletIoC\Internal\Creators\CreatorBase.cs" />

View File

@ -35,7 +35,13 @@ namespace StyletIoC.Internal.Builders
return this.builderBinding;
}
public IWithKey ToAbstractFactory()
public IWithKeyOrAsWeakBinding ToInstance(object instance)
{
this.builderBinding = new InstanceBinding(this.ServiceType, instance);
return this.builderBinding;
}
public IWithKeyOrAsWeakBinding ToAbstractFactory()
{
this.builderBinding = new AbstractFactoryBinding(this.ServiceType);
return this.builderBinding;

View File

@ -6,7 +6,7 @@ using System.Reflection;
namespace StyletIoC.Internal.Builders
{
internal abstract class BuilderBindingBase : IInScopeOrWithKeyOrAsWeakBinding, IWithKey
internal abstract class BuilderBindingBase : IInScopeOrWithKeyOrAsWeakBinding, IWithKeyOrAsWeakBinding
{
protected Type serviceType;
protected RegistrationFactory registrationFactory;
@ -89,9 +89,10 @@ namespace StyletIoC.Internal.Builders
return this.registrationFactory(registrationContext, creator, this.Key);
}
void IWithKey.WithKey(string key)
IAsWeakBinding IWithKeyOrAsWeakBinding.WithKey(string key)
{
this.Key = key;
return this;
}
void IAsWeakBinding.AsWeakBinding()

View File

@ -6,7 +6,7 @@ namespace StyletIoC.Internal.Builders
{
internal class BuilderFactoryBinding<TImplementation> : BuilderBindingBase
{
private Func<IRegistrationContext, TImplementation> factory;
private readonly Func<IRegistrationContext, TImplementation> factory;
public BuilderFactoryBinding(Type serviceType, Func<IRegistrationContext, TImplementation> factory)
: base(serviceType)

View File

@ -0,0 +1,29 @@
using StyletIoC.Internal.Creators;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StyletIoC.Internal.Builders
{
internal class InstanceBinding : BuilderBindingBase
{
private readonly object instance;
public InstanceBinding(Type serviceType, object instance)
: base(serviceType)
{
this.EnsureType(instance.GetType());
this.instance = instance;
}
public override void Build(Container container)
{
var creator = new InstanceCreator(this.instance);
var registration = this.CreateRegistration(container, creator);
container.AddRegistration(new TypeKey(this.serviceType, this.Key), registration);
}
}
}

View File

@ -0,0 +1,27 @@
using StyletIoC.Creation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace StyletIoC.Internal.Creators
{
internal class InstanceCreator : ICreator
{
public Type Type { get; private set; }
private readonly Expression instanceExpression;
public InstanceCreator(object instance)
{
this.Type = instance.GetType();
this.instanceExpression = Expression.Constant(instance, this.Type);
}
public Expression GetInstanceExpression(ParameterExpression registrationContext)
{
return this.instanceExpression;
}
}
}

View File

@ -34,10 +34,16 @@ namespace StyletIoC
/// <param name="factory">Factory delegate to bind got</param>
IInScopeOrWithKeyOrAsWeakBinding ToFactory<TImplementation>(Func<IRegistrationContext, TImplementation> factory);
/// <summary>
/// Bind the specified service to the given untyped instance
/// </summary>
/// <param name="instance">Instance to use</param>
IWithKeyOrAsWeakBinding ToInstance(object instance);
/// <summary>
/// If the service is an interface with a number of methods which return other types, generate an implementation of that abstract factory and bind it to the interface.
/// </summary>
IWithKey ToAbstractFactory();
IWithKeyOrAsWeakBinding ToAbstractFactory();
/// <summary>
/// Discover all implementations of the service in the specified assemblies / the current assembly, and bind those to the service
@ -69,15 +75,15 @@ namespace StyletIoC
}
/// <summary>
/// Fluent interface on which WithKey can be called
/// Fluent interface on which WithKey or AsWeakBinding can be called
/// </summary>
public interface IWithKey
public interface IWithKeyOrAsWeakBinding : IAsWeakBinding
{
/// <summary>
/// Associate a key with this binding. Requests for the service will have to specify this key to retrieve the result of this binding
/// </summary>
/// <param name="key">Key to associate with this binding</param>
void WithKey(string key);
IAsWeakBinding WithKey(string key);
}
/// <summary>
@ -93,7 +99,7 @@ namespace StyletIoC
}
/// <summary>
/// Fluent interface on which WithKey, or the scoping extensions can be called
/// Fluent interface on which WithKey, AsWeakBinding, or the scoping extensions can be called
/// </summary>
public interface IInScopeOrWithKeyOrAsWeakBinding : IInScopeOrAsWeakBinding
{

View File

@ -0,0 +1,30 @@
using NUnit.Framework;
using StyletIoC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StyletUnitTests.StyletIoC
{
[TestFixture]
public class StyletIoCInstanceBindingTests
{
interface I1 { }
class C1 : I1 { }
[Test]
public void InstanceBindingUsesInstanceToResolve()
{
var c1 = new C1();
var builder = new StyletIoCBuilder();
builder.Bind<I1>().ToInstance(c1);
var ioc = builder.BuildContainer();
Assert.AreEqual(c1, ioc.Get<I1>());
Assert.AreEqual(c1, ioc.Get<I1>());
}
}
}

View File

@ -67,6 +67,7 @@
<Compile Include="DebugConverterTests.cs" />
<Compile Include="StyletIoC\StyletIoCChildContainerTests.cs" />
<Compile Include="StyletIoC\StyletIoCFuncFactoryTests.cs" />
<Compile Include="StyletIoC\StyletIoCInstanceBindingTests.cs" />
<Compile Include="TraceLoggerTests.cs" />
<Compile Include="EqualityConverterTests.cs" />
<Compile Include="EventActionTests.cs" />