Exemplo n.º 1
0
 /// <summary>
 /// Removes the first instance of the specefied binding from the objects bindings.
 /// </summary>
 /// <param name="obj">The object to remove the binding from.</param>
 /// <param name="binding">The binding to remove.</param>
 public static void RemoveBinding(this object obj, ObjectBinding binding)
 {
     if (bindings.ContainsKey(obj))
     {
         bindings[obj].Remove(binding);
     }
 }
Exemplo n.º 2
0
        public void test_find_nested_property_binding()
        {
            var obj = new
            {
                X = new ObservableList <int>()
            };

            var mockObjectPropertyBinding = new Mock <IObjectPropertyBinding>();
            var mockBindingsFactory       = new Mock <IBindingsFactory>();

            mockObjectPropertyBinding
            .Setup(m => m.PropertyName)
            .Returns("X");

            mockObjectPropertyBinding
            .Setup(m => m.FindNestedBinding("0"))
            .Returns(mockObjectPropertyBinding.Object);

            mockBindingsFactory
            .Setup(m => m.CreateObjectBindings(obj))
            .Returns(new IObjectPropertyBinding[] { mockObjectPropertyBinding.Object });

            var testObject = new ObjectBinding(obj, mockBindingsFactory.Object);

            Assert.Equal(mockObjectPropertyBinding.Object, testObject.FindNestedBinding("X.0"));
        }
Exemplo n.º 3
0
		Control TitleBox()
		{
			var control = new TextArea { Size = new Size(300, -1) };
			var binding = new ObjectBinding<MessageBoxSection, string>(this, r => r.MessageBoxText, (r, val) => r.MessageBoxText = val);
			control.TextBinding.Bind(binding);
			return control;
		}
Exemplo n.º 4
0
		Control CaptionBox()
		{
			var control = new TextBox { Size = new Size(300, -1) };
			var binding = new ObjectBinding<MessageBoxSection, string>(this, r => r.MessageBoxCaption, (r, val) => r.MessageBoxCaption = val);
			control.TextBinding.Bind(binding);
			return control;
		}
Exemplo n.º 5
0
        public void test_find_property_binding()
        {
            var obj = new TestClass()
            {
                X = 15
            };

            var mockObjectPropertyBinding = new Mock <IObjectPropertyBinding>();
            var mockBindingsFactory       = new Mock <IBindingsFactory>();

            mockObjectPropertyBinding
            .Setup(m => m.PropertyName)
            .Returns("X");

            mockObjectPropertyBinding
            .Setup(m => m.FindNestedBinding(string.Empty))
            .Returns(mockObjectPropertyBinding.Object);

            mockBindingsFactory
            .Setup(m => m.CreateObjectBindings(obj))
            .Returns(new IObjectPropertyBinding[] { mockObjectPropertyBinding.Object });

            var testObject = new ObjectBinding(obj, mockBindingsFactory.Object);

            Assert.Equal(mockObjectPropertyBinding.Object, testObject.FindNestedBinding("X"));
        }
Exemplo n.º 6
0
		Control MessageBoxTypeCombo()
		{
			var control = new EnumComboBox<MessageBoxType>();
			var binding = new ObjectBinding<MessageBoxSection, MessageBoxType>(this, r => r.MessageBoxType, (r, val) => r.MessageBoxType = val);
			control.SelectedValueBinding.Bind(binding);
			return control;
		}
Exemplo n.º 7
0
        public void property_changing_is_propagated_from_child_node_after_reconnect()
        {
            var propertyName = "X";

            var obj = new TestClass();
            var childEventStream = new Subject <BoundPropertyChangingEventArgs>();

            InitMocks();
            InitTestObjectWithPropertyChangingStream(childEventStream, obj);

            var testObject = new ObjectBinding(obj, mockBindingsFactory.Object);

            List <BoundPropertyChangingEventArgs> events = new List <BoundPropertyChangingEventArgs>();

            testObject.PropertyChangingEventStream.Subscribe(ev => events.Add(ev));

            testObject.Disconnect();

            Assert.Empty(events);

            testObject.Connect(obj);

            childEventStream.OnNext(new BoundPropertyChangingEventArgs(propertyName, mockObjectPropertyBinding.Object));

            Assert.Equal(1, events.Count);
            Assert.Equal(propertyName, events[0].PropertyName);
            Assert.Equal(mockObjectPropertyBinding.Object, events[0].Binding);
        }
Exemplo n.º 8
0
        /// <inheritdoc />
        protected override void ProcessRecord()
        {
            switch (ParameterSetName)
            {
            case nameof(ParameterSets.ByInternalId):
                Identity = ObjectBinding <FieldSet> .FromId(Id);

                break;

            case nameof(ParameterSets.ByName):
                Identity = ObjectBinding <FieldSet> .FromName(Name);

                break;

            case nameof(ParameterSets.ByIdentity):
                break;
            }
            if (!this.GetSingleValue(Identity, out var identity))
            {
                return;
            }

            var orderedFields = new List <CustomField>();

            foreach (var field in Order)
            {
                if (!this.GetSingleValue(field, out var fieldItem))
                {
                    return;
                }
                orderedFields.Add(fieldItem);
            }

            WriteObject(ApiHelper.Instance.CustomFields.Reorder(identity, orderedFields));
        }
Exemplo n.º 9
0
		public DualBinding Bind(IndirectBinding controlBinding, object objectValue, IndirectBinding objectBinding, DualBindingMode mode = DualBindingMode.TwoWay, object defaultControlValue = null, object defaultContextValue = null)
		{
			var valueBinding = new ObjectBinding(objectValue, objectBinding) {
				SettingNullValue = defaultContextValue,
				GettingNullValue = defaultControlValue
			};
			return Bind(controlBinding, valueBinding, mode);
		}
Exemplo n.º 10
0
		public void IntPropertyShouldUpdate()
		{
			var bindObject = new BindObject { IntProperty = 0 };
			var binding = new ObjectBinding<int>(bindObject, "IntProperty");
			Assert.AreEqual(binding.DataValue, bindObject.IntProperty, "Data value should equal object value");
			bindObject.IntProperty = 1;
			Assert.AreEqual(binding.DataValue, bindObject.IntProperty, "Data value should equal object value");
		}
Exemplo n.º 11
0
		public void DoublePropertyShouldUpdate()
		{
			var bindObject = new BindObject { DoubleProperty = 0 };
			var binding = new ObjectBinding<double>(bindObject, "DoubleProperty");
			Assert.AreEqual(binding.DataValue, bindObject.DoubleProperty, "Data value should equal object value");
			bindObject.DoubleProperty = 1.2;
			Assert.AreEqual(binding.DataValue, bindObject.DoubleProperty, "Data value should equal object value");
		}
Exemplo n.º 12
0
        Control MessageBoxDefaultButtonCombo()
        {
            var control = new EnumComboBox <MessageBoxDefaultButton>();
            var binding = new ObjectBinding <MessageBoxSection, MessageBoxDefaultButton>(this, r => r.MessageBoxDefaultButton, (r, val) => r.MessageBoxDefaultButton = val);

            control.SelectedValueBinding.Bind(binding);
            return(control);
        }
Exemplo n.º 13
0
 /// <summary>
 /// Bind an ObjectBinding to a particular object.
 /// </summary>
 /// <param name="obj">The object to be bound.</param>
 /// <param name="binding">The binding to bind.</param>
 public static void Bind(this object obj, ObjectBinding binding)
 {
     if (bindings.ContainsKey(obj)) {
         bindings[obj].Add(binding);
     } else {
         bindings.Add(obj, new List<ObjectBinding>());
         bindings[obj].Add(binding);
     }
 }
Exemplo n.º 14
0
		public void StringPropertyShouldUpdate()
		{
			var bindObject = new BindObject { StringProperty = "Initial Value" };
			var binding = new ObjectBinding<string>(bindObject, "StringProperty");
			Assert.AreEqual(binding.DataValue, bindObject.StringProperty, "Data value should equal object value");
			bindObject.StringProperty = "Other Value";
			Assert.AreEqual(binding.DataValue, bindObject.StringProperty, "Data value should equal object value");
			bindObject.StringProperty = null;
			Assert.AreEqual(binding.DataValue, bindObject.StringProperty, "Data value should equal object value");
		}
Exemplo n.º 15
0
        Control AttachToParentCheckBox()
        {
            var control = new CheckBox {
                Text = "Attach to Parent Window"
            };
            var binding = new ObjectBinding <MessageBoxSection, bool>(this, r => r.AttachToParent, (r, val) => r.AttachToParent = val);

            control.CheckedBinding.Bind(binding);
            return(control);
        }
Exemplo n.º 16
0
		public void BoolPropertyShouldUpdate()
		{
			var bindObject = new BindObject { BoolProperty = true };
			var binding = new ObjectBinding<bool>(bindObject, "BoolProperty");
			Assert.AreEqual(binding.DataValue, bindObject.BoolProperty, "Data value should equal object value");
			bindObject.BoolProperty = false;
			Assert.AreEqual(binding.DataValue, bindObject.BoolProperty, "Data value should equal object value");
			bindObject.BoolProperty = true;
			Assert.AreEqual(binding.DataValue, bindObject.BoolProperty, "Data value should equal object value");
		}
Exemplo n.º 17
0
        Control TitleBox()
        {
            var control = new TextArea {
                Size = new Size(300, -1)
            };
            var binding = new ObjectBinding <MessageBoxSection, string>(this, r => r.MessageBoxText, (r, val) => r.MessageBoxText = val);

            control.TextBinding.Bind(binding);
            return(control);
        }
Exemplo n.º 18
0
        Control CaptionBox()
        {
            var control = new TextBox {
                Size = new Size(300, -1)
            };
            var binding = new ObjectBinding <MessageBoxSection, string>(this, r => r.MessageBoxCaption, (r, val) => r.MessageBoxCaption = val);

            control.TextBinding.Bind(binding);
            return(control);
        }
Exemplo n.º 19
0
        /// <summary>
        /// Stop monitoring object graph.
        /// </summary>
        public void Disconnect()
        {
            if (binding == null)
            {
                return;
            }

            binding.Disconnect();

            binding = null;
        }
Exemplo n.º 20
0
        public void IntPropertyShouldUpdate()
        {
            var bindObject = new BindObject {
                IntProperty = 0
            };
            var binding = new ObjectBinding <int>(bindObject, "IntProperty");

            Assert.AreEqual(binding.DataValue, bindObject.IntProperty, "Data value should equal object value");
            bindObject.IntProperty = 1;
            Assert.AreEqual(binding.DataValue, bindObject.IntProperty, "Data value should equal object value");
        }
Exemplo n.º 21
0
        public void DoublePropertyShouldUpdate()
        {
            var bindObject = new BindObject {
                DoubleProperty = 0
            };
            var binding = new ObjectBinding <double>(bindObject, "DoubleProperty");

            Assert.AreEqual(binding.DataValue, bindObject.DoubleProperty, "Data value should equal object value");
            bindObject.DoubleProperty = 1.2;
            Assert.AreEqual(binding.DataValue, bindObject.DoubleProperty, "Data value should equal object value");
        }
Exemplo n.º 22
0
 /// <summary>
 /// Bind an ObjectBinding to a particular object.
 /// </summary>
 /// <param name="obj">The object to be bound.</param>
 /// <param name="binding">The binding to bind.</param>
 public static void Bind(this object obj, ObjectBinding binding)
 {
     if (bindings.ContainsKey(obj))
     {
         bindings[obj].Add(binding);
     }
     else
     {
         bindings.Add(obj, new List <ObjectBinding>());
         bindings[obj].Add(binding);
     }
 }
Exemplo n.º 23
0
        public void BoolPropertyShouldUpdate()
        {
            var bindObject = new BindObject {
                BoolProperty = true
            };
            var binding = new ObjectBinding <bool>(bindObject, "BoolProperty");

            Assert.AreEqual(binding.DataValue, bindObject.BoolProperty, "Data value should equal object value");
            bindObject.BoolProperty = false;
            Assert.AreEqual(binding.DataValue, bindObject.BoolProperty, "Data value should equal object value");
            bindObject.BoolProperty = true;
            Assert.AreEqual(binding.DataValue, bindObject.BoolProperty, "Data value should equal object value");
        }
Exemplo n.º 24
0
        public void StringPropertyShouldUpdate()
        {
            var bindObject = new BindObject {
                StringProperty = "Initial Value"
            };
            var binding = new ObjectBinding <string>(bindObject, "StringProperty");

            Assert.AreEqual(binding.DataValue, bindObject.StringProperty, "Data value should equal object value");
            bindObject.StringProperty = "Other Value";
            Assert.AreEqual(binding.DataValue, bindObject.StringProperty, "Data value should equal object value");
            bindObject.StringProperty = null;
            Assert.AreEqual(binding.DataValue, bindObject.StringProperty, "Data value should equal object value");
        }
Exemplo n.º 25
0
 /// <summary>
 /// Validate that a binding has one and exactly one value.
 /// </summary>
 /// <param name="cmdlet">The cmdlet to operate on.</param>
 /// <param name="binding">The binding to validate.</param>
 /// <param name="queryType">What type of query was it?</param>
 /// <param name="value">What was the query value?</param>
 /// <typeparam name="T">The type of object the binding is for.</typeparam>
 /// <returns>True if the binding is valid, else false.</returns>
 internal static bool ValidateHasExactlyOneValue <T>(this Cmdlet cmdlet, ObjectBinding <T> binding, string queryType = "query", string value = null)
     where T : AbstractBaseModel, new()
 {
     value = value ?? binding.Query;
     if (!binding.HasValue)
     {
         cmdlet.WriteNotFoundError <T>(queryType, value, binding.Error);
         return(false);
     }
     if (binding.Value.Count > 1)
     {
         cmdlet.WriteTooManyFoundError <T>(queryType, value);
         return(false);
     }
     return(true);
 }
Exemplo n.º 26
0
 /// <summary>
 /// Retrieve a single value from a binding.
 /// </summary>
 /// <param name="cmdlet">The cmdlet to operate on.</param>
 /// <param name="binding">The binding to use.</param>
 /// <param name="value">The value to output.</param>
 /// <param name="queryType">The type of query.</param>
 /// <param name="queryValue">The value of the query.</param>
 /// <typeparam name="R">The type of object to retrieve</typeparam>
 /// <returns>True if a valid value was retrieved.</returns>
 internal static bool GetSingleValue <R>(this Cmdlet cmdlet, ObjectBinding <R> binding, out R value, string queryType = "identity", string queryValue = null)
     where R : AbstractBaseModel, new()
 {
     value = null;
     if (null == binding)
     {
         cmdlet.WriteApiError(new ArgumentNullException(paramName: nameof(binding)));
         return(false);
     }
     if (null != binding.Error)
     {
         cmdlet.WriteApiError(binding.Error, target: binding.Query.ToString());
         return(false);
     }
     if (cmdlet.ValidateHasExactlyOneValue(binding, queryType, queryValue))
     {
         value = binding.Value[0];
         return(true);
     }
     return(false);
 }
Exemplo n.º 27
0
        /// <inheritdoc />
        protected override void ProcessRecord()
        {
            switch (ParameterSetName)
            {
            case nameof(ParameterSets.ByInternalId):
                Identity = ObjectBinding <FieldSet> .FromId(Id);

                break;

            case nameof(ParameterSets.ByName):
                Identity = ObjectBinding <FieldSet> .FromName(Name);

                break;

            case nameof(ParameterSets.ByIdentity):
                break;
            }
            if (!Identity.HasValue)
            {
                WriteError(new ErrorRecord(Identity.Error, $"{nameof(FieldSet)} not found: {Identity.Query}", ErrorCategory.InvalidArgument, null));
                return;
            }
            if (Identity.Value.Count > 1)
            {
                WriteError(new ErrorRecord(Identity.Error, $"More than one {nameof(FieldSet)} was found: {Identity.Query}", ErrorCategory.InvalidArgument, null));
                return;
            }

            // validate data before proceeding.
            if (Add != null && Add.Length > 0)
            {
                foreach (var field in Add)
                {
                    if (!field.HasValue)
                    {
                        WriteError(new ErrorRecord(field.Error, $"{nameof(CustomField)} was not found: {field.Query}", ErrorCategory.InvalidArgument, null));
                        return;
                    }
                    if (field.Value.Count > 1)
                    {
                        WriteError(new ErrorRecord(field.Error, $"More than one {nameof(CustomField)} was found: {field.Query}", ErrorCategory.InvalidArgument, null));
                        return;
                    }
                }
            }

            if (AddRequired != null && AddRequired.Length > 0)
            {
                foreach (var field in AddRequired)
                {
                    if (!field.HasValue)
                    {
                        WriteError(new ErrorRecord(field.Error, $"{nameof(CustomField)} was not found: {field.Query}", ErrorCategory.InvalidArgument, null));
                        return;
                    }
                    if (field.Value.Count > 1)
                    {
                        WriteError(new ErrorRecord(field.Error, $"More than one {nameof(CustomField)} was found: {field.Query}", ErrorCategory.InvalidArgument, null));
                        return;
                    }
                }
            }

            if (Remove != null && Remove.Length > 0)
            {
                foreach (var field in Remove)
                {
                    if (!field.HasValue)
                    {
                        WriteError(new ErrorRecord(field.Error, $"{nameof(CustomField)} was not found: {field.Query}", ErrorCategory.InvalidArgument, null));
                        return;
                    }
                    if (field.Value.Count > 1)
                    {
                        WriteError(new ErrorRecord(field.Error, $"More than one {nameof(CustomField)} was found: {field.Query}", ErrorCategory.InvalidArgument, null));
                        return;
                    }
                }
            }

            var value = Identity.Value[0];

            // populate record
            PopulateItem(value);

            // modify fields
            if (Add != null && Add.Length > 0)
            {
                foreach (var field in Add)
                {
                    //TODO: error handling
                    ApiHelper.Instance.CustomFields.Associate(field.Value[0], value, required: false);
                }
            }

            if (AddRequired != null && AddRequired.Length > 0)
            {
                foreach (var field in AddRequired)
                {
                    //TODO: error handling
                    ApiHelper.Instance.CustomFields.Associate(field.Value[0], value, required: true);
                }
            }

            if (Remove != null && Remove.Length > 0)
            {
                foreach (var field in Remove)
                {
                    //TODO: error handling
                    ApiHelper.Instance.CustomFields.Disassociate(field.Value[0], value);
                }
            }

            //TODO: error handling
            WriteObject(ApiHelper.Instance.FieldSets.Update(value));
        }
Exemplo n.º 28
0
 /// <summary>
 /// Removes the first instance of the specefied binding from the objects bindings.
 /// </summary>
 /// <param name="obj">The object to remove the binding from.</param>
 /// <param name="binding">The binding to remove.</param>
 public static void RemoveBinding(this object obj, ObjectBinding binding)
 {
     if (bindings.ContainsKey(obj)) {
         bindings[obj].Remove(binding);
     }
 }
Exemplo n.º 29
0
        /// <inheritdoc />
        protected override void ProcessRecord()
        {
            ObjectBinding <TObject> Object;

            switch (ParameterSetName)
            {
            case nameof(ParameterSets.ByInternalId):
                Object = ObjectBinding <TObject> .FromId(Id);

                if (Overwrite)
                {
                    // We need to reset if we're overwriting so we don't just erase fields
                    // if DisableLookupVerification is on
                    Object.Reset();
                }
                break;

            case nameof(ParameterSets.ByName):
                Object = ObjectBinding <TObject> .FromName(Name);

                // no need to reset here, FromName always fetches the full object
                break;

            case nameof(ParameterSets.ByIdentity):
                Object = Identity;
                // We need to reset if we're overwriting so we don't just erase fields
                // if DisableLookupVerification is on
                Object.Reset();
                break;

            default:
                Object = GetBoundObject();
                // we'll assume that there's no need to reset here.
                if (null == Object)
                {
                    this.WriteNotFoundError <TObject>("query", null);
                    return;
                }
                break;
            }

            // any errors that occur during lookup will be written to the error stream, and GetSingleValue will return false
            if (!this.GetSingleValue(Object, out var value))
            {
                return;
            }

            if (!PopulateItem(value))
            {
                return;
            }

            //TODO: error handling
            var response = Overwrite ? ApiHelper.Instance.GetEndPoint <TObject>().Set(value)     // write all fields
                                     : ApiHelper.Instance.GetEndPoint <TObject>().Update(value); // write only modified fields

            if (PassThru)
            {
                WriteObject(response);
            }
        }
Exemplo n.º 30
0
		public DualBinding Bind(IndirectBinding controlBinding, DirectBinding valueBinding, DualBindingMode mode = DualBindingMode.TwoWay)
		{
			var binding = new ObjectBinding(this, controlBinding);
			return binding.Bind(valueBinding: valueBinding, mode: mode);
		}
Exemplo n.º 31
0
		public DualBinding Bind(IndirectBinding controlBinding, IndirectBinding dataContextBinding, DualBindingMode mode = DualBindingMode.TwoWay, object defaultControlValue = null, object defaultContextValue = null)
		{
			var binding = new ObjectBinding(this, controlBinding);
			return binding.Bind(dataContextBinding, mode, defaultControlValue, defaultContextValue);
		}
Exemplo n.º 32
0
		Control AttachToParentCheckBox()
		{
			var control = new CheckBox { Text = "Attach to Parent Window" };
			var binding = new ObjectBinding<MessageBoxSection, bool>(this, r => r.AttachToParent, (r, val) => r.AttachToParent = val);
			control.CheckedBinding.Bind(binding);
			return control;
		}
Exemplo n.º 33
0
        public BindingManager(object bindingObject)
        {
            Argument.NotNull(() => bindingObject);

            this.binding = new ObjectBinding(bindingObject, new BindingsFactory());
        }
Exemplo n.º 34
0
 /// <summary>
 /// Initializes the test object.
 /// </summary>
 private void InitTestObject(TestClass obj)
 {
     testObject = new ObjectBinding(obj, mockBindingsFactory.Object);
 }