Пример #1
0
        protected virtual ControllerConfiguration LoadConfig(string controllerName)
        {
            ControllerConfiguration config = null;

            if (!(_controllers.TryGetValue(controllerName, out config)))
            {
                config = DataControllerBase.CreateConfigurationInstance(GetType(), controllerName);
                _controllers[controllerName] = config;
            }
            return(config);
        }
 public void EnsureJsonCompatibility()
 {
     if (Values != null)
     {
         foreach (var v in Values)
         {
             if (v.Modified)
             {
                 v.DisableConversion();
                 v.NewValue = DataControllerBase.EnsureJsonCompatibility(v.NewValue);
                 v.EnableConversion();
             }
         }
     }
 }
        protected virtual void ExecuteRule(string ruleId)
        {
            var methods = GetType().GetMethods((BindingFlags.Public | (BindingFlags.NonPublic | BindingFlags.Instance)));

            foreach (var method in methods)
            {
                var ruleBindings = method.GetCustomAttributes(typeof(RuleAttribute), true);
                foreach (RuleAttribute ra in ruleBindings)
                {
                    if (ra.Id == ruleId)
                    {
                        BlockRule(ruleId);
                        var parameters = method.GetParameters();
                        var arguments  = new object[parameters.Length];
                        for (var i = 0; (i < parameters.Length); i++)
                        {
                            var p = parameters[i];
                            if ((parameters.Length == 1) && p.ParameterType.IsSubclassOf(typeof(BusinessRulesObjectModel)))
                            {
                                var self = p.ParameterType.Assembly.CreateInstance(p.ParameterType.FullName, true, BindingFlags.CreateInstance, null, new object[] {
                                    this
                                }, System.Globalization.CultureInfo.CurrentCulture, null);
                                var fields = self.GetType().GetFields((BindingFlags.Instance | BindingFlags.NonPublic));
                                foreach (var fi in fields)
                                {
                                    var index     = (fi.Name.IndexOf("_") + 1);
                                    var fieldName = fi.Name.Substring(index);
                                    if (fieldName.Length == 1)
                                    {
                                        fieldName = fieldName.ToUpper();
                                    }
                                    else
                                    {
                                        fieldName = (char.ToUpperInvariant(fieldName[0]) + fieldName.Substring(1));
                                    }
                                    var v = SelectFieldValueObject(fieldName);
                                    if (v != null)
                                    {
                                        try
                                        {
                                            self.GetType().InvokeMember(fi.Name, (BindingFlags.SetField | (BindingFlags.Instance | BindingFlags.NonPublic)), null, self, new object[] {
                                                DataControllerBase.ConvertToType(fi.FieldType, v.Value)
                                            });
                                        }
                                        finally
                                        {
                                            // release resources here
                                        }
                                    }
                                }
                                arguments[i] = self;
                            }
                            else
                            {
                                var v = SelectFieldValueObject(p.Name);
                                if (v != null)
                                {
                                    if (p.ParameterType.Equals(typeof(FieldValue)))
                                    {
                                        arguments[i] = v;
                                    }
                                    else
                                    {
                                        try
                                        {
                                            arguments[i] = DataControllerBase.ConvertToType(p.ParameterType, v.Value);
                                        }
                                        catch (Exception)
                                        {
                                        }
                                    }
                                }
                            }
                        }
                        method.Invoke(this, arguments);
                    }
                }
            }
        }
        private bool InternalExecuteMethod(ActionArgs args, ActionResult result, ActionPhase phase, bool viewMatch, bool argumentMatch)
        {
            _arguments = args;
            _result    = result;
            var success = false;
            var methods = GetType().GetMethods((BindingFlags.Public | (BindingFlags.NonPublic | BindingFlags.Instance)));

            foreach (var method in methods)
            {
                var filters = method.GetCustomAttributes(typeof(ControllerActionAttribute), true);
                foreach (ControllerActionAttribute action in filters)
                {
                    if (((action.Controller == args.Controller) || (!(string.IsNullOrEmpty(args.Controller)) && Regex.IsMatch(args.Controller, action.Controller))) && ((!viewMatch && string.IsNullOrEmpty(action.View)) || (action.View == args.View)))
                    {
                        if ((action.CommandName == args.CommandName) && ((!argumentMatch && string.IsNullOrEmpty(action.CommandArgument)) || (action.CommandArgument == args.CommandArgument)))
                        {
                            if (action.Phase == phase)
                            {
                                var parameters = method.GetParameters();
                                if ((parameters.Length == 2) && ((parameters[0].ParameterType == typeof(ActionArgs)) && (parameters[1].ParameterType == typeof(ActionResult))))
                                {
                                    method.Invoke(this, new object[] {
                                        args,
                                        result
                                    });
                                }
                                else
                                {
                                    var arguments = new object[parameters.Length];
                                    for (var i = 0; (i < parameters.Length); i++)
                                    {
                                        var p = parameters[i];
                                        var v = SelectFieldValueObject(p.Name);
                                        if (v != null)
                                        {
                                            if (p.ParameterType.Equals(typeof(FieldValue)))
                                            {
                                                arguments[i] = v;
                                            }
                                            else
                                            {
                                                try
                                                {
                                                    arguments[i] = DataControllerBase.ConvertToType(p.ParameterType, v.Value);
                                                }
                                                catch (Exception)
                                                {
                                                }
                                            }
                                        }
                                    }
                                    method.Invoke(this, arguments);
                                    success = true;
                                }
                            }
                        }
                    }
                }
            }
            return(success);
        }