public override void Connect() { if (viewModelPropertyWatcher != null) { // Already connected - no need to connect again. return; } string propertyName; object parentViewModel; ParseViewModelEndPointReference(viewModelPropertyName, out propertyName, out parentViewModel); viewModelPropertyWatcher = new PropertyWatcher(parentViewModel, propertyName, NotifyPropertyChanged_PropertyChanged); UpdateViewModel(); }
/// <summary> /// Connect to the attached view model. /// </summary> public override void Connect() { Disconnect(); string propertyName; ParseViewModelEndPointReference(viewModelPropertyName, out propertyName, out viewModel); viewModelPropertyWatcher = new PropertyWatcher(viewModel, propertyName, InitalizeTemplate); // Get property from view model. viewModelProperty = viewModel.GetType().GetProperty(propertyName); if (viewModelProperty == null) { throw new ApplicationException("Expected property " + viewModelPropertyName + ", but was not found."); } InitalizeTemplate(); }
/// <summary> /// Connect to the attached view model. /// </summary> public override void Connect() { Disconnect(); string propertyName; ParseViewModelEndPointReference(viewModelPropertyName, out propertyName, out viewModel); viewModelPropertyWatcher = new PropertyWatcher(viewModel, propertyName, InitalizeTemplate); // Get property from view model. viewModelProperty = viewModel.GetType().GetProperty(propertyName); if (viewModelProperty == null) { throw new MemberNotFoundException( string.Format("Expected property {0} on type {1}, but was not found.", propertyName, viewModel.GetType().Name) ); } InitalizeTemplate(); }
public static Task <T> InsertAndFetchAsync <T>(this DbContext context, PropertyWatcher <T> Saved) { string table = GetTableName(typeof(T), context); var vals = Saved.GetValues().ToArray(); if (vals.Length == 0) { string query = string.Format("INSERT INTO {0} OUTPUT INSERTED.* DEFAULT VALUES", table); return(context.Database.SqlQuery <T>(query).FirstAsync()); } else { string query = string.Format("INSERT INTO {0} ({1}) OUTPUT INSERTED.* VALUES ({2})", table, string.Join(", ", vals.Select(c => GetColumnName(c.Key))), string.Join(", ", Enumerable.Range(0, vals.Length).Select(c => "@p" + c))); return(context.Database.SqlQuery <T>(query, vals.Select(c => GetParameter(c.Value)).ToArray()).FirstAsync()); } }
public static void Update <T, T2>(this DbContext context, PropertyWatcher <T> Saved, Expression <Func <T, T2> > KeyField, T2 KeyValue) where T : class, new() { var ret = new T(); Saved.LoadToInstance(ref ret); var prop = Reflection.GetPropertyInfo(KeyField); prop.SetValue(ret, KeyValue); context.Set <T>().Attach(ret); var updated = context.Entry <T>(ret); foreach (var v in Saved.GetValues()) { updated.Property(v.Key.Name).IsModified = true; } context.Configuration.ValidateOnSaveEnabled = false; }
public override void Connect() { Disconnect(); string propertyName; object newViewModel; ParseViewModelEndPointReference( ViewModelPropertyName, out propertyName, out newViewModel ); viewModel = newViewModel; viewModelPropertyWatcher = new PropertyWatcher( newViewModel, propertyName, NotifyPropertyChanged_PropertyChanged ); BindCollection(); }
public static Task <T2> InsertAsync <T, T2>(this DbContext context, PropertyWatcher <T> Saved, Expression <Func <T, T2> > OutputColumn) { string table = GetTableName(typeof(T), context); var vals = Saved.GetValues().ToArray(); var outputColumnName = GetColumnName(Reflection.GetPropertyInfo(OutputColumn)); if (vals.Length == 0) { string query = string.Format("INSERT INTO {0} OUTPUT INSERTED.{1} DEFAULT VALUES", table, outputColumnName); return(context.Database.SqlQuery <T2>(query).FirstAsync()); } else { string query = string.Format("INSERT INTO {0} ({1}) OUTPUT INSERTED.{2} VALUES ({3})", table, string.Join(", ", vals.Select(c => GetColumnName(c.Key))), outputColumnName, string.Join(", ", Enumerable.Range(0, vals.Length).Select(c => "@p" + c))); return(context.Database.SqlQuery <T2>(query, vals.Select((c, i) => GetParameter(c.Value)).ToArray()).FirstAsync()); } }
public override void Connect() { string propertyName; Component view; ParseViewEndPointReference(viewPropertyName, out propertyName, out view); var viewModelEndPoint = MakeViewModelEndPoint(viewModelPropertyName, null, null); var propertySync = new PropertySync( // Source viewModelEndPoint, // Dest new PropertyEndPoint( view, propertyName, TypeResolver.GetAdapter(ViewAdapterId), viewAdapterOptions, "view", this ), // Errors, exceptions and validation. null, // Validation not needed this ); viewModelWatcher = viewModelEndPoint.Watch( () => propertySync.SyncFromSource() ); // Copy the initial value over from the view-model. propertySync.SyncFromSource(); }
public override void Connect() { var viewModelEndPoint = MakeViewModelEndPoint(viewModelPropertyName, null, null); Assert.IsTrue( viewModelEndPoint.GetValue() is bool, "ToggleActiveBinding can only be bound to a boolean property." ); var propertySync = new PropertySync( // Source viewModelEndPoint, // Dest new PropertyEndPoint( this, "ChildrenActive", CreateAdapter(viewAdapterTypeName), viewAdapterOptions, "view", this ), // Errors, exceptions and validation. null, // Validation not needed this ); viewModelWatcher = viewModelEndPoint.Watch( () => propertySync.SyncFromSource() ); // Copy the initial value over from the view-model. propertySync.SyncFromSource(); }
public override void Connect() { dropdown = GetComponent <Dropdown>(); var selectionPropertyEndPoint = MakeViewModelEndPoint(viewModelSelectionPropertyName, selectionUIToViewModelAdapter, null); var selectionPropertySync = new PropertySync( // Source selectionPropertyEndPoint, // Dest new PropertyEndPoint( this, "SelectedOption", CreateAdapter(selectionViewModelToUIAdapter), null, "view", this ), // Errors, exceptions and validation. !string.IsNullOrEmpty(exceptionPropertyName) ? MakeViewModelEndPoint(exceptionPropertyName, exceptionAdapterTypeName, null) : null , this ); selectionPropertyWatcher = selectionPropertyEndPoint .Watch(() => selectionPropertySync.SyncFromSource()); selectionEventWatcher = new UnityEventWatcher( dropdown, "onValueChanged", () => { selectedOption = Options[dropdown.value]; // Copy value back from dropdown. selectionPropertySync.SyncFromDest(); } ); var optionsPropertySync = new PropertySync( // Source MakeViewModelEndPoint(viewModelOptionsPropertyName, null, null), // Dest new PropertyEndPoint( this, "Options", CreateAdapter(optionsAdapter), null, "view", this ), // Errors, exceptions and validation. null, // Validation not needed this ); // Copy the initial value from view-model to view. selectionPropertySync.SyncFromSource(); optionsPropertySync.SyncFromSource(); UpdateOptions(); }
public override void Connect() { if (boundAnimator == null) { boundAnimator = GetComponent <Animator>(); } Assert.IsTrue( boundAnimator != null, "Animator is null!" ); Assert.IsTrue( !string.IsNullOrEmpty(AnimatorParameterName), "AnimatorParameter is not set" ); string propertyName; switch (AnimatorParameterType) { case AnimatorControllerParameterType.Float: propertyName = "FloatParameter"; break; case AnimatorControllerParameterType.Int: propertyName = "IntParameter"; break; case AnimatorControllerParameterType.Bool: propertyName = "BoolParameter"; break; case AnimatorControllerParameterType.Trigger: propertyName = "TriggerParameter"; break; default: throw new IndexOutOfRangeException("Unexpected animator parameter type"); } var viewModelEndPoint = MakeViewModelEndPoint(viewModelPropertyName, null, null); // If the binding property is an AnimatorParameterTrigger, // we change the owner to the instance of the property // and change the property to "TriggerSetOrReset" if (AnimatorParameterType == AnimatorControllerParameterType.Trigger) { viewModelEndPoint = new PropertyEndPoint(viewModelEndPoint.GetValue(), "TriggerSetOrReset", null, null, "view-model", this); } var propertySync = new PropertySync( // Source viewModelEndPoint, // Dest new PropertyEndPoint( this, propertyName, CreateAdapter(viewAdapterTypeName), viewAdapterOptions, "Animator", this ), // Errors, exceptions and validation. null, // Validation not needed this ); viewModelWatcher = viewModelEndPoint.Watch( () => propertySync.SyncFromSource() ); // Copy the initial value over from the view-model. propertySync.SyncFromSource(); }
public static async Task UpdateAsync <T>(this DbContext context, IQueryable <T> query, PropertyWatcher <T> Saved) where T : class, new() { //var itemParam = Expression.Parameter(typeof(T), "x"); //var primaryKeys = Utilities.GetPrimaryKeys(typeof(T), context); //var primaryKeyArgs = primaryKeys.Select(c => Expression.PropertyOrField(itemParam, c.Name)).ToArray(); //var selector = Expression.Call(typeof(Tuple), "Create", primaryKeyArgs.Select(c => c.Type).ToArray(), primaryKeyArgs); //var lambda = Expression.Lambda<Func<T, object>>(selector, itemParam); //foreach (var item in query.Select(lambda.Compile())) //{ // T ret = new T(); // foreach (var v in primaryKeys.Select((c, index) => new { c, index })) // { // var tupleProp = item.GetType().GetProperty("Item" + v.index).GetValue(item); // v.c.SetValue(ret, tupleProp); // } // var updated = context.Entry(ret); // Saved.LoadToInstance(ref ret); // foreach (var v in Saved.GetValues()) // { // updated.Property(v.Key.Name).IsModified = true; // } //} await query.ForEachAsync(item => { var ret = item; var updated = context.Entry(ret); Saved.LoadToInstance(ref ret); foreach (var v in Saved.GetValues()) { updated.Property(v.Key.Name).IsModified = true; } }); context.Configuration.ValidateOnSaveEnabled = false; }