protected override void UpdateDataObject(object dataContext, object table, object oldDataObject, object newDataObject) { var dataServiceContext = (DataServiceContext)dataContext; var idataServiceContext = dataContext as IDataServiceContext; string etag = null; if (this._etagMap != null && this._etagMap.TryGetValue(DataServiceUtilities.BuildCompositeKey(oldDataObject), out etag)) { if (idataServiceContext != null) { idataServiceContext.AttachTo(TableName, newDataObject, etag); } else { dataServiceContext.AttachTo(TableName, newDataObject, etag); } } else { if (idataServiceContext != null) { idataServiceContext.AttachTo(TableName, newDataObject); } else { dataServiceContext.AttachTo(TableName, newDataObject); } } ProcessForeignKeys(dataServiceContext, newDataObject, _values); dataServiceContext.UpdateObject(newDataObject); DataServiceResponse dataServiceResponse = dataServiceContext.SaveChanges(); }
protected override void DeleteDataObject(object dataContext, object table, object oldDataObject) { var dataServiceContext = (DataServiceContext)dataContext; var idataServiceContext = dataContext as IDataServiceContext; string etag = null; if (this._etagMap != null && this._etagMap.TryGetValue(DataServiceUtilities.BuildCompositeKey(oldDataObject), out etag)) { if (idataServiceContext != null) { idataServiceContext.AttachTo(TableName, oldDataObject, etag); } else { dataServiceContext.AttachTo(TableName, oldDataObject, etag); } } else { if (idataServiceContext != null) { idataServiceContext.AttachTo(TableName, oldDataObject); } else { dataServiceContext.AttachTo(TableName, oldDataObject); } } dataServiceContext.DeleteObject(oldDataObject); dataServiceContext.SaveChanges(); }
// The logic that figures out the association is not yet ready. For now we use the second implementation, which // gets us going until we iron things out #if NOTYETREADY internal void Initialize() { if (DataServiceUtilities.IsEntityType(this.ColumnType, this.Table.DataModel.ContextType)) { TableProvider tp = this.Table.DataModel.Tables .FirstOrDefault(t => t.EntityType.IsAssignableFrom(this.ColumnType)); if (tp != null) { DataServiceColumnProvider nullColumn = new DataServiceColumnProvider(tp) { ColumnType = typeof(string), Name = "Null(" + this.Table.Name + "." + this.Name + ")" }; ((DataServiceTableProvider)tp).AddColumn(nullColumn); Association = new DataServiceAssociationProvider( AssociationDirection.ManyToOne, this, nullColumn, new List <string>() { this.Table.Name + "({0})/" + this.Name }); } } // not the best heuristic, but it'll do for now else if (!DataServiceUtilities.IsPrimitiveType(this.ColumnType) && typeof(IEnumerable).IsAssignableFrom(this.ColumnType) && this.ColumnType.GetGenericArguments().Length > 0 && DataServiceUtilities.IsEntityType(this.ColumnType.GetGenericArguments()[0], this.Table.DataModel.ContextType)) { TableProvider tp = this.Table.DataModel.Tables .FirstOrDefault(t => t.EntityType.IsAssignableFrom(this.ColumnType)); if (tp != null) { DataServiceColumnProvider nullColumn = new DataServiceColumnProvider(this.Table) { ColumnType = typeof(string), Name = "Null(" + this.Table.Name + "." + this.Name + ")" }; ((DataServiceTableProvider)tp).AddColumn(nullColumn); Association = new DataServiceAssociationProvider( AssociationDirection.ManyToMany, this, nullColumn, new List <string>() { this.Table.Name + "({0})/" + this.Name }); } } }
public static IEnumerable <PropertyInfo> EnumerateEntitySetProperties(Type contextType) { foreach (PropertyInfo prop in contextType.GetProperties()) { if (typeof(IQueryable).IsAssignableFrom(prop.PropertyType) && prop.PropertyType.GetGenericArguments().Length > 0 && DataServiceUtilities.IsEntityType(prop.PropertyType.GetGenericArguments()[0], contextType)) { yield return(prop); } } }
protected override object SaveViewState() { var myState = new Pair(); myState.First = base.SaveViewState(); if (_dataServiceContext != null) { myState.Second = _dataServiceContext.Entities.Where(ed => !String.IsNullOrEmpty(ed.ETag)).ToDictionary( ed => DataServiceUtilities.BuildCompositeKey(ed.Entity), ed => ed.ETag); } return(myState); }
public DataServiceColumnProvider(TableProvider table, PropertyInfo prop, bool isKey) : base(table) { _prop = prop; EntityTypeProperty = prop; Name = _prop.Name; ColumnType = _prop.PropertyType; IsPrimaryKey = isKey; IsSortable = true; IsGenerated = false; // We don't have a way of knowing this...could be an issue IsForeignKeyComponent = false; // No FKs in Astoria IsCustomProperty = false; // All properties are considered part of the contract in Astoria client Nullable = DataServiceUtilities.IsNullableType(ColumnType); }
public DataServiceDataModelProvider(Type contextType) { if (contextType == null) { throw new ArgumentNullException("contextType"); } this.ContextType = contextType; var tables = new Collection <TableProvider>(); foreach (PropertyInfo prop in DataServiceUtilities.EnumerateEntitySetProperties(contextType)) { tables.Add(new DataServiceTableProvider(this, prop)); } this._tables = new ReadOnlyCollection <TableProvider>(tables); foreach (var table in tables) { ((DataServiceTableProvider)table).Initialize(); } }
private void ProcessForeignKeys(DataServiceContext dataServiceContext, object dataObject, IDictionary values) { foreach (string key in values.Keys) { // Check if it looks like a FK, e.g. Category.CategoryID string[] parts = key.Split('.'); if (parts.Length != 2) { continue; } // Get the name of the entity ref property, e.g. Category string entityRefPropertyName = parts[0]; // Create an 'empty' relaty entity, e.g a Category PropertyInfo propInfo = dataObject.GetType().GetProperty(entityRefPropertyName); object entityRefObject = Activator.CreateInstance(propInfo.PropertyType); // Set the PK in the related entity, e.g. set the CategoryID in the Category PropertyInfo subPropInfo = propInfo.PropertyType.GetProperty(parts[1]); subPropInfo.SetValue( entityRefObject, Convert.ChangeType(values[key], subPropInfo.PropertyType), null); // Find the entity set property for the association var entitySetProp = DataServiceUtilities.FindEntitySetProperty( dataServiceContext.GetType(), propInfo.PropertyType); // Attach the related entity and set it as the link on the main entity if (entitySetProp != null) { dataServiceContext.AttachTo(entitySetProp.Name, entityRefObject); dataServiceContext.SetLink(dataObject, entityRefPropertyName, entityRefObject); } } }
private bool IsKeyColumn(PropertyInfo columnProp) { return(DataServiceUtilities.IsKeyColumn(columnProp)); }