Esempio n. 1
0
        private IBehavior HydrateBehavior(string id, bool edit)
        {
            IBehavior behavior = null;
            // find the mapped setting if any
            Settings mappedSetting = LayoutSettings
                                     .Where(setting => setting.PanelID.EqualsIgnoreCase(id))
                                     .FirstOrDefault();

            if (mappedSetting != null)
            {
                behavior = LoadBehaviorControl(mappedSetting.BehaviorControl, edit);
                if (behavior != null &&
                    !string.IsNullOrEmpty(mappedSetting.BehaviorDataRaw))
                {
                    var data = TryHydrateData(behavior.GetType(), mappedSetting.BehaviorDataRaw);

                    // Although we're using generics to have strongly typed Data properties,
                    // Generics unfortunately doesn't support contra-variance for downcasting
                    // the defined template for T in IBehavior<T> eventhough the T will always
                    // a type inherited from IData, we therefore resort to reflection
                    var dataProperty = behavior
                                       .GetType()
                                       .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                                       .Where(prop => prop.Name.EqualsIgnoreCase("Data"))
                                       .FirstOrDefault();
                    if (dataProperty != null)
                    {
                        dataProperty.SetValue(behavior, data, new object[] { });
                    }
                }
            }

            return(behavior);
        }
Esempio n. 2
0
        public IBehavior Remove(IBehavior behavior)
        {
            //validate
            if (behavior == null)
            {
                return(null);
            }

            IBehavior storedBehavior = null;

            if (_behaviors.TryGetValue(behavior.GetType(), out storedBehavior))
            {
                //remove only the same instance
                if (storedBehavior != behavior)
                {
                    return(null);
                }

                //remove
                _behaviors.TryRemove(behavior.GetType(), out storedBehavior);

                //disable
                RemoveDisable(behavior);
            }

            return(storedBehavior);
        }
Esempio n. 3
0
        public void EnableUndo_EnablesUndoSetValueBehavior()
        {
            UserVM vm = CreateUserVMWithItems();

            var department = vm.GetValue(x => x.Groups);

            var relevantProperties = new[] {
                department.GetProperty(x => x.AllItems),
                department.GetProperty(x => x.SelectedItems)
            };

            foreach (var property in relevantProperties)
            {
                bool found = false;
                for (IBehavior b = property.Behaviors; b != null; b = b.Successor)
                {
                    if (b.GetType().Name.Contains("UndoSetValueBehavior") ||
                        b.GetType().Name.Contains("UndoCollectionModifcationBehavior"))
                    {
                        found = true;
                        break;
                    }
                }
                Assert.IsTrue(found);
            }
        }
Esempio n. 4
0
        /// <inheritdoc />
        public void Add(IBehavior <TExtension> behavior)
        {
            Console.WriteLine("::: Adding behavior {0}", behavior.GetType().Name);

            this.decoratedExecutable.Add(behavior);

            Console.WriteLine("::: Added behavior {0}", behavior.GetType().Name);
        }
Esempio n. 5
0
 protected void setBehavior(IBehavior behavior)
 {
     if (!isUpdating)
     {
         behaviors.Add(behavior.GetType().Name, behavior);
     }
     else
     {
         addedBehaviors.Add(behavior);
     }
 }
Esempio n. 6
0
        public void EnableUndo_EnablesUndoSetValueBehavior()
        {
            UserVM vm = new UserVM();

            IViewModel department = vm.GetValue(x => x.Department);

            foreach (var property in department.Descriptor.Properties)
            {
                bool found = false;
                for (IBehavior b = property.Behaviors; b != null; b = b.Successor)
                {
                    if (b.GetType().Name.Contains("UndoSetValueBehavior") ||
                        b.GetType().Name.Contains("UndoCollectionModifcationBehavior"))
                    {
                        found = true;
                        break;
                    }
                }
                Assert.IsTrue(found);
            }
        }
Esempio n. 7
0
 public override void Process()
 {
     try
     {
         Router = RouterManager.Manager.Routing(null);
         if (Router == null)
         {
             throw new NotFoundException("未找到资源");
         }
         IBehavior behavior = null;
         TypeInfo  type     = null;
         while ((behavior = Router.Next(path)) != null)
         {
             type = behavior.GetType().GetTypeInfo();
             RouterOnMethodAttribute routerOnMethodAttr = type.GetCustomAttribute <RouterOnMethodAttribute>();
             if (path.Current.Next != null && routerOnMethodAttr != null)
             {
                 var rest = type.GetMethod(routerOnMethodAttr.Method, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod);
                 if (rest == null)
                 {
                     throw new StatusException($"{nameof(behavior)}内部未实现RouteOnMethod接口", 404);
                 }
                 string raw = rest.Invoke(behavior, new object[] { path }) as string;
                 if (string.IsNullOrWhiteSpace(raw))
                 {
                     break;
                     //throw new StatusException($"{nameof(behavior)}内部未制定具体路由", 404);
                 }
                 rest = type.GetMethod(raw, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod);
                 if (rest == null)
                 {
                     throw new StatusException("未找相关的实现方法", 404);
                 }
                 InvokeMethod(rest, behavior, null);
                 break;
             }
             else
             {
                 Invoke(behavior);
             }
         }
         //throw new Exception("1");
     }
     catch (Exception ex)
     {
         //Assert.E(ex);
         throw ex;
     }
 }
Esempio n. 8
0
 /// <summary>
 /// 增加一个单例行为
 /// </summary>
 /// <param name="beh"></param>
 public void AddSingletonBehavior(IBehavior beh)
 {
     if (_behaviors == null)
     {
         _behaviors = new List <IBehavior>();
     }
     foreach (var b in _behaviors)
     {
         if (beh.GetType() == b.GetType())
         {
             return;
         }
     }
     _behaviors.Add(beh);
 }
Esempio n. 9
0
        protected void Invoke(IBehavior behavior)
        {
            var verbAttrs = behavior.GetType().GetTypeInfo().GetCustomAttribute(typeof(VerbAttribute)) as VerbAttribute;

            if (verbAttrs == null || verbAttrs.Verb.HasFlag(Verb))
            {
                //string text = middleware.Text;
                //身份验证
                Authentic(behavior);

                behavior.Invoke().Wait();
                return;
            }
            //Assert.W("该中间件不支持相应谓词操作");
            throw new NotFoundException("该中间件不支持相应谓词操作");
        }
Esempio n. 10
0
        //protected abstract void Invoke();

        protected virtual void Authentic(IBehavior middle)
        {
            var ti     = middle.GetType().GetTypeInfo();
            var authes = ti.GetCustomAttributes(typeof(Phyah.Authentication.AuthenticationAttribute)) as IEnumerable <Phyah.Authentication.AuthenticationAttribute>;

            foreach (var item in authes)
            {
                if (item is Phyah.Authentication.IAuthentication)
                {
                    ((Phyah.Authentication.IAuthentication)(item)).Authentic();
                }
                else
                {
                    ((Phyah.Authentication.IAuthentication)Activator.CreateInstance(item.Type)).Authentic();
                }
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Adds the specified behavior.
        /// </summary>
        /// <param name="behavior">The behavior.</param>
        public void Add(IBehavior behavior)
        {
            // Try finding the entity behavior dictionary
            if (!_entityBehaviors.TryGetValue(behavior.Name, out var ebd))
            {
                ebd = new EntityBehaviorDictionary(behavior.Name);
                _entityBehaviors.Add(behavior.Name, ebd);
            }
            ebd.Add(behavior.GetType(), behavior);

            // Track lists
            foreach (var pair in _behaviorLists)
            {
                if (ebd.TryGetValue(pair.Key, out var b) && b == behavior)
                {
                    pair.Value.Add(b);
                }
            }
        }
Esempio n. 12
0
        public IBehavior Add(IBehavior behavior)
        {
            //validate
            if (behavior == null)
            {
                return(behavior);
            }

            //add or replace
            _behaviors.AddOrUpdate(behavior.GetType(), behavior, (i, e) => { RemoveDisable(e); return(behavior); });

            //enable
            behavior.Enable(_arm);

            //add workflow
            AddWorkflow(behavior as IWorkflow);

            return(behavior);
        }
Esempio n. 13
0
        /// <summary>
        /// Adds the specified behavior to the pool.
        /// </summary>
        /// <param name="type">The type of the requested behavior.</param>
        /// <param name="creator">The entity identifier to which the .</param>
        /// <param name="behavior">The behavior to be added.</param>
        /// <exception cref="SpiceSharp.CircuitException">Invalid behavior</exception>
        public void Add(Type type, string creator, IBehavior behavior)
        {
            // Create the list entry if necessary
            if (!_behaviors.TryGetValue(type, out var behaviorList))
            {
                behaviorList = new List <IBehavior>();
                _behaviors.Add(type, behaviorList);
            }

            // Find the entity behaviors
            if (!_entityBehaviors.TryGetValue(behavior.Name, out var ebd))
            {
                ebd = new EntityBehaviorDictionary(behavior.Name);
                _entityBehaviors.Add(behavior.Name, ebd);
            }

            // Add the behavior
            ebd.Add(behavior.GetType(), behavior);
            behaviorList.Add(behavior);
        }
Esempio n. 14
0
 public void Set(IBehavior behavior)
 {
     _lazyBehaviors.Instance[behavior.GetType( )] = behavior;
 }