/// <summary> /// Raises the <see cref="E:Deleted"/> event. /// </summary> /// <param name="args">The <see cref="NBear.Web.Data.NBearDataSourceEventArgs"/> instance containing the event data.</param> internal void OnDeleted(NBearDataSourceEventArgs args) { if (Deleted != null) { Deleted(this, args); } }
/// <summary> /// Raises the <see cref="E:Saved"/> event. /// </summary> /// <param name="args">The <see cref="NBear.Web.Data.NBearDataSourceEventArgs"/> instance containing the event data.</param> internal void OnSaved(NBearDataSourceEventArgs args) { if (Saved != null) { Saved(this, args); } }
protected override int ExecuteInsert(IDictionary values) { Gateway gateway = owner.Gateway; Type type = Util.GetType(this.owner.TypeName); object entity = Activator.CreateInstance(type); foreach (string key in values.Keys) { PropertyInfo property = Util.DeepGetProperty(type, key); if (property.CanWrite) { property.SetValue(entity, ChangeType(values[key], property.PropertyType), null); } } NBearDataSourceEventArgs savingArgs = new NBearDataSourceEventArgs(entity); owner.OnSaving(savingArgs); entity = savingArgs.Entity; if (entity == null) { return(0); } System.Data.Common.DbTransaction tran = null; try { tran = gateway.BeginTransaction(); MethodInfo miSave = GetGatewayMethodInfo("Int32 Save[EntityType](EntityType, System.Data.Common.DbTransaction)"); miSave.MakeGenericMethod(type).Invoke(gateway, new object[] { entity, tran }); tran.Commit(); } catch { tran.Rollback(); throw; } finally { gateway.CloseTransaction(tran); } owner.OnSaved(new NBearDataSourceEventArgs(entity)); this.OnDataSourceViewChanged(EventArgs.Empty); return(1); }
protected override int ExecuteDelete(IDictionary keys, IDictionary oldValues) { Gateway gateway = owner.Gateway; Type type = Util.GetType(this.owner.TypeName); string keyMember = null; Type keyType = null; object keyValue = null; PropertyInfo keyMemberProperty = null; List <string> keyNames = new List <string>(); List <object> keyValues = new List <object>(); if (keys != null && keys.Count > 0) { IEnumerator en = keys.Keys.GetEnumerator(); while (en.MoveNext()) { keyMember = en.Current.ToString(); keyNames.Add(keyMember); keyMemberProperty = Util.DeepGetProperty(type, keyMember); keyValue = ChangeType(keys[keyMember], keyMemberProperty.PropertyType); keyValues.Add(keyValue); } } else { EntityConfiguration ec = MetaDataManager.GetEntityConfiguration(this.owner.TypeName); foreach (PropertyConfiguration pc in ec.Properties) { if (pc.IsPrimaryKey) { keyMember = pc.Name; keyNames.Add(keyMember); keyType = Util.GetType(pc.PropertyType); keyValue = ChangeType(oldValues[keyMember], keyType); keyValues.Add(keyValue); } } } MethodInfo miFind = GetGatewayMethodInfo("EntityType Find[EntityType](System.Object[])"); object entity = miFind.MakeGenericMethod(type).Invoke(gateway, new object[] { keyValues.ToArray() }); if (entity == null) { return(0); } //check DBConcurrency if (this.owner.ConflictDetection == ConflictOptions.CompareAllValues) { foreach (string key in oldValues.Keys) { if (keyNames.Contains(key)) { continue; } PropertyInfo property = Util.DeepGetProperty(type, key); if (property != null && property.GetValue(entity, null) != ChangeType(oldValues[key], property.PropertyType)) { throw new System.Data.DBConcurrencyException("The underlying data has changed."); } } } NBearDataSourceEventArgs deletingArgs = new NBearDataSourceEventArgs(entity); owner.OnDeleting(deletingArgs); entity = deletingArgs.Entity; if (entity == null) { return(0); } System.Data.Common.DbTransaction tran = null; try { tran = gateway.BeginTransaction(); MethodInfo miDelete = GetGatewayMethodInfo("Void Delete[EntityType](EntityType, System.Data.Common.DbTransaction)"); miDelete.MakeGenericMethod(type).Invoke(gateway, new object[] { entity, tran }); tran.Commit(); } catch { tran.Rollback(); throw; } finally { gateway.CloseTransaction(tran); } owner.OnDeleted(new NBearDataSourceEventArgs(entity)); this.OnDataSourceViewChanged(EventArgs.Empty); return(1); }
protected override int ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) { Gateway gateway = owner.Gateway; Type type = Util.GetType(this.owner.TypeName); string keyMember = null; Type keyType = null; object keyValue = null; PropertyInfo keyMemberProperty = null; List <string> keyNames = new List <string>(); Entity entity = (Entity)Activator.CreateInstance(type); if (keys != null && keys.Count > 0) { IEnumerator en = keys.Keys.GetEnumerator(); while (en.MoveNext()) { keyMember = en.Current.ToString(); keyNames.Add(keyMember); keyMemberProperty = Util.DeepGetProperty(type, keyMember); keyValue = ChangeType(keys[keyMember], keyMemberProperty.PropertyType); keyMemberProperty.SetValue(entity, keyValue, null); } } else { EntityConfiguration ec = MetaDataManager.GetEntityConfiguration(this.owner.TypeName); foreach (PropertyConfiguration pc in ec.Properties) { if (pc.IsPrimaryKey) { keyMember = pc.Name; keyNames.Add(keyMember); keyType = Util.GetType(pc.PropertyType); keyValue = ChangeType(oldValues[keyMember], keyType); keyMemberProperty = Util.DeepGetProperty(type, keyMember); keyMemberProperty.SetValue(entity, keyValue, null); } } } bool changed = false; entity.Attach(); foreach (string key in values.Keys) { if (keyNames.Contains(key)) { continue; } PropertyInfo property = Util.DeepGetProperty(type, key); if (this.owner.ConflictDetection == ConflictOptions.CompareAllValues) { if (property != null && property.GetValue(entity, null) != ChangeType(oldValues[key], property.PropertyType)) { throw new System.Data.DBConcurrencyException("The underlying data has changed."); } } if (values[key] == oldValues[key]) { continue; } if (property != null && property.CanWrite) { changed = true; property.SetValue(entity, ChangeType(values[key], property.PropertyType), null); } } if (changed) { NBearDataSourceEventArgs savingArgs = new NBearDataSourceEventArgs(entity); owner.OnSaving(savingArgs); entity = (Entity)savingArgs.Entity; if (entity == null) { return(0); } System.Data.Common.DbTransaction tran = null; try { tran = gateway.BeginTransaction(); MethodInfo miSave = GetGatewayMethodInfo("Int32 Save[EntityType](EntityType, System.Data.Common.DbTransaction)"); miSave.MakeGenericMethod(type).Invoke(gateway, new object[] { entity, tran }); tran.Commit(); } catch { tran.Rollback(); throw; } finally { gateway.CloseTransaction(tran); } owner.OnSaved(new NBearDataSourceEventArgs(entity)); this.OnDataSourceViewChanged(EventArgs.Empty); } return(changed ? 1 : 0); }