Skip to content

Commit

Permalink
Added BindIFactory methods
Browse files Browse the repository at this point in the history
  • Loading branch information
svermeulen committed Apr 30, 2016
1 parent 118bbec commit f99e188
Show file tree
Hide file tree
Showing 19 changed files with 241 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System;
using System.Collections.Generic;
using Zenject;
using NUnit.Framework;
using System.Linq;
using ModestTree;
using Assert=ModestTree.Assert;

namespace Zenject.Tests.Bindings
{
[TestFixture]
public class TestIFactory : TestWithContainer
{
[Test]
public void Test1()
{
Container.BindIFactory<Foo>();

var factory = Container.Resolve<IFactory<Foo>>();

Assert.IsNotNull(factory.Create());
}

[Test]
public void Test2Error()
{
Container.BindIFactory<FooTwo>();

Assert.Throws(() => Container.Validate());

var factory = Container.Resolve<IFactory<FooTwo>>();

Assert.Throws(() => factory.Create());
}

[Test]
public void Test2()
{
Container.BindIFactory<string, FooTwo>();

Container.Validate();

var factory = Container.Resolve<IFactory<string, FooTwo>>();

Assert.IsEqual(factory.Create("asdf").Value, "asdf");
}

[Test]
public void Test5()
{
Container.BindIFactory<string, int, char, long, double, IFooFive>().To<FooFive>();

Container.Validate();

var factory = Container.Resolve<IFactory<string, int, char, long, double, IFooFive>>();

Assert.IsEqual(factory.Create("asdf", 0, 'z', 2, 3.0).P1, "asdf");
}

public class Foo
{
}

public class FooTwo
{
public FooTwo(string value)
{
Value = value;
}

public string Value
{
get;
private set;
}
}

public interface IFooFive
{
string P1
{
get;
}
}

public class FooFive : IFooFive
{
string _p1;
public FooFive(string p1, int p2, char p3, long p4, double p5)
{
_p1 = p1;
}

public string P1
{
get
{
return _p1;
}
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ namespace Zenject
public class FactoryFromBinder<TContract> : FactoryFromBinderBase<TContract>
{
public FactoryFromBinder(
BindInfo bindInfo, BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, finalizerWrapper)
BindInfo bindInfo,
Type factoryType,
BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, factoryType, finalizerWrapper)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ namespace Zenject
{
public class FactoryFromBinder<TParam1, TContract> : FactoryFromBinderWithParams<TContract>
{
public FactoryFromBinder(BindInfo bindInfo, BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, finalizerWrapper)
public FactoryFromBinder(
BindInfo bindInfo,
Type factoryType,
BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, factoryType, finalizerWrapper)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Zenject
{
public class FactoryFromBinder<TParam1, TParam2, TContract> : FactoryFromBinderWithParams<TContract>
{
public FactoryFromBinder(BindInfo bindInfo, BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, finalizerWrapper)
public FactoryFromBinder(BindInfo bindInfo, Type factoryType, BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, factoryType, finalizerWrapper)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Zenject
{
public class FactoryFromBinder<TParam1, TParam2, TParam3, TContract> : FactoryFromBinderWithParams<TContract>
{
public FactoryFromBinder(BindInfo bindInfo, BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, finalizerWrapper)
public FactoryFromBinder(BindInfo bindInfo, Type factoryType, BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, factoryType, finalizerWrapper)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Zenject
{
public class FactoryFromBinder<TParam1, TParam2, TParam3, TParam4, TContract> : FactoryFromBinderWithParams<TContract>
{
public FactoryFromBinder(BindInfo bindInfo, BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, finalizerWrapper)
public FactoryFromBinder(BindInfo bindInfo, Type factoryType, BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, factoryType, finalizerWrapper)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace Zenject
{
public class FactoryFromBinder<TParam1, TParam2, TParam3, TParam4, TParam5, TContract> : FactoryFromBinderWithParams<TContract>
{
public FactoryFromBinder(BindInfo bindInfo, BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, finalizerWrapper)
public FactoryFromBinder(BindInfo bindInfo, Type factoryType, BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, factoryType, finalizerWrapper)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ public class FactoryFromBinderBase<TContract> : ConditionBinder
{
public FactoryFromBinderBase(
BindInfo bindInfo,
Type factoryType,
BindFinalizerWrapper finalizerWrapper)
: base(bindInfo)
{
var factoryType = bindInfo.ContractTypes.Single();

// Note that it doesn't derive from Factory<TContract>
// when used with To<>, so we can only check IDynamicFactory
Assert.That(factoryType.DerivesFrom<IDynamicFactory>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace Zenject
public class FactoryFromBinderWithParams<TContract> : FactoryFromBinderBase<TContract>
{
public FactoryFromBinderWithParams(
BindInfo bindInfo, BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, finalizerWrapper)
BindInfo bindInfo, Type factoryType, BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, factoryType, finalizerWrapper)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace Zenject
public class FactoryToChoiceBinder<TContract> : FactoryFromBinder<TContract>
{
public FactoryToChoiceBinder(
BindInfo bindInfo,
BindInfo bindInfo, Type factoryType,
BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, finalizerWrapper)
: base(bindInfo, factoryType, finalizerWrapper)
{
}

Expand All @@ -35,7 +35,7 @@ public FactoryFromBinder<TConcrete> To<TConcrete>()
};

return new FactoryFromBinder<TConcrete>(
BindInfo, FinalizerWrapper);
BindInfo, FactoryType, FinalizerWrapper);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace Zenject
public class FactoryToChoiceBinder<TParam1, TContract> : FactoryFromBinder<TParam1, TContract>
{
public FactoryToChoiceBinder(
BindInfo bindInfo,
BindInfo bindInfo, Type factoryType,
BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, finalizerWrapper)
: base(bindInfo, factoryType, finalizerWrapper)
{
}

Expand All @@ -35,7 +35,7 @@ public FactoryFromBinder<TParam1, TConcrete> To<TConcrete>()
};

return new FactoryFromBinder<TParam1, TConcrete>(
BindInfo, FinalizerWrapper);
BindInfo, FactoryType, FinalizerWrapper);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace Zenject
public class FactoryToChoiceBinder<TParam1, TParam2, TContract> : FactoryFromBinder<TParam1, TParam2, TContract>
{
public FactoryToChoiceBinder(
BindInfo bindInfo,
BindInfo bindInfo, Type factoryType,
BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, finalizerWrapper)
: base(bindInfo, factoryType, finalizerWrapper)
{
}

Expand All @@ -35,7 +35,7 @@ public FactoryFromBinder<TParam1, TParam2, TConcrete> To<TConcrete>()
};

return new FactoryFromBinder<TParam1, TParam2, TConcrete>(
BindInfo, FinalizerWrapper);
BindInfo, FactoryType, FinalizerWrapper);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace Zenject
public class FactoryToChoiceBinder<TParam1, TParam2, TParam3, TContract> : FactoryFromBinder<TParam1, TParam2, TParam3, TContract>
{
public FactoryToChoiceBinder(
BindInfo bindInfo,
BindInfo bindInfo, Type factoryType,
BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, finalizerWrapper)
: base(bindInfo, factoryType, finalizerWrapper)
{
}

Expand All @@ -35,7 +35,7 @@ public FactoryFromBinder<TParam1, TParam2, TParam3, TConcrete> To<TConcrete>()
};

return new FactoryFromBinder<TParam1, TParam2, TParam3, TConcrete>(
BindInfo, FinalizerWrapper);
BindInfo, FactoryType, FinalizerWrapper);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace Zenject
public class FactoryToChoiceBinder<TParam1, TParam2, TParam3, TParam4, TContract> : FactoryFromBinder<TParam1, TParam2, TParam3, TParam4, TContract>
{
public FactoryToChoiceBinder(
BindInfo bindInfo,
BindInfo bindInfo, Type factoryType,
BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, finalizerWrapper)
: base(bindInfo, factoryType, finalizerWrapper)
{
}

Expand All @@ -35,7 +35,7 @@ public FactoryFromBinder<TParam1, TParam2, TParam3, TParam4, TConcrete> To<TConc
};

return new FactoryFromBinder<TParam1, TParam2, TParam3, TParam4, TConcrete>(
BindInfo, FinalizerWrapper);
BindInfo, FactoryType, FinalizerWrapper);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace Zenject
public class FactoryToChoiceBinder<TParam1, TParam2, TParam3, TParam4, TParam5, TContract> : FactoryFromBinder<TParam1, TParam2, TParam3, TParam4, TParam5, TContract>
{
public FactoryToChoiceBinder(
BindInfo bindInfo,
BindInfo bindInfo, Type factoryType,
BindFinalizerWrapper finalizerWrapper)
: base(bindInfo, finalizerWrapper)
: base(bindInfo, factoryType, finalizerWrapper)
{
}

Expand All @@ -35,7 +35,7 @@ public FactoryFromBinder<TParam1, TParam2, TParam3, TParam4, TParam5, TConcrete>
};

return new FactoryFromBinder<TParam1, TParam2, TParam3, TParam4, TParam5, TConcrete>(
BindInfo, FinalizerWrapper);
BindInfo, FactoryType, FinalizerWrapper);
}
}
}
Expand Down
Loading

0 comments on commit f99e188

Please sign in to comment.