Add Modules to StyletIoC

This commit is contained in:
Antony Male 2014-09-14 12:19:48 +01:00
parent eb9339b081
commit be22da44d0
5 changed files with 160 additions and 3 deletions

View File

@ -76,6 +76,7 @@
<Compile Include="StyletIoC\Internal\Registrations\TransientRegistration.cs" />
<Compile Include="StyletIoC\Internal\TypeKey.cs" />
<Compile Include="StyletIoC\StyletIoCContainer.cs" />
<Compile Include="StyletIoC\StyletIoCModule.cs" />
<Compile Include="Xaml\ApplicationLoader.cs" />
<Compile Include="ConductorNavigating.cs" />
<Compile Include="GlobalSuppressions.cs" />

View File

@ -140,12 +140,22 @@ namespace StyletIoC
public class StyletIoCBuilder : IStyletIoCBuilder
{
private readonly Container parent;
private List<BuilderBindTo> bindings = new List<BuilderBindTo>();
private readonly List<BuilderBindTo> bindings = new List<BuilderBindTo>();
private readonly List<StyletIoCModule> modules = new List<StyletIoCModule>();
/// <summary>
/// Create a new StyletIoC buidler instance
/// Create a new StyletIoC builder instance
/// </summary>
public StyletIoCBuilder() : this(null) { }
public StyletIoCBuilder() { }
/// <summary>
/// Create a new StyletIoC builder instanc, which contains the given modules
/// </summary>
/// <param name="modules">Modules to add to the builder</param>
public StyletIoCBuilder(params StyletIoCModule[] modules)
{
this.modules.AddRange(modules);
}
internal StyletIoCBuilder(Container parent)
{
@ -183,12 +193,35 @@ namespace StyletIoC
}
}
/// <summary>
/// Add a single module to this builder
/// </summary>
/// <param name="module">Module to add</param>
public void AddModule(StyletIoCModule module)
{
this.modules.Add(module);
}
/// <summary>
/// Add many modules to this builder
/// </summary>
/// <param name="modules">Modules to add</param>
public void AddModules(params StyletIoCModule[] modules)
{
this.modules.AddRange(modules);
}
/// <summary>
/// Once all bindings have been set, build an IContainer from which instances can be fetches
/// </summary>
/// <returns>An IContainer, which should be used from now on</returns>
public IContainer BuildContainer()
{
foreach (var module in this.modules)
{
module.AddToBuilder(this);
}
var container = this.parent == null ? new Container() : new Container(this.parent);
// For each TypeKey, we remove any weak bindings if there are any strong bindings
@ -200,6 +233,11 @@ namespace StyletIoC
}
return container;
}
internal void AddBinding(BuilderBindTo binding)
{
this.bindings.Add(binding);
}
}
/// <summary>

View File

@ -0,0 +1,54 @@
using StyletIoC.Internal.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StyletIoC
{
/// <summary>
/// Module which contains its own bindings, and can be added to a builder
/// </summary>
public abstract class StyletIoCModule
{
private readonly List<BuilderBindTo> bindings = new List<BuilderBindTo>();
/// <summary>
/// Bind the specified service (interface, abstract class, concrete class, unbound generic, etc) to something
/// </summary>
/// <param name="serviceType">Service to bind</param>
protected IBindTo Bind(Type serviceType)
{
var builderBindTo = new BuilderBindTo(serviceType);
this.bindings.Add(builderBindTo);
return builderBindTo;
}
/// <summary>
/// Bind the specified service (interface, abstract class, concrete class, unbound generic, etc) to something
/// </summary>
/// <typeparam name="TService">Service to bind</typeparam>
protected IBindTo Bind<TService>()
{
return this.Bind(typeof(TService));
}
/// <summary>
/// Override, and use 'Bind' to add bindings to the module
/// </summary>
protected abstract void Load();
internal void AddToBuilder(StyletIoCBuilder builder)
{
this.bindings.Clear();
this.Load();
foreach (var binding in this.bindings)
{
builder.AddBinding(binding);
}
}
}
}

View File

@ -0,0 +1,63 @@
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 StyletIoCModuleTests
{
class C1 { }
class C2 { }
class ModuleA : StyletIoCModule
{
protected override void Load()
{
Bind<C1>().ToSelf();
}
}
class ModuleB : StyletIoCModule
{
protected override void Load()
{
Bind<C2>().ToSelf();
}
}
[Test]
public void BuilderAddsBindingsFromModule()
{
var builder = new StyletIoCBuilder(new ModuleA());
var ioc = builder.BuildContainer();
Assert.IsInstanceOf<C1>(ioc.Get<C1>());
}
[Test]
public void BuilderAddsBindingsFromModuleAddedWithAddModule()
{
var builder = new StyletIoCBuilder();
builder.AddModule(new ModuleA());
var ioc = builder.BuildContainer();
Assert.IsInstanceOf<C1>(ioc.Get<C1>());
}
[Test]
public void BuilderAddsBindingsFromModulesAddedWithAddModules()
{
var builder = new StyletIoCBuilder();
builder.AddModules(new ModuleA(), new ModuleB());
var ioc = builder.BuildContainer();
Assert.IsInstanceOf<C1>(ioc.Get<C1>());
Assert.IsInstanceOf<C2>(ioc.Get<C2>());
}
}
}

View File

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