/// <summary> /// Default constructor /// </summary> /// <param name="queryMap">The query map</param> /// <param name="type">The type that contains the <see cref="PropertyInfo"/></param> /// <param name="property">The property that's getting map.</param> /// <param name="column"> /// The column. /// NOTE: The column will be <see cref="null"/> when the <see cref="PropertyInfo"/> is a navigation property! /// </param> public PropertyMap(QueryMap queryMap, Type type, PropertyInfo property, IDbProviderColumn column = null) : base() { Column = column; QueryMap = queryMap ?? throw new ArgumentNullException(nameof(queryMap)); Type = type ?? throw new ArgumentNullException(nameof(type)); PropertyInfo = property ?? throw new ArgumentNullException(nameof(property)); Table = QueryMap.Tables.First(x => x.TableName == queryMap.GetTableName(type)); }
/// <summary> /// Updates the specified <paramref name="model"/> /// </summary> /// <param name="model">The model</param> /// <returns></returns> public async Task <IFailable <object> > UpdateDataAsync(object model) { // Create the result var result = new Failable <object>(); try { // Get the type of the model var modelType = model.GetType(); // Get the name of the table var tableName = QueryMap.GetTableName(modelType); // Get the primary key column var primaryKeyColumn = QueryMap.Columns.First(x => x.TableName == tableName && x.IsPrimaryKey); // Get the primary key property var primaryKeyProperty = modelType.GetProperty(primaryKeyColumn.ColumnName); // Get the existing model var existingModel = await DbContext.FindAsync(modelType, primaryKeyProperty.GetValue(model)); // For every property of the model... // TODO: Better implementation by using AutoMapper! foreach (var property in modelType.GetProperties()) { // Update the property value of the existing model property.SetValue(existingModel, property.GetValue(model)); } // Mark the existing model for update DbContext.Update(existingModel); // Save the changes await DbContext.SaveChangesAsync(); } catch (Exception ex) { // Set the error result.ErrorMessage = ex.Message; } // Return the result return(result); }
/// <summary> /// Creates and returns a <see cref="QueryMap"/> from the specified <paramref name="dataModel"/>. /// NOTE: If the model is not set, then the default values are used! /// </summary> /// <param name="databaseOptions">The database options</param> /// <param name="database">The database that contains the mapped table</param> /// <param name="columns">The columns of the tables</param> /// <param name="tables">The tables</param> /// <param name="joins">The joins</param> /// <param name="dataModel">The data model</param> /// <returns></returns> public static QueryMap FromDataModel(BaseDatabaseOptionsDataModel databaseOptions, IDbProviderDatabase database, IEnumerable <IDbProviderTable> tables, IEnumerable <IDbProviderColumn> columns, IEnumerable <JoinMap> joins, QueryMapDataModel dataModel = null) { // Create the map var queryMap = new QueryMap(databaseOptions, database, tables, columns, joins); // If there is a data model... if (dataModel != null) { queryMap.Id = dataModel.Id; queryMap.Color = dataModel.Color; queryMap.Description = dataModel.Description; queryMap.Name = dataModel.Name; } var tableToTypeBuilderMapper = new Dictionary <IDbProviderTable, TypeBuilder>(); // For every table... foreach (var table in tables) { // Create the builder var typeBuilder = CeidDiplomatikiHelpers.GetTypeBuilder(queryMap.Id + "-" + table.TableName); // For every table column... foreach (var column in columns.Where(x => x.TableName == table.TableName)) { // Create a property TypeBuilderHelpers.CreateProperty(typeBuilder, column.ColumnName, column.DataType); } // Map it tableToTypeBuilderMapper.Add(table, typeBuilder); } // For every join... foreach (var join in joins) { // Get the principle model type builder var principleModelTypeBuilder = tableToTypeBuilderMapper.First(x => x.Key == join.Table).Value; // Get the referenced model type builder var referencedModelTypeBuilder = tableToTypeBuilderMapper.First(x => x.Key == join.ReferencedTable).Value; // Create the principle navigation property type var principleNavigationPropertyType = typeof(IEnumerable <>).MakeGenericType(referencedModelTypeBuilder); // Add it to the principle model TypeBuilderHelpers.CreateProperty(principleModelTypeBuilder, CeidDiplomatikiHelpers.GetPluralForm(join.ReferencedTable.TableName), principleNavigationPropertyType); // Add the foreign navigation property type TypeBuilderHelpers.CreateProperty(referencedModelTypeBuilder, join.Table.TableName, principleModelTypeBuilder); } var tableToTypeMapper = new Dictionary <IDbProviderTable, Type>(); // For every table to type builder map... foreach (var map in tableToTypeBuilderMapper) { // Build the type var type = map.Value.CreateType(); if (joins.Any(x => x.Index == 0 && x.Table.TableName == map.Key.TableName)) { queryMap.RootType = type; } else if (joins.Count() == 0) { queryMap.RootType = type; } // Map it tableToTypeMapper.Add(map.Key, type); } // Set the types to the query queryMap.DataModelTypes = tableToTypeMapper.Select(x => x.Value).ToList(); // Create the db context type builder var dbContextTypeBuilder = CeidDiplomatikiHelpers.GetTypeBuilder(Guid.NewGuid().ToString() + "-DbContext"); // Set the base type var baseType = typeof(PresenterDbContext); // Inherit from the PresenterDbContext dbContextTypeBuilder.SetParent(baseType); // For every data model type... foreach (var map in tableToTypeMapper) { // Create the DbSet type that will be set as a property to the DbContext var dbSetType = typeof(DbSet <>).MakeGenericType(map.Value); // Add it to the type TypeBuilderHelpers.CreateProperty(dbContextTypeBuilder, map.Key.TableName, dbSetType); } // Create the constructors TypeBuilderHelpers.CreatePassThroughConstructors(dbContextTypeBuilder, baseType); // Create the db context type queryMap.DbContextType = dbContextTypeBuilder.CreateType(); // If there is a data model... // NOTE: We create the column and the data presenter maps at the end of the initialization // because some properties of the query map are required to get set! if (dataModel != null) { dataModel.PropertyMaps.ForEach(model => queryMap.Add(PropertyMap.FromDataModel(queryMap, queryMap.DataModelTypes.First(x => queryMap.GetTableName(x) == model.TableName), dataModel.PropertyMaps.First(x => x.TableName == model.TableName && x.ColumnName == model.ColumnName)))); dataModel.DataGridPresenterMaps.ForEach(model => queryMap.Add(DataGridPresenterMap.FromDataModel(queryMap, model))); } // Return the map return(queryMap); }
/// <summary> /// Default constructor /// </summary> /// <param name="presenterMap">The data grid presenter map</param> public DataGridPresenterDataStorage(DataGridPresenterMap presenterMap) : base(presenterMap) { DataGridMap = PresenterMap.DataGrids.First(x => x.Type == QueryMap.RootType); PrimaryKeyColumn = QueryMap.Columns.First(x => x.IsPrimaryKey && x.TableName == QueryMap.GetTableName(QueryMap.RootType)); PrimaryKeyProperty = QueryMap.RootType.GetProperty(PrimaryKeyColumn.ColumnName); }