TestFromIFactory2.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using NUnit.Framework;
  2. using Assert = ModestTree.Assert;
  3. namespace Zenject.Tests.Bindings
  4. {
  5. [TestFixture]
  6. public class TestFromIFactory2 : ZenjectUnitTestFixture
  7. {
  8. [Test]
  9. public void Test1()
  10. {
  11. Container.BindFactory<int, Foo, Foo.Factory>().WithId("foo1")
  12. .FromIFactory(x => x.To<FooFactory>().AsCached().WithArguments("asdf"));
  13. Container.BindFactory<int, Foo, Foo.Factory>().WithId("foo2")
  14. .FromIFactory(x => x.To<FooFactory>().AsCached().WithArguments("zxcv"));
  15. var factory1 = Container.ResolveId<Foo.Factory>("foo1");
  16. var factory2 = Container.ResolveId<Foo.Factory>("foo2");
  17. var foo1 = factory1.Create(5);
  18. var foo2 = factory2.Create(2);
  19. Assert.IsEqual(foo1.Value, "asdf");
  20. Assert.IsEqual(foo1.Value2, 5);
  21. Assert.IsEqual(foo2.Value, "zxcv");
  22. Assert.IsEqual(foo2.Value2, 2);
  23. }
  24. public class Foo
  25. {
  26. public Foo(string value, int value2)
  27. {
  28. Value = value;
  29. Value2 = value2;
  30. }
  31. public int Value2
  32. {
  33. get; private set;
  34. }
  35. public string Value
  36. {
  37. get; private set;
  38. }
  39. public class Factory : PlaceholderFactory<int, Foo>
  40. {
  41. }
  42. }
  43. public class FooFactory : IFactory<int, Foo>
  44. {
  45. readonly string _value;
  46. readonly DiContainer _container;
  47. public FooFactory(
  48. DiContainer container,
  49. string value)
  50. {
  51. _value = value;
  52. _container = container;
  53. }
  54. public Foo Create(int value)
  55. {
  56. return _container.Instantiate<Foo>(new object [] { value, _value });
  57. }
  58. }
  59. }
  60. }