Ejemplo n.º 1
0
        public void PublishObject(ILiveObject obj)
        {
            var typeDesc = (ObjectDescriptor)TypeContext.GetTypeDescriptor(obj.GetType());

            var objId = obj.ResourceId;

            if (String.IsNullOrEmpty(objId))
            {
                throw new Exception("Cannot publish a LiveObject with missing ResourceId!");
            }

            var existingObj = ObjectRepository.GetValueOrDefault(objId);

            //if (existingObj != null && existingObj != obj)
            //    throw new Exception($"Object's key is not unique: {objId}");
            if (existingObj != null && existingObj != obj)
            {
                Logger.Log($"[Warning] Object's key is not unique: {objId}");
            }
            ObjectRepository[objId] = obj;

            if (TrackChanges)
            {
                if (obj is INotifyPropertyChanged)
                {
                    ((INotifyPropertyChanged)obj).PropertyChanged += OnObjectPropertyChanged;
                }

                foreach (var propDesc in typeDesc.Properties.Values.Where(x => typeof(INotifyCollectionChanged).IsAssignableFrom(x.PropertyInfo.PropertyType)))
                {
                    var propValue = (INotifyCollectionChanged)propDesc.PropertyInfo.GetValue(obj);
                    if (propValue == null)
                    {
                        continue;
                    }

                    propValue.CollectionChanged += (sender, args) => ListChanged?.Invoke(this, obj, propDesc, args);
                }
            }
        }