예제 #1
0
        /// <summary>
        /// Gets the view models of a model.
        /// </summary>
        /// <param name="model">The model to find the linked view models for.</param>
        /// <returns>An array containing all the view models.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="model"/> is <c>null</c>.</exception>
        public IViewModel[] GetViewModelsOfModel(object model)
        {
            Argument.IsNotNull("model", model);

            var modelType = ObjectToStringHelper.ToTypeString(model);

            Log.Debug("Getting all view models that are linked to model '{0}'", modelType);

            var viewModels = new List <IViewModel>();

            lock (_viewModelModelsLock)
            {
                foreach (var viewModelModel in _viewModelModels)
                {
                    var viewModelIdentifiers = (from m in viewModelModel.Value
                                                where ObjectHelper.AreEqualReferences(m, model)
                                                select viewModelModel.Key);

                    foreach (var viewModelIdentifier in viewModelIdentifiers)
                    {
                        var vm = GetViewModel(viewModelIdentifier);
                        if (vm != null)
                        {
                            viewModels.Add(vm);
                        }
                    }
                }
            }

            Log.Debug("Found '{0}' view models that are linked to model '{1}'", viewModels.Count, modelType);

            return(viewModels.ToArray());
        }
예제 #2
0
파일: NinjectHelper.cs 프로젝트: ziez/Catel
        /// <summary>
        /// Gets the registration info.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="interfaceType">Type of the interface.</param>
        /// <returns>The registration info about the type or <c>null</c> if the type is not registered.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="container"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">If <paramref name="interfaceType"/> is <c>null</c>.</exception>
        /// <exception cref="NotSupportedException">If <paramref name="container"/> is not a valid container.</exception>
        /// <remarks>
        /// Equals <c>kernel.GetBindings(interfaceType).Any()</c>.
        /// </remarks>
        public override RegistrationInfo GetRegistrationInfo(object container, Type interfaceType)
        {
            Argument.IsNotNull("container", container);
            Argument.IsNotNull("interfaceType", interfaceType);

            if (!IsValidContainer(container))
            {
                throw new NotSupportedException("Only Ninject containers are supported");
            }

            // Note that this code equals:
            //   var binding = kernel.GetBindings(interfaceType).FirstOrDefault();
            //   binding.ScopeCallback != StandardScopeCallbacks.Singleton

            var containerType         = container.GetType();
            var getBindingsMethodInfo = containerType.GetMethodEx("GetBindings");
            var bindings = (IEnumerable)getBindingsMethodInfo.Invoke(container, new object[] { interfaceType });

            var firstBinding = bindings.Cast <object>().FirstOrDefault();

            if (firstBinding == null)
            {
                return(null);
            }

            var standardScopeCallbacks          = TypeCache.GetTypeWithoutAssembly("Ninject.Infrastructure.StandardScopeCallbacks");
            var singletonScopeCallbackFieldInfo = standardScopeCallbacks.GetFieldEx("Singleton", false, true);
            var singletonScopeCallback          = singletonScopeCallbackFieldInfo.GetValue(null);
            var scopeCallback = PropertyHelper.GetPropertyValue(firstBinding, "ScopeCallback");

            var registrationType = ObjectHelper.AreEqualReferences(scopeCallback, singletonScopeCallback) ? RegistrationType.Singleton : RegistrationType.Transient;

            return(new RegistrationInfo(interfaceType, typeof(object), registrationType));
        }
예제 #3
0
        /// <summary>
        /// Sets the value of a specific property.
        /// </summary>
        /// <param name="property">The property to set.</param>
        /// <param name="value">Value of the property.</param>
        /// <param name="notifyOnChange">If <c>true</c>, the <see cref="INotifyPropertyChanged.PropertyChanged"/> event will be invoked.</param>
        /// <exception cref="PropertyNotNullableException">The property is not nullable, but <paramref name="value"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="property"/> is <c>null</c>.</exception>
        protected internal void SetValue(PropertyData property, object value, bool notifyOnChange = true)
        {
            Argument.IsNotNull("property", property);

            // Is the object currently read-only (and aren't we changing that)?
            if (IsReadOnly)
            {
                if (property != IsReadOnlyProperty)
                {
                    Log.Warning("Cannot set property '{0}', object is currently read-only", property.Name);
                    return;
                }
            }

            if (property.IsCalculatedProperty)
            {
                Log.Warning("Cannot set property '{0}', the property is a calculated property", property.Name);
                return;
            }

            if ((value != null) && !property.Type.IsInstanceOfTypeEx(value))
            {
                if (!value.GetType().IsCOMObjectEx())
                {
                    throw Log.ErrorAndCreateException(msg => new InvalidPropertyValueException(property.Name, property.Type, value.GetType()),
                                                      "Cannot set value '{0}' to property '{1}' of type '{2}', the value is invalid", value, property.Name, GetType().FullName);
                }
            }

            var    notify   = false;
            object oldValue = null;

            lock (_lock)
            {
                var changeNotificationsSuspensionContext = _changeNotificationsSuspensionContext;

                oldValue = GetValueFromPropertyBag <object>(property.Name);
                var areOldAndNewValuesEqual = ObjectHelper.AreEqualReferences(oldValue, value);

                if (!areOldAndNewValuesEqual)
                {
                    SetValueToPropertyBag(property.Name, value);
                }

                notify = (notifyOnChange && (AlwaysInvokeNotifyChanged || !areOldAndNewValuesEqual));

                if (changeNotificationsSuspensionContext != null)
                {
                    changeNotificationsSuspensionContext.Add(property.Name);
                    notify = false;
                }
            }

            // Notify outside lock
            if (notify)
            {
                RaisePropertyChanged(property.Name, oldValue, value);
            }
        }
예제 #4
0
            public void ReturnsTrueForTwoNullValues()
            {
                object obj1 = null;
                object obj2 = null;

                Assert.IsTrue(ObjectHelper.AreEqualReferences(obj1, obj2));
                Assert.IsTrue(ObjectHelper.AreEqualReferences(obj2, obj1));
            }
예제 #5
0
            public void ReturnsFalseForBoxedDifferentIntegers()
            {
                object obj1 = 5;
                object obj2 = 6;

                Assert.IsFalse(ObjectHelper.AreEqualReferences(obj1, obj2));
                Assert.IsFalse(ObjectHelper.AreEqualReferences(obj2, obj1));
            }
예제 #6
0
            public void ReturnsTrueForBoxedEqualIntegers()
            {
                object obj1 = 5;
                object obj2 = 5;

                Assert.IsTrue(ObjectHelper.AreEqualReferences(obj1, obj2));
                Assert.IsTrue(ObjectHelper.AreEqualReferences(obj2, obj1));
            }
예제 #7
0
            public void ReturnsFalseForOneNullValue()
            {
                object obj1 = 5;
                object obj2 = null;

                Assert.IsFalse(ObjectHelper.AreEqualReferences(obj1, obj2));
                Assert.IsFalse(ObjectHelper.AreEqualReferences(obj2, obj1));
            }
예제 #8
0
            public void ReturnsTrueForDifferentReferenceTypes()
            {
                object obj1 = new { Id = "test" };
                object obj2 = new { Id = "test" };

                Assert.IsTrue(obj1.Equals(obj2));
                Assert.IsFalse(ObjectHelper.AreEqualReferences(obj1, obj2));
                Assert.IsFalse(ObjectHelper.AreEqualReferences(obj2, obj1));
            }
예제 #9
0
        /// <summary>
        /// Unregisters the view model as child on the parent view model.
        /// </summary>
        private void UnregisterViewModelAsChild()
        {
            var parentViewModel = _parentViewModel as IRelationalViewModel;
            var viewModel       = ViewModel as IRelationalViewModel;

            if ((parentViewModel != null) && (viewModel != null) && !ObjectHelper.AreEqualReferences(parentViewModel, viewModel))
            {
                viewModel.SetParentViewModel(null);
                parentViewModel.UnregisterChildViewModel(ViewModel);
            }
        }
예제 #10
0
        /// <summary>
        /// Sets the property value.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="value">The value.</param>
        /// <exception cref="ArgumentException">The <paramref name="propertyName" /> is <c>null</c> or whitespace.</exception>
        public void SetPropertyValue(string propertyName, object value)
        {
            Argument.IsNotNullOrWhitespace("propertyName", propertyName);

            lock (_lockObject)
            {
                if (!_properties.ContainsKey(propertyName) || !ObjectHelper.AreEqualReferences(_properties[propertyName], value))
                {
                    _properties[propertyName] = value;

                    RaisePropertyChanged(propertyName);
                }
            }
        }
예제 #11
0
        /// <summary>
        /// Subscribes to a parent view model.
        /// </summary>
        /// <param name="parentViewModel">The parent view model.</param>
        private void SubscribeToParentViewModel(IViewModel parentViewModel)
        {
            if ((parentViewModel != null) && !ObjectHelper.AreEqualReferences(parentViewModel, ViewModel))
            {
                _parentViewModel = parentViewModel;

                RegisterViewModelAsChild();

                _parentViewModel.Saving    += OnParentViewModelSaving;
                _parentViewModel.Canceling += OnParentViewModelCanceling;

                Log.Debug("Subscribed to parent view model '{0}'", parentViewModel.GetType());
            }
        }
예제 #12
0
        public override void SetValue <TValue>(string name, TValue value)
        {
            Argument.IsNotNullOrWhitespace("name", name);

            var raisePropertyChanged = false;

            lock (_lockObject)
            {
                if (!_properties.TryGetValue(name, out var propertyValue) || !ObjectHelper.AreEqualReferences(propertyValue, value))
                {
                    _properties[name]    = value;
                    raisePropertyChanged = true;
                }
            }

            if (raisePropertyChanged)
            {
                RaisePropertyChanged(name);
            }
        }
예제 #13
0
        /// <summary>
        /// Sets the property value.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="value">The value.</param>
        /// <exception cref="ArgumentException">The <paramref name="propertyName" /> is <c>null</c> or whitespace.</exception>
        public void SetPropertyValue(string propertyName, object value)
        {
            Argument.IsNotNullOrWhitespace("propertyName", propertyName);

            var raisePropertyChanged = false;

            lock (_lockObject)
            {
                object propertyValue;
                if (!_properties.TryGetValue(propertyName, out propertyValue) || !ObjectHelper.AreEqualReferences(propertyValue, value))
                {
                    _properties[propertyName] = value;
                    raisePropertyChanged      = true;
                }
            }

            if (raisePropertyChanged)
            {
                RaisePropertyChanged(propertyName);
            }
        }
예제 #14
0
        /// <summary>
        /// Unregisters the view model as child on the parent view model.
        /// </summary>
        private void UnregisterViewModelAsChild()
        {
            var parentViewModel = _parentViewModel as IRelationalViewModel;

            if (parentViewModel is null)
            {
                return;
            }

            var viewModel = ViewModel as IRelationalViewModel;

            if (viewModel is null)
            {
                return;
            }

            if (ObjectHelper.AreEqualReferences(parentViewModel, viewModel))
            {
                return;
            }

            viewModel.SetParentViewModel(null);
            parentViewModel.UnregisterChildViewModel(viewModel);
        }
        /// <summary>
        /// Sets the value of a specific property.
        /// </summary>
        /// <param name="property">The property to set.</param>
        /// <param name="value">Value of the property.</param>
        /// <param name="notifyOnChange">If <c>true</c>, the <see cref="INotifyPropertyChanged.PropertyChanged"/> event will be invoked.</param>
        /// <param name="validateAttributes">If set to <c>true</c>, the validation attributes on the property will be validated.</param>
        /// <exception cref="PropertyNotNullableException">The property is not nullable, but <paramref name="value"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="property"/> is <c>null</c>.</exception>
        internal void SetValue(PropertyData property, object value, bool notifyOnChange, bool validateAttributes)
        {
            Argument.IsNotNull("property", property);

            // Is the object currently read-only (and aren't we changing that)?
            if (IsReadOnly)
            {
                if (property != IsReadOnlyProperty)
                {
                    Log.Warning("Cannot set property '{0}', object is currently read-only", property.Name);
                    return;
                }
            }

            if (property.IsCalculatedProperty)
            {
                Log.Warning("Cannot set property '{0}', the property is a calculated property", property.Name);
                return;
            }

            if (!LeanAndMeanModel)
            {
                if ((value != null) && !property.Type.IsInstanceOfTypeEx(value))
                {
                    if (!value.GetType().IsCOMObjectEx())
                    {
                        throw Log.ErrorAndCreateException(msg => new InvalidPropertyValueException(property.Name, property.Type, value.GetType()),
                                                          "Cannot set value '{0}' to property '{1}' of type '{2}', the value is invalid", value, property.Name, GetType().FullName);
                    }
                }
            }

            var    notify   = false;
            object oldValue = null;

            lock (_propertyValuesLock)
            {
                oldValue = GetValueFast <object>(property.Name);
                var areOldAndNewValuesEqual = ObjectHelper.AreEqualReferences(oldValue, value);

                if (notifyOnChange && (AlwaysInvokeNotifyChanged || !areOldAndNewValuesEqual) && !LeanAndMeanModel)
                {
                    var propertyChangingEventArgs = new AdvancedPropertyChangingEventArgs(property.Name);
                    RaisePropertyChanging(this, propertyChangingEventArgs);

                    if (propertyChangingEventArgs.Cancel)
                    {
                        Log.Debug("Change of property '{0}.{1}' is canceled in PropertyChanging event", GetType().FullName, property.Name);
                        return;
                    }
                }

                // Validate before assigning, dynamic properties will cause exception
                if (validateAttributes && !LeanAndMeanModel)
                {
                    ValidatePropertyUsingAnnotations(property.Name, value, property);
                }

                if (!areOldAndNewValuesEqual)
                {
                    SetValueFast(property.Name, value);
                }

                notify = (notifyOnChange && (AlwaysInvokeNotifyChanged || !areOldAndNewValuesEqual) && !LeanAndMeanModel);
            }

            // Notify outside lock
            if (notify)
            {
                RaisePropertyChanged(property.Name, oldValue, value);
            }
        }