/// <summary> /// Создает объект Модели и два параметра, связанных с ней /// </summary> private void CreateModel() { var dataObject = new DataObject { Id = 0, Name = "Model" }; _dataObjectService.AddObject(dataObject); var bindingRight = new Binding { Name = "bindingRight", Object = (long)dataObject.Id, Property = "rightTemperature", Transformation = new Transformation { Min = 30, Max = 60, } }; var bindingLeft = new Binding { Name = "bindingLeft", Object = (long)dataObject.Id, Property = "leftTemperature", Transformation = new Transformation { Min = 40, Max = 60 } }; var bindingDown = new Binding() { Name = "bindingDown", Object = (long)dataObject.Id, Property = "downTemperature", Transformation = new Transformation { Min = 10, Max = 40 } }; _bindingService.AddBinding(bindingRight); _bindingService.AddBinding(bindingLeft); _bindingService.AddBinding(bindingDown); }
/// <summary> /// Подготавливает данные для сервиса, который добавляет привязку параметра к объекту в хранилище; /// вызывает этот сервис; /// сохраняет новое состояние хранилища /// </summary> /// <param name="parameters"> параметры, характеризующие новую привязку (Binding), в виде JObject </param> /// <param name="socketId"> id сокета, от которого пришел запрос </param> /// <returns></returns> private bool AddBinding(JObject parameters, string socketId) { var store = _storeService.GetStore(socketId); var binding = JObjectToBinding(parameters); store = _bindingService.AddBinding(binding, store); _storeService.SetStore(socketId, store); return(true); }
public IActionResult Add([FromForm] Binding binding) { JsonResult result; try { _bindingService.AddBinding(binding); result = Json(Ok()); } catch (BadRequestException e) { result = Json(BadRequest(e.Message)); } catch (Exception e) { _logger.LogError(e.Message); return(Json(BadRequest())); } return(result); }
public TInstance CreateObject <TInstance>(XxElement element, bool resolveBindings = true) { const string TypeNameProperty = "TypeName"; var type = element.Type; var typeDef = element.Properties.FirstOrDefault(o => o.Key.Name == TypeNameProperty); var typeName = typeDef.Value as string; if (!string.IsNullOrEmpty(typeName)) { type = Type.GetType(typeName.Replace(';', ',')) ?? type; } if (!typeof(TInstance).IsAssignableFrom(type)) { throw new InvalidDataException($"Cannot create {typeof(TInstance).Name} from {type.Name}!"); } var instance = objectFactory.Create(type, this, elementTypeMapping); if (instance is IStoreElement storeElement) { storeElement.Element = element; } var properties = GetProperties(element.Type, element.Properties); foreach (var prop in properties) { if (prop.Value is XxBindingString binding) { if (prop.Key.GetCustomAttribute <XxBindingStringAttribute>() != null || !resolveBindings) { prop.Key.SetValue(instance, $"{{{binding.Value}}}"); } else { bindingService.AddBinding(instance, prop.Key, binding.Value); } } else { if (prop.Key.PropertyType.IsAssignableFrom(prop.Value.GetType())) { prop.Key.SetValue(instance, prop.Value); } else { var constructor = prop.Key.PropertyType.GetConstructor(new Type[] { prop.Value.GetType() }); if (constructor != null) { var obj = constructor.Invoke(new[] { prop.Value }); prop.Key.SetValue(instance, obj); } } } } bool childrenAsDefinitions = element.Type.GetCustomAttribute <ChildrenAsDefinitionsAttribute>() != null; if (element.Children != null) { if (instance is IElementsContainer container) { var children = new List <object>(); foreach (var childElement in element.Children) { if (childrenAsDefinitions) { children.Add(childElement); } else { var child = CreateObject <object>(childElement, !childrenAsDefinitions && resolveBindings); children.Add(child); } } container.InitChildren(children); } else { throw new InvalidOperationException($"Cannot add child elements to {element.Type.Name}"); } } return((TInstance)instance); }