public void Test_Constructor_Throws_When_Given_Null_Construction_Expression()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         LazyConstructorBinding <object> binding = new LazyConstructorBinding <object>(new IBinding[0], null);
     });
 }
 public void Test_Constructor_Throws_When_Given_Null_Dependencies()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         LazyConstructorBinding <object> binding = new LazyConstructorBinding <object>(null, bindings => new object());
     });
 }
        public void Test_Resolve_Implementation_Builds_Expression()
        {
            var lazyBinding = new LazyConstructorBinding <ISample>(new IBinding[0], bindings => new Sample());

            Assert.False(lazyBinding.IsBuilt);

            ISample sample = lazyBinding.Resolve();

            Assert.IsType <Sample>(sample);
            Assert.True(lazyBinding.IsBuilt);
        }
        public void Test_Resolve_Implementation_Handles_Non_Lazy_Bindings_Correctly()
        {
            IBinding <ISample> binding = new ConstructorBinding <ISample>(() => new Sample());
            LazyConstructorBinding <IHasSample> lazyBinding = new LazyConstructorBinding <IHasSample>(
                new IBinding[0],
                bindings => new HasSample(binding.Resolve()));

            IHasSample sample = lazyBinding.Resolve();

            Assert.IsType <HasSample>(sample);
            Assert.IsType <ConstructorBinding <IHasSample> >(lazyBinding.Constructor);
        }
        public void Test_Resolve_Implementation_Flattens_Multiple_Lazy_Bindings()
        {
            LazyConstructorBinding <IHasSample> lazyBinding = new LazyConstructorBinding <IHasSample>(new IBinding[]
            {
                new LazyConstructorBinding <ISample>(new IBinding[0], bindings => new Sample())
            }, bindings => new HasSample(((LazyConstructorBinding <ISample>)bindings[0]).Resolve()));

            IHasSample sample = lazyBinding.Resolve();

            Assert.IsType <HasSample>(sample);
            Assert.IsType <ConstructorBinding <IHasSample> >(lazyBinding.Constructor);
        }
        public void Test_Resolve_Implementation_Handles_Binding_Inside_Initializer()
        {
            IBinding <ISample> binding = new LazyConstructorBinding <ISample>(new IBinding[]
            {
                new LazyConstructorBinding <object>(new IBinding[0], bindings => new Sample())
            }, bindings => new Sample
            {
                Obj = ((ILazyConstructorBinding <object>)bindings[0]).Resolve()
            });

            ISample sample = binding.Resolve();

            Assert.IsType <Sample>(sample);
            Assert.IsType <Sample>(((Sample)sample).Obj);
        }
Beispiel #7
0
        /// <summary>
        /// Finishes the binding between the given <see cref="IBinding{TInterface}"/> and the type resolved by the given expression.
        /// </summary>
        /// <param name="binding">The binding that should be finished.</param>
        /// <param name="expression">
        /// The expression that specifies how the value is retrieved.
        /// This is generally a <see cref="NewExpression"/>, however anything that can be translated into a <see cref="Func{TImplementer}"/> can be used.
        /// </param>
        /// <example>
        /// Declare a binding to a constructor-like function:
        ///
        /// <code>
        /// IBinding&lt;TInterface&gt; finalBinding = b.Select(type => new TImplementer());
        /// IBinding&lt;TInterface&gt; finalBinding = from type in b select new TImplementer();
        /// </code>
        /// </example>
        /// <returns>
        /// Returns a new <see cref="IBinding{TInterface}"/> that is able to resolve a value that implements the specified type when <see cref="IBinding{TInterface}.Resolve"/> is called.
        /// </returns>
        /// <exception cref="ArgumentException">Invalid Expression. The given expression must be a NewExpression. That is, the calling of a constructor.</exception>
        public static IBinding <TInterface> Select <TInterface, TImplementer>(this IBinding <TInterface> binding, [NotNull] Expression <Func <TInterface, TImplementer> > expression)
            where TImplementer : TInterface
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }
            Expression constructorExpression = expression.Body;

            IBinding[] dependencies;
            Expression <Func <IBinding[], TInterface> > lazyExpression = constructorExpression.BuildLazyBindingExpression <TInterface>(out dependencies);

            LazyConstructorBinding <TInterface> b = new LazyConstructorBinding <TInterface>(dependencies, lazyExpression);

            return(Wrap(binding, b));
        }