示例#1
0
        /// <summary>
        /// Unregisters a specific recipient for a specific message with the specified tag.
        /// </summary>
        /// <typeparam name="TMessage">The type of the message.</typeparam>
        /// <param name="recipient">The recipient to unregister.</param>
        /// <param name="handler">The handler method.</param>
        /// <param name="tag">The message tag.</param>
        /// <returns><c>true</c> if the handler is unregistered successfully; otherwise <c>false</c>.</returns>
        /// <remarks>
        /// A handler cannot be unregistered when it is not registered first. If a handler is unregistered while it
        /// is not registered, this method will return <c>false</c>.
        /// </remarks>
        /// <exception cref="ArgumentNullException">The <paramref name="handler"/> is <c>null</c>.</exception>
        public bool Unregister <TMessage>(object recipient, Action <TMessage> handler, object tag = null)
        {
            Argument.IsNotNull("handler", handler);

            lock (_lockObject)
            {
                var messageType = typeof(TMessage);

                if (_registeredHandlers.ContainsKey(messageType))
                {
                    var messageHandlers = _registeredHandlers[messageType];
                    for (int i = 0; i < messageHandlers.Count; i++)
                    {
                        var handlerInfo = messageHandlers[i];
                        var weakAction  = (IWeakAction <TMessage>)handlerInfo.Action;
                        if (TagHelper.AreTagsEqual(tag, handlerInfo.Tag) && AreEqualHandlers(handler, weakAction))
                        {
                            messageHandlers.RemoveAt(i--);

                            Log.Debug("Unregistered handler for message type '{0}' with tag '{1}'", messageType.Name, ObjectToStringHelper.ToString(tag));

                            return(true);
                        }
                    }
                }

                Log.Warning("Failed to unregister handler for message type '{0}' with tag '{1}' because handler was not registered", messageType.Name, ObjectToStringHelper.ToString(tag));

                return(false);
            }
        }
示例#2
0
        /// <summary>
        /// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object" /> to compare with the current <see cref="T:System.Object" />.</param>
        /// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
            {
                return(false);
            }

            if (ReferenceEquals(this, obj))
            {
                return(true);
            }

            if (obj.GetType() != GetType())
            {
                return(false);
            }

            var other = (TypeRequestInfo)obj;

            if (Type != other.Type)
            {
                return(false);
            }

            if (!TagHelper.AreTagsEqual(Tag, other.Tag))
            {
                return(false);
            }

            return(true);
        }
示例#3
0
        /// <summary>
        /// Unregisters a specific recipient for all the (non-static) message the recipient is subscribed to.
        /// </summary>
        /// <param name="recipient">The recipient to unregister.</param>
        /// <param name="tag">The message tag.</param>
        /// <param name="ignoreTag">If set to <c>true</c>, tags are ignored.</param>
        /// <returns><c>true</c> if the handler is unregistered successfully; otherwise <c>false</c>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="recipient"/> is <c>null</c>.</exception>
        /// <remarks>A handler cannot be unregistered when it is not registered first. If a handler is unregistered while it
        /// is not registered, this method will return <c>false</c>.</remarks>
        public bool UnregisterRecipient(object recipient, object tag, bool ignoreTag)
        {
            Argument.IsNotNull("recipient", recipient);

            lock (_lockObject)
            {
                int handlerCounter = 0;
                var keys           = _registeredHandlers.Keys.ToList();
                foreach (var key in keys)
                {
                    var messageHandlers = _registeredHandlers[key];
                    for (int i = 0; i < messageHandlers.Count; i++)
                    {
                        var handlerInfo   = messageHandlers[i];
                        var weakReference = (IWeakReference)handlerInfo.Action;
                        if (ignoreTag || TagHelper.AreTagsEqual(tag, handlerInfo.Tag))
                        {
                            if (ReferenceEquals(recipient, weakReference.Target))
                            {
                                messageHandlers.RemoveAt(i--);
                                handlerCounter++;

                                Log.Debug("Unregistered handler for message type '{0}' with tag '{1}'", key.Name, ObjectToStringHelper.ToString(tag));
                            }
                        }
                    }
                }

                Log.Debug("Unregistered '{0}' handlers for the recipient with tag '{1}'", handlerCounter, ObjectToStringHelper.ToString(tag));

                return(true);
            }
        }
示例#4
0
        /// <summary>
        /// Broadcasts a message to all message targets for a given message tag and passes a parameter.
        /// </summary>
        /// <typeparam name="TMessage">The type of the message.</typeparam>
        /// <param name="message">The message parameter.</param>
        /// <param name="tag">The message tag.</param>
        /// <returns>
        /// <c>true</c> if any handlers were invoked; otherwise <c>false</c>.
        /// </returns>
        /// <exception cref="ArgumentNullException">The <paramref name="message"/> is <c>null</c>.</exception>
        public bool SendMessage <TMessage>(TMessage message, object tag = null)
        {
            Argument.IsNotNull("message", message);

            Log.Debug("Sending message of type '{0}' with tag '{1}'", message.GetType().FullName, ObjectToStringHelper.ToString(tag));

            int invokedHandlersCount = 0;

            lock (_lockObject)
            {
                var messageType = typeof(TMessage);

                if (_registeredHandlers.ContainsKey(messageType))
                {
                    // CTL-311: first convert to array, then handle messages
                    var messageHandlers = _registeredHandlers[messageType].ToArray();
                    foreach (var handler in messageHandlers)
                    {
                        if (TagHelper.AreTagsEqual(tag, handler.Tag))
                        {
                            if (handler.Action.ExecuteWithObject(message))
                            {
                                invokedHandlersCount++;
                            }
                        }
                    }
                }
            }

            Log.Debug("Sent message to {0} recipients", invokedHandlersCount);

            CleanUp();

            return(invokedHandlersCount != 0);
        }
        /// <summary>
        /// Processes the specified types to register.
        /// </summary>
        /// <param name="typesToRegister">The types to register.</param>
        /// <exception cref="System.ArgumentNullException">The <paramref name="typesToRegister" /> is <c>null</c>.</exception>
        public override void Process(IEnumerable <Type> typesToRegister)
        {
            Argument.IsNotNull("typesToRegister", typesToRegister);

            var typesToHandle  = typesToRegister as Type[] ?? typesToRegister.ToArray();
            var interfaceTypes = typesToHandle.Select(type =>
            {
                if (type.IsInterfaceEx() && type.Name.StartsWith("I"))
                {
                    var implementationType = typesToHandle.FirstOrDefault(row => TagHelper.AreTagsEqual(row.Name, type.Name.Replace("I", string.Empty).Trim()) && row.IsClassEx() && type.IsAssignableFromEx(row));

                    if (implementationType != null)
                    {
                        return(new { InterfaceType = type, ImplementationType = implementationType });
                    }
                }

                return(null);
            }).Where(type => type != null);

            interfaceTypes.ForEach(type =>
            {
                Log.Debug("Applying '{0}' on '{1}'", GetType().Name, type.InterfaceType);

                Container.RegisterType(type.InterfaceType, type.ImplementationType, registrationType: RegistrationType);
            });
        }
示例#6
0
        /// <summary>
        /// Determines whether the specified recipient is registered.
        /// </summary>
        /// <typeparam name="TMessage">The type of the message.</typeparam>
        /// <param name="recipient">The recipient.</param>
        /// <param name="handler">The handler.</param>
        /// <param name="tag">The tag.</param>
        /// <returns>
        ///     <c>true</c> if the specified recipient is registered; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="ArgumentNullException">The <paramref name="handler"/> is <c>null</c>.</exception>
        public bool IsRegistered <TMessage>(object recipient, Action <TMessage> handler, object tag = null)
        {
            Argument.IsNotNull("handler", handler);

            lock (_lockObject)
            {
                var messageType = typeof(TMessage);

                if (_registeredHandlers.ContainsKey(messageType))
                {
                    var messageHandlers = _registeredHandlers[messageType];
                    for (int i = 0; i < messageHandlers.Count; i++)
                    {
                        var handlerInfo = messageHandlers[i];
                        var weakAction  = (IWeakAction <TMessage>)handlerInfo.Action;

                        var target = weakAction.Target;
                        if (target != null)
                        {
                            if (!ReferenceEquals(recipient, target))
                            {
                                continue;
                            }
                        }

                        if (TagHelper.AreTagsEqual(tag, handlerInfo.Tag) && AreEqualHandlers(handler, weakAction))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
        /// <summary>
        /// Gets the document.
        /// </summary>
        /// <param name="docingManager">The docing manager .</param>
        /// <param name="viewType">Type of the view.</param>
        /// <param name="tag">The tag.</param>
        /// <returns>
        /// The found document or <c>null</c> if no document was found.
        /// </returns>
        /// <exception cref="ArgumentNullException">The <paramref name="docingManager" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="docingManager" /> is <c>null</c>.</exception>
        public static LayoutAnchorable FindDocument(DockingManager docingManager, Type viewType, object tag = null)
        {
            Argument.IsNotNull("docingManager", docingManager);
            Argument.IsNotNull("viewType", viewType);

            LayoutAnchorable doc = null;
            var documents        = docingManager.Layout.Descendents().OfType <LayoutAnchorable>();

            foreach (var layoutAnchorable in documents)
            {
                var content = layoutAnchorable.Content as IDockingManagerContainer;

                if (content != null)
                {
                    var nestedDockingManager = content;

                    if (nestedDockingManager.IsActive)
                    {
                        doc = FindDocument(nestedDockingManager.DockingManager, viewType, tag);
                        break;
                    }
                }

                if (layoutAnchorable.Content.GetType() == viewType && TagHelper.AreTagsEqual(tag, ((IView)layoutAnchorable.Content).Tag))
                {
                    doc = layoutAnchorable;
                    break;
                }
            }

            return(doc);
        }
示例#8
0
            public void ReturnsFalseForDifferentInstances()
            {
                var firstEntry  = ModelBaseTestHelper.CreateIniEntryObject("A", "B", "C");
                var secondEntry = ModelBaseTestHelper.CreateIniEntryObject("D", "E", "F");

                Assert.IsFalse(TagHelper.AreTagsEqual(firstEntry, secondEntry));
            }
示例#9
0
        /// <summary>
        /// Finds the anchorable with the specified name.
        /// </summary>
        /// <param name="name">
        /// The name of the anchorable.
        /// </param>
        /// <param name="throwExceptionWhenNotFound">
        /// If set to <c>true</c>, this method will throw an <see cref="InvalidOperationException"/> when the anchorable cannot be found.
        /// </param>
        /// <returns>
        /// The <see cref="LayoutAnchorable"/> or <c>null</c> if the anchorable cannot be found.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// The <paramref name="name"/> is <c>null</c> or whitespace.
        /// </exception>
        static private LayoutAnchorable FindAnchorable(string name, bool throwExceptionWhenNotFound = false)
        {
            Argument.IsNotNullOrWhitespace("name", name);

            var visibleAnchorable = (from child in DockingManager.Layout.Children
                                     where child is LayoutAnchorable && TagHelper.AreTagsEqual(((LayoutAnchorable)child).ContentId, name)
                                     select(LayoutAnchorable) child).FirstOrDefault();

            if (visibleAnchorable != null)
            {
                return(visibleAnchorable);
            }

            var invisibleAnchorable = (from child in DockingManager.Layout.Hidden
                                       where TagHelper.AreTagsEqual((child).ContentId, name)
                                       select child).FirstOrDefault();

            if (invisibleAnchorable != null)
            {
                return(invisibleAnchorable);
            }

            if (throwExceptionWhenNotFound)
            {
                string error = string.Format("Anchorable with name '{0}' cannot be found", name);
                Log.Error(error);
                throw new InvalidOperationException(error);
            }

            return(null);
        }
示例#10
0
        /// <summary>
        /// Gets the document.
        /// </summary>
        /// <param name="viewType">Type of the view.</param>
        /// <param name="tag">The tag.</param>
        /// <returns>The found document or <c>null</c> if no document was found.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="viewType"/> is <c>null</c>.</exception>
        public static CustomLayoutDocument FindDocument(Type viewType, object tag = null)
        {
            Argument.IsNotNull("viewType", viewType);

            return((from document in LayoutDocumentPane.Children
                    where document is LayoutDocument && document.Content.GetType() == viewType &&
                    TagHelper.AreTagsEqual(tag, ((IView)document.Content).Tag)
                    select document).Cast <CustomLayoutDocument>().FirstOrDefault());
        }
示例#11
0
        /// <summary>
        /// Gets all the business rule validations with the specified tag.
        /// </summary>
        /// <param name="tag">The tag.</param>
        /// <returns>
        /// List of <see cref="IBusinessRuleValidationResult"/> items.
        /// </returns>
        public List <IBusinessRuleValidationResult> GetBusinessRuleValidations(object tag)
        {
            lock (_businessRuleValidations)
            {
                var list = (from validation in _businessRuleValidations
                            where TagHelper.AreTagsEqual(validation.Tag, tag)
                            select validation).ToList();

                return(list);
            }
        }
示例#12
0
        /// <summary>
        /// Gets all the field validations with the specified tag.
        /// </summary>
        /// <param name="tag">The tag.</param>
        /// <returns>List of <see cref="IFieldValidationResult" /> items.</returns>
        public List <IFieldValidationResult> GetFieldValidations(object tag)
        {
            lock (_fieldValidations)
            {
                var list = (from validation in _fieldValidations
                            where TagHelper.AreTagsEqual(validation.Tag, tag)
                            select validation).ToList();

                return(list);
            }
        }
示例#13
0
            public void ReturnsTrueForEqualInstances()
            {
                var firstEntry  = ModelBaseTestHelper.CreateIniEntryObject("A", "B", "C");
                var secondEntry = ModelBaseTestHelper.CreateIniEntryObject("A", "B", "C");

                // References equal
                Assert.IsTrue(TagHelper.AreTagsEqual(firstEntry, firstEntry));

                // Objects equal
                Assert.IsTrue(TagHelper.AreTagsEqual(firstEntry, secondEntry));
            }
示例#14
0
        /// <summary>
        /// Determines whether the specified message type is registered.
        /// </summary>
        /// <param name="messageType">The type of the message.</param>
        /// <param name="tag">The tag.</param>
        /// <returns>
        ///   <c>true</c> if the message type is registered; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="ArgumentNullException">The <paramref name="messageType"/> is <c>null</c>.</exception>
        public bool IsMessageRegistered(Type messageType, object tag = null)
        {
            Argument.IsNotNull("messageType", messageType);

            lock (_lockObject)
            {
                if (_registeredHandlers.TryGetValue(messageType, out var messageHandlers))
                {
                    return(messageHandlers.Any(handlerInfo => TagHelper.AreTagsEqual(tag, handlerInfo.Tag)));
                }

                return(false);
            }
        }
示例#15
0
            public void MustExistsForRegisteredViewModels()
            {
                var firstvm   = new TestViewModel();
                var secondvm  = new TestViewModel();
                var vmManager = new ViewModelManager();

                vmManager.RegisterViewModelInstance(firstvm);
                vmManager.RegisterViewModelInstance(secondvm);

                var vmList = vmManager.ActiveViewModels.ToList();

                Assert.IsTrue(vmList.Any(vm => TagHelper.AreTagsEqual(vm.UniqueIdentifier, firstvm.UniqueIdentifier)));
                Assert.IsTrue(vmList.Any(vm => TagHelper.AreTagsEqual(vm.UniqueIdentifier, secondvm.UniqueIdentifier)));
            }
示例#16
0
        /// <summary>
        /// Gets all the field validations for the specified property name with the specified tag.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="tag">The tag.</param>
        /// <returns>List of <see cref="IFieldValidationResult" /> items.</returns>
        /// <exception cref="ArgumentException">The <paramref name="propertyName" /> is <c>null</c> or whitespace.</exception>
        public List <IFieldValidationResult> GetFieldValidations(string propertyName, object tag)
        {
            Argument.IsNotNullOrWhitespace("propertyName", propertyName);

            lock (_fieldValidations)
            {
                var list = (from validation in _fieldValidations
                            where string.Equals(validation.PropertyName, propertyName, StringComparison.OrdinalIgnoreCase) &&
                            TagHelper.AreTagsEqual(validation.Tag, tag)
                            select validation).ToList();

                return(list);
            }
        }
示例#17
0
        /// <summary>
        /// Determines whether the specified message type is registered.
        /// </summary>
        /// <param name="messageType">The type of the message.</param>
        /// <param name="tag">The tag.</param>
        /// <returns>
        ///   <c>true</c> if the message type is registered; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="ArgumentNullException">The <paramref name="messageType"/> is <c>null</c>.</exception>
        public bool IsMessageRegistered(Type messageType, object tag = null)
        {
            Argument.IsNotNull("messageType", messageType);

            lock (_lockObject)
            {
                if (_registeredHandlers.ContainsKey(messageType))
                {
                    var messageHandlers = _registeredHandlers[messageType];
                    return(messageHandlers.Any(handlerInfo => TagHelper.AreTagsEqual(tag, handlerInfo.Tag)));
                }

                return(false);
            }
        }
示例#18
0
        /// <summary>
        /// Finds the anchorable with the specified name.
        /// </summary>
        /// <param name="name">The name of the anchorable.</param>
        /// <param name="throwExceptionWhenNotFound">if set to <c>true</c>, this method will throw an <see cref="InvalidOperationException"/> when the anchorable cannot be found.</param>
        /// <returns>The <see cref="LayoutAnchorable"/> or <c>null</c> if the anchorable cannot be found.</returns>
        /// <exception cref="ArgumentException">The <paramref name="name"/> is <c>null</c> or whitespace.</exception>
        private LayoutAnchorable FindAnchorable(string name, bool throwExceptionWhenNotFound = false)
        {
            Argument.IsNotNullOrWhitespace("name", name);

            var result = (from anchorable in dockingManager.AnchorablesSource.Cast <LayoutAnchorable>()
                          where TagHelper.AreTagsEqual(anchorable.ContentId, name)
                          select anchorable).FirstOrDefault();

            if (throwExceptionWhenNotFound && result == null)
            {
                string error = string.Format("Anchorable with name '{0}' cannot be found", name);
                Log.Error(error);
                throw new InvalidOperationException(error);
            }

            return(result);
        }
示例#19
0
        /// <summary>
        /// Gets all the field and business rule validations with the specified tag.
        /// </summary>
        /// <param name="tag">The tag.</param>
        /// <returns>
        /// List of <see cref="IValidationResult"/> items.
        /// </returns>
        public List <IValidationResult> GetValidations(object tag)
        {
            var list = new List <IValidationResult>();

            lock (_fieldValidations)
            {
                list.AddRange(from validation in _fieldValidations
                              where TagHelper.AreTagsEqual(validation.Tag, tag)
                              select validation as IValidationResult);
            }

            lock (_businessRuleValidations)
            {
                list.AddRange(from validation in _businessRuleValidations
                              where TagHelper.AreTagsEqual(validation.Tag, tag)
                              select validation as IValidationResult);
            }

            return(list);
        }
示例#20
0
        /// <summary>
        /// Determines whether the specified recipient is registered.
        /// </summary>
        /// <typeparam name="TMessage">The type of the message.</typeparam>
        /// <param name="recipient">The recipient.</param>
        /// <param name="handler">The handler.</param>
        /// <param name="tag">The tag.</param>
        /// <returns>
        ///     <c>true</c> if the specified recipient is registered; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="ArgumentNullException">The <paramref name="handler"/> is <c>null</c>.</exception>
        public bool IsRegistered <TMessage>(object recipient, Action <TMessage> handler, object tag = null)
        {
            Argument.IsNotNull("handler", handler);

            lock (_lockObject)
            {
                var messageType = typeof(TMessage);

                List <WeakActionInfo> messageHandlers;
                if (_registeredHandlers.TryGetValue(messageType, out messageHandlers))
                {
                    for (int i = messageHandlers.Count - 1; i >= 0; i--)
                    {
                        var handlerInfo = messageHandlers[i];
                        var weakAction  = (IWeakAction <TMessage>)handlerInfo.Action;

                        if (!weakAction.IsTargetAlive)
                        {
                            messageHandlers.RemoveAt(i);
                            continue;
                        }

                        var target = weakAction.Target;
                        if (target != null)
                        {
                            if (!ReferenceEquals(recipient, target))
                            {
                                continue;
                            }
                        }

                        if (TagHelper.AreTagsEqual(tag, handlerInfo.Tag) && AreEqualHandlers(handler, weakAction))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
示例#21
0
 /// <summary>
 /// Remove all registered instances.
 /// </summary>
 /// <param name="tag">The tag of the registered the service. The default value is <c>null</c>.</param>
 public void RemoveAllInstances(object tag = null)
 {
     lock (_syncObject)
     {
         if (tag == null)
         {
             _registeredInstances.Clear();
         }
         else
         {
             for (int i = _registeredInstances.Count - 1; i >= 0; i--)
             {
                 var serviceInfo = _registeredInstances.Keys.ElementAt(i);
                 if (TagHelper.AreTagsEqual(serviceInfo.Tag, tag))
                 {
                     _registeredInstances.Remove(serviceInfo);
                 }
             }
         }
     }
 }
示例#22
0
        /// <summary>
        /// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
        /// <returns>
        ///   <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            var memberDefinition = obj as IMemberDefinition;

            if (memberDefinition == null)
            {
                return(false);
            }

            if (Parameters.Count != memberDefinition.Parameters.Count)
            {
                return(false);
            }

            return(!Parameters.Where((parameter, index) => !parameter.IsGenericParameter && parameter != memberDefinition.Parameters[index]).Any() && TagHelper.AreTagsEqual(MemberName, memberDefinition.MemberName));
        }
示例#23
0
 public void ReturnsTrueForBothNull()
 {
     Assert.IsTrue(TagHelper.AreTagsEqual(null, null));
 }
示例#24
0
 public void ReturnsTrueForEqualStrings()
 {
     Assert.IsTrue(TagHelper.AreTagsEqual("Catel", "Catel"));
 }
示例#25
0
 public void ReturnsFalseForDifferentCasingStrings()
 {
     Assert.IsFalse(TagHelper.AreTagsEqual("Catel", "catel"));
 }