예제 #1
0
        public static BusinessRule <T, object> AddRuleWithParam <T>(this T item, string formula)
            where T : EntityBase <T>
        {
            var index    = formula.IndexOf("=");
            var left     = formula.Substring(0, index);
            var right    = formula.Substring(index + 1);
            var compiler = new FormulaCompiler()
                           .WithType <T>().WithType <User>();

            var get = compiler.NewLambda()
                      .WithThis <T>()
                      .Returns <object>()
                      .CompileToExpression(left);

            var func = compiler.NewLambda()
                       .WithThis <T>()
                       .WithParam <User>("User")
                       .Returns <object>()
                       .CompileToExpression(right);
            var f = func.Compile();
            var r = new RuleFactory <T>();

            var rule = r.CreateBusinessRule(e => f(e, User.Current), get)
                       .WithModeAtStartup(BusinessRuleStartupMode.None)
                       .AnalyzeDependencies(func);

            item.Extensions.Rules.Add(rule);
            return(rule);
        }
예제 #2
0
        //[ExpectedException(typeof(ParameterNotFoundException))]
        public void NonExistingParam()
        {
            var compiler = new FormulaCompiler();

            compiler.NewLambda()
            .Returns <int>()
            .Compile("@Param");

            compiler.BuildInfo.Errors.Count().ShouldBeGreaterThan(0);
        }
예제 #3
0
        public void Linq()
        {
            var compiler = new FormulaCompiler()
                           .WithInfo(FormulaCompilerBuildInfoLevels.None)
                           .WithLinq();
            var lambda = compiler.NewLambda()
                         .WithParam <IEnumerable <int> >("input")
                         .Returns <int>();

            TestTime.Measure(1000, () => lambda.Compile("input.Max()"));
        }
예제 #4
0
        //[ExpectedException(typeof(MethodNotFoundException))]
        public void SecondLevelParam()
        {
            var compiler = new FormulaCompiler();

            compiler.NewLambda()
            .WithParam <int>("Param")
            .Returns <int>()
            .Compile("@Param.@abc");

            compiler.BuildInfo.Errors.Count().ShouldBeGreaterThan(0);
        }
예제 #5
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _func = null;

            var compiler = new FormulaCompiler();

            if (Custom.IsChecked ?? false)
            {
                compiler.
                WithMethod("rand", () => new Random(Environment.TickCount).Next())
                .WithMethod("rand", (int max) => new Random(Environment.TickCount).Next(max))
                .WithMethod("p", (int p) => 5);
            }
            if (Math.IsChecked ?? false)
            {
                compiler.WithMath();
            }
            if (Date.IsChecked ?? false)
            {
                compiler.WithDate();
            }
            if (All.IsChecked ?? false)
            {
                var a = new AssemblyCallProvider()
                        .AddAssembly("System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
                        .AddAssembly("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
                        .AddAssembly("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
                        .Using(Using.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
                        .WithExtensionMethods();
                compiler.With(a);
            }

            if (SampleEntity.IsChecked ?? false)
            {
                compiler.WithType <SampleEntity>();
                var f = compiler.NewLambda()
                        .WithParam <SampleEntity>("s")
                        .Returns <object>()
                        .Compile(Formula.Text);
                if (f != null)
                {
                    _func = () => f(new SampleEntity());
                }
            }
            else
            {
                _func = compiler.Compile <object>(Formula.Text);
            }

            Status.Text = compiler.BuildInfo.Message;

            Button_Click_1(null, null);
        }
예제 #6
0
        public void ParamTest()
        {
            var compiler = new FormulaCompiler();

            var f = compiler.NewLambda().
                    WithParam <int>("Param1").
                    WithParam <int>("Param2").
                    Returns <int>().
                    Compile("Param1 * Param2");

            f(2, 3).ShouldEqual(6);
        }
예제 #7
0
        public void Where()
        {
            var compiler = new FormulaCompiler().WithLinq();

            var f = compiler.NewLambda()
                    .WithParam <IEnumerable <int> >("data")
                    .Returns <double>()
                    .Compile("data.Where((int e)=> e < 5).Where((int x)=>x>2).Average()");

            f.ShouldNotBeNull();
            f(Enumerable.Range(0, 10)).ShouldEqual(3.5d);
        }
예제 #8
0
        public void RootCallThisPropertyTest()
        {
            var param = new This {
                CurrentUserProperty = "michael"
            };
            var f = _compiler.NewLambda()
                    .WithThis <This>()
                    .Returns <string>()
                    .Compile("CurrentUserProperty");

            f(param).ShouldEqual("michael");
        }