예제 #1
0
        public void OneArgumentBinding()
        {
            IBindingContext context = TestsFactory.BindingContext();

            context.Bind <int>().With <string>().To((strParam) => 45);

            context.Bind <string>().To(() => "uhul");

            int ret = context.Get <int>();

            Assert.AreEqual(ret, 45);
        }
예제 #2
0
        public void OneCorrectArgumentBinding()
        {
            IBindingContext context = TestsFactory.BindingContext();

            string parameter = "";

            context.Bind <int>().With <string>().To((value) => { parameter = value; return(45); });

            context.Bind <string>().To(() => "uhul");

            context.Get <int>();

            Assert.AreEqual(parameter, "uhul");
        }
예제 #3
0
        public void OneArgumentNamedBinding()
        {
            IBindingContext context = TestsFactory.BindingContext();

            int expected = 45;

            context.Bind <int>().With <string>("MyText").To((value) => expected);

            context.Bind <string>("MyText").To(() => "uhul");

            int ret = context.Get <int>();

            Assert.AreEqual(ret, expected);
        }
예제 #4
0
        public void TwoArgumentBinding()
        {
            IBindingContext context = TestsFactory.BindingContext();

            context.Bind <int>().With <string>().With <float>().To((str, flt) => 45);

            context.Bind <string>().To(() => "uhul");

            context.Bind <float>().To(() => 3.0f);

            int ret = context.Get <int>();

            Assert.AreEqual(ret, 45);
        }
예제 #5
0
        public void NamedBindingDifferentNameError()
        {
            IBindingContext context = TestsFactory.BindingContext();

            context.Bind <int>("foo").To(() => 45);

            Assert.Throws <BindingNotFound>(() => context.Get <int>("notFoo"));
        }
예제 #6
0
        public void BindingSimpleInt()
        {
            IBindingContext context = TestsFactory.BindingContext();

            int expected = 45;

            context.Bind <int>().To(() => expected);

            Assert.AreEqual(context.Get <int>(), expected);
        }
예제 #7
0
        public void OneArgumentBindingError()
        {
            //Requires a string to get the int binging

            IBindingContext context = TestsFactory.BindingContext();

            context.Bind <int>().With <string>().To((value) => 45);

            Assert.Throws <BindingNotFound>(() => context.Get <int>());
        }
예제 #8
0
        public static SelectExpression BindSelect(PipelineExpression pipeline, IBindingContext bindingContext, LambdaExpression lambda)
        {
            bindingContext.AddExpressionMapping(lambda.Parameters[0], pipeline.Projector);

            var selector = bindingContext.Bind(lambda.Body);

            return new SelectExpression(
                pipeline.Source,
                lambda.Parameters[0].Name,
                selector);
        }
예제 #9
0
        public static SelectExpression BindSelect(PipelineExpression pipeline, IBindingContext bindingContext, LambdaExpression lambda)
        {
            bindingContext.AddExpressionMapping(lambda.Parameters[0], pipeline.Projector);

            var selector = bindingContext.Bind(lambda.Body);

            return(new SelectExpression(
                       pipeline.Source,
                       lambda.Parameters[0].Name,
                       selector));
        }
예제 #10
0
        public bool Bind(object instance, PropertyInfo propertyInfo, IBindingContext bindingContext)
        {
            using (bindingContext.OpenChildContext(string.Format("{0}_", propertyInfo.Name)))
            {
                var obj = bindingContext.Bind(propertyInfo.PropertyType);
                if (obj == null) return false;

                propertyInfo.SetValue(instance, obj, new object[0]);
                return true;
            }
        }
예제 #11
0
        public static WhereExpression BindWhere(PipelineExpression pipeline, IBindingContext bindingContext, LambdaExpression lambda)
        {
            bindingContext.AddExpressionMapping(lambda.Parameters[0], pipeline.Projector);

            var predicate = bindingContext.Bind(lambda.Body);

            return new WhereExpression(
                pipeline.Source,
                lambda.Parameters[0].Name,
                predicate);
        }
예제 #12
0
        public static WhereExpression BindWhere(PipelineExpression pipeline, IBindingContext bindingContext, LambdaExpression lambda)
        {
            bindingContext.AddExpressionMapping(lambda.Parameters[0], pipeline.Projector);

            var predicate = bindingContext.Bind(lambda.Body);

            return(new WhereExpression(
                       pipeline.Source,
                       lambda.Parameters[0].Name,
                       predicate));
        }
예제 #13
0
        public void NamedBindingInt()
        {
            IBindingContext context = TestsFactory.BindingContext();

            int    expected    = 45;
            string bindingName = "foo";

            context.Bind <int>(bindingName).To(() => expected);

            Assert.AreEqual(context.Get <int>(bindingName), expected);
        }
예제 #14
0
        public async Task <bool> Bind(object instance, PropertyInfo propertyInfo, IBindingContext bindingContext)
        {
            using (bindingContext.OpenChildContext($"{propertyInfo.Name}_"))
            {
                var result = await bindingContext.Bind(propertyInfo.PropertyType).ConfigureAwait(false);

                if (!result.Success)
                {
                    return(false);
                }

                propertyInfo.SetValue(instance, result.Instance, new object[0]);

                return(true);
            }
        }
예제 #15
0
        public async Task <bool> Bind(object instance, PropertyInfo propertyInfo, IBindingContext bindingContext)
        {
            var type     = propertyInfo.PropertyType;
            var itemType = type.GetGenericArguments()[0];

            if (type.IsInterface)
            {
                type = typeof(List <>).MakeGenericType(itemType);
            }

            var currentCollection = propertyInfo.GetValue(instance, null);
            var collection        = currentCollection ?? Activator.CreateInstance(type);
            var collectionType    = collection.GetType();

            Func <Type, string, Task <bool> > addToCollection = async(typeToBind, prefix) =>
            {
                using (bindingContext.OpenChildContext(prefix))
                {
                    var addMethod = AddMethods[collectionType];
                    var result    = await bindingContext.Bind(itemType).ConfigureAwait(false);

                    if (!result.Success)
                    {
                        return(false);
                    }

                    addMethod.Invoke(collection, new[] { result.Instance });

                    return(true);
                }
            };

            var formatString = string.Concat(propertyInfo.Name, "[{0}]_");

            var    index = 0;
            string currentPrefix;

            do
            {
                currentPrefix = string.Format(formatString, index);
                index++;
            } while (await addToCollection(itemType, currentPrefix).ConfigureAwait(false));

            propertyInfo.SetValue(instance, collection, null);

            return(((IEnumerable)collection).OfType <object>().Any());
        }
예제 #16
0
        public void UnsafePartialBinding()
        {
            IBindingContext     context     = TestsFactory.BindingContext();
            IBindingRequirement requirement = BindingRequirements.Instance.With <float>();

            context.Bind <float>().To(() => 0.1f);
            int extra = -1;

            System.Func <float, int, int> func = (bindinded, nonBinded) => { extra = nonBinded; return(45); };

            IBinding binding = new Binding(func, requirement);

            context.Unsafe.Bind(typeof(int)).To(binding);

            context.Get <int>(InnerBindingNames.Empty, 32);

            Assert.AreEqual(32, extra);
        }
예제 #17
0
        public bool Bind(object instance, PropertyInfo propertyInfo, IBindingContext bindingContext)
        {
            var type = propertyInfo.PropertyType;
            var itemType = type.GetGenericArguments()[0];
            if (type.IsInterface)
            {
                type = typeof(List<>).MakeGenericType(itemType);
            }

            var currentCollection = propertyInfo.GetValue(instance, null);
            var collection = currentCollection ?? Activator.CreateInstance(type);
            var collectionType = collection.GetType();

            Func<Type, string, bool> addToCollection = (typeToBind, prefix) =>
                                                           {
                                                               using (bindingContext.OpenChildContext(prefix))
                                                               {
                                                                   var addMethod = AddMethods[collectionType];
                                                                   var obj = bindingContext.Bind(itemType);
                                                                   if (obj != null)
                                                                   {
                                                                       addMethod.Invoke(collection, new[] { obj });
                                                                       return true;
                                                                   }
                                                                   return false;
                                                               }
                                                           };

            var formatString = string.Format("{0}{1}", bindingContext.GetPrefix(), propertyInfo.Name) + "[{0}]_";

            var index = 0;
            string currentPrefix;
            do
            {
                currentPrefix = formatString.ToFormat(index.ToString());
                index++;
            } while (addToCollection(itemType, currentPrefix));

            propertyInfo.SetValue(instance, collection, null);

            return ((IEnumerable) collection).OfType<object>().Count() > 0;
        }
예제 #18
0
        public static IBindingContext GetSubcontext(this IBindingContext me, params object[] names)
        {
            IBindingContext context = me;
            var             length  = names.Length;

            for (int i = 0; i < length; ++i)
            {
                var             name = names[i];
                IBindingContext currentContext;

                if (!context.TryGet <IBindingContext>(name, out currentContext))
                {
                    currentContext = BindingContext.Create();
                    currentContext.FallBack(context);
                    context.Bind <IBindingContext>(name).To(currentContext);
                }

                context = currentContext;
            }



            return(context);
        }
예제 #19
0
        public void RequireItselfWithSameNameError()
        {
            IBindingContext context = TestsFactory.BindingContext();

            Assert.Throws <BindingSelfRequirement>(() => context.Bind <int>("name").With <int>("name").To((value) => 45));
        }
예제 #20
0
        public void TwoRequireItseflWithSameNameError()
        {
            IBindingContext context = TestsFactory.BindingContext();

            Assert.Throws <BindingSelfRequirement>(() => context.Bind <int>().With <string>().With <int>().To((str, i) => 45));
        }
 static public IValueBindingContext <T> Bind <T> (this IBindingContext me, object name)
 {
     return(me.Bind <T>(new BindingName(name)));
 }
예제 #22
0
        public void TwoRequireItselfWithDifferentNameNoError2()
        {
            IBindingContext context = TestsFactory.BindingContext();

            Assert.DoesNotThrow(() => context.Bind <int>("name").With <int>().With <int>().To((i, j) => 45));
        }