private void InitInfrastructureServices() { try { // Cargar todos los modelos de datos y verificar ensamblados DataModelDataAccess dataModelDataAccess = new DataModelDataAccess(); Collection <DataModelEntity> result = dataModelDataAccess.LoadWhere(DataModelEntity.DBIdStore, "0", false, OperatorType.Equal); dataModelDataAccess = new DataModelDataAccess(); Collection <DataModelEntity> dataModels = dataModelDataAccess.LoadAll(true); if (result.Count == 0) { ConsoleWriter.SetText("Creating mall data model"); DataModelEntity dataModel = new DataModelEntity(); MallEntity mall = new MallEntity(); mall.MallName = "Mall"; (new MallDataAccess()).Save(mall); dataModel.Mall = mall; dataModelDataAccess.Save(dataModel); } foreach (DataModelEntity dataModel in dataModels) { string assemblyFileName = dataModel.ServiceAssemblyFileName; if (assemblyFileName != null) { if (File.Exists(Path.Combine(ServiceBuilder.AssembliesFolder, assemblyFileName))) { Type[] servicesTypes = ServiceBuilder.GetInfrastructureServiceTypes(assemblyFileName); Binding serviceBinding = new BasicHttpBinding(); if (PublishInfrastructureService(servicesTypes[0], servicesTypes[1], serviceBinding)) { Debug.WriteLine("SUCCESS : infrastructure service published."); } else { Debug.WriteLine("FAILURE : trying to publish infrastructure service."); } } else { ServiceBuilder builder = new ServiceBuilder(); dataModel.Deployed = false; builder.BuildAndImplementInfrastructureService(dataModel, false, serverSession); } } } } catch (DataException dataError) { Debug.WriteLine("ERROR : Data exception running infrastructure services. MESSAGE : " + dataError.Message); } catch (IOException ioError) { Debug.WriteLine("ERROR : IO error running infrastructure services. MESSAGE : " + ioError.Message); } }
private void FillSaveParameters(MallEntity mall, IDbCommand sqlCommand) { IDbDataParameter parameter; parameter = dataAccess.GetNewDataParameter("@serverName", DbType.String); parameter.Value = mall.ServerName; if (String.IsNullOrEmpty(mall.ServerName)) { parameter.Value = DBNull.Value; } sqlCommand.Parameters.Add(parameter); parameter = dataAccess.GetNewDataParameter("@mallName", DbType.String); parameter.Value = mall.MallName; if (String.IsNullOrEmpty(mall.MallName)) { parameter.Value = DBNull.Value; } sqlCommand.Parameters.Add(parameter); }
/// <summary> /// Function to load a MallEntity from database. /// </summary> /// <param name="id">The ID of the record to load</param> /// <param name="loadRelation">if is true load the relation</param> /// <param name="scope">Internal structure used to avoid circular reference locks, must be provided if calling from other data access object</param> /// <returns>The entity instance</returns> /// <exception cref="UtnEmallDataAccessException"> /// If a DbException occurs while accessing the database. /// </exception> public MallEntity Load(int id, bool loadRelation, Dictionary <string, IEntity> scope) { // Build a key for internal scope object string scopeKey = id.ToString(NumberFormatInfo.InvariantInfo) + "Mall"; if (scope != null) { // If scope contains the object it was already loaded, // return it to avoid circular references if (scope.ContainsKey(scopeKey)) { return((MallEntity)scope[scopeKey]); } } else { // If there isn't a current scope create one scope = new Dictionary <string, IEntity>(); } MallEntity mall = null; // Check if the entity was already loaded by current data access object // and return it if that is the case if (inMemoryEntities.ContainsKey(id)) { mall = inMemoryEntities[id]; // Add current object to current load scope scope.Add(scopeKey, mall); } else { bool closeConnection = false; try { // Open a new connection if it isn't on a transaction if (dbConnection == null || dbConnection.State.CompareTo(ConnectionState.Closed) == 0) { closeConnection = true; dbConnection = dataAccess.GetNewConnection(); dbConnection.Open(); } string cmdText = "SELECT idMall, serverName, mallName, timestamp FROM [Mall] WHERE idMall = @idMall"; // Create the command IDbCommand sqlCommand = dataAccess.GetNewCommand(cmdText, dbConnection, dbTransaction); // Create the Id parameter for the query IDbDataParameter parameter = dataAccess.GetNewDataParameter("@idMall", DbType.Int32); parameter.Value = id; sqlCommand.Parameters.Add(parameter); // Use a DataReader to get data from db IDataReader reader = sqlCommand.ExecuteReader(); mall = new MallEntity(); if (reader.Read()) { // Load fields of entity mall.Id = reader.GetInt32(0); if (!reader.IsDBNull(1)) { mall.ServerName = reader.GetString(1); } if (!reader.IsDBNull(2)) { mall.MallName = reader.GetString(2); } // Add current object to the scope scope.Add(scopeKey, mall); // Add current object to cache of loaded entities inMemoryEntities.Add(mall.Id, mall); // Read the timestamp and set new and changed properties mall.Timestamp = reader.GetDateTime(3); mall.IsNew = false; mall.Changed = false; // Close the reader reader.Close(); // Load related objects if required if (loadRelation) { } } else { reader.Close(); } } catch (DbException dbException) { // Catch DBException and rethrow as custom exception throw new UtnEmallDataAccessException(dbException.Message, dbException); } finally { // Close connection if it was opened by ourself if (closeConnection) { dbConnection.Close(); } } } // Return the loaded entity return(mall); }
/// <summary> /// Function to Delete a MallEntity from database. /// </summary> /// <param name="mall">MallEntity to delete</param> /// <param name="scope">Internal structure to avoid circular reference locks. Must provide an instance while calling from other data access object.</param> /// <exception cref="ArgumentNullException"> /// If <paramref name="mall"/> is not a <c>MallEntity</c>. /// </exception> /// <exception cref="UtnEmallDataAccessException"> /// If an DbException occurs in the try block while accessing the database. /// </exception> public void Delete(MallEntity mall, Dictionary <string, IEntity> scope) { if (mall == null) { throw new ArgumentException("The argument can't be null"); } try { // Open connection and initialize a transaction if needed if (!isGlobalTransaction) { dbConnection = dataAccess.GetNewConnection(); dbConnection.Open(); dbTransaction = dbConnection.BeginTransaction(); } // Reload the entity to ensure deletion of older data mall = this.Load(mall.Id, true); if (mall == null) { throw new UtnEmallDataAccessException("Error retrieving data while trying to delete."); } // Create a command for delete string cmdText = "DeleteMall"; IDbCommand sqlCommand = dataAccess.GetNewCommand(cmdText, dbConnection, dbTransaction); sqlCommand.CommandType = CommandType.StoredProcedure; // Add values to parameters IDbDataParameter parameterID = dataAccess.GetNewDataParameter("@idMall", DbType.Int32); parameterID.Value = mall.Id; sqlCommand.Parameters.Add(parameterID); // Execute the command sqlCommand.ExecuteNonQuery(); // Delete related objects // Commit transaction if is mine if (!isGlobalTransaction) { dbTransaction.Commit(); } // Remove entity from loaded objects inMemoryEntities.Remove(mall.Id); // Remove entity from current internal scope if (scope != null) { string scopeKey = mall.Id.ToString(NumberFormatInfo.InvariantInfo) + "Mall"; scope.Remove(scopeKey); } } catch (DbException dbException) { // Rollback transaction if (!isGlobalTransaction) { dbTransaction.Rollback(); } // Rethrow as custom exception throw new UtnEmallDataAccessException(dbException.Message, dbException); } finally { // Close connection if it was initiated by this instance if (!isGlobalTransaction) { dbConnection.Close(); dbConnection = null; dbTransaction = null; } } }
/// <summary> /// Function to Delete a MallEntity from database. /// </summary> /// <param name="mall">MallEntity to delete</param> /// <exception cref="ArgumentNullException"> /// If <paramref name="mall"/> is not a <c>MallEntity</c>. /// </exception> /// <exception cref="UtnEmallDataAccessException"> /// If an DbException occurs in the try block while accessing the database. /// </exception> public void Delete(MallEntity mall) { Delete(mall, null); }
/// <summary> /// Function to Save a MallEntity in the database. /// </summary> /// <param name="mall">MallEntity to save</param> /// <param name="scope">Interna structure to avoid circular reference locks. Provide an instance when calling from other data access object.</param> /// <exception cref="ArgumentNullException"> /// If <paramref name="mall"/> is not a <c>MallEntity</c>. /// </exception> /// <exception cref="UtnEmallDataAccessException"> /// If an DbException occurs in the try block while accessing the database. /// </exception> public void Save(MallEntity mall, Dictionary <string, IEntity> scope) { if (mall == null) { throw new ArgumentException("The argument can't be null"); } // Create a unique key to identify the object in the internal scope string scopeKey = mall.Id.ToString(NumberFormatInfo.InvariantInfo) + "Mall"; if (scope != null) { // If it's on the scope return it, don't save again if (scope.ContainsKey(scopeKey)) { return; } } else { // Create a new scope if it's not provided scope = new Dictionary <string, IEntity>(); } try { // Open a DbConnection and a new transaction if it isn't on a higher level one if (!isGlobalTransaction) { dbConnection = dataAccess.GetNewConnection(); dbConnection.Open(); dbTransaction = dbConnection.BeginTransaction(); } string commandName = ""; bool isUpdate = false; // Check if it is an insert or update command if (mall.IsNew || !DataAccessConnection.ExistsEntity(mall.Id, "Mall", "idMall", dbConnection, dbTransaction)) { commandName = "SaveMall"; } else { isUpdate = true; commandName = "UpdateMall"; } // Create a db command IDbCommand sqlCommand = dataAccess.GetNewCommand(commandName, dbConnection, dbTransaction); sqlCommand.CommandType = CommandType.StoredProcedure; // Add parameters values to current command IDbDataParameter parameter; if (isUpdate) { parameter = dataAccess.GetNewDataParameter("@idMall", DbType.Int32); parameter.Value = mall.Id; sqlCommand.Parameters.Add(parameter); } FillSaveParameters(mall, sqlCommand); // Execute the command if (isUpdate) { sqlCommand.ExecuteNonQuery(); } else { IDbDataParameter parameterIdOutput = dataAccess.GetNewDataParameter("@idMall", DbType.Int32); parameterIdOutput.Direction = ParameterDirection.ReturnValue; sqlCommand.Parameters.Add(parameterIdOutput); sqlCommand.ExecuteNonQuery(); mall.Id = Convert.ToInt32(parameterIdOutput.Value, NumberFormatInfo.InvariantInfo); } scopeKey = mall.Id.ToString(NumberFormatInfo.InvariantInfo) + "Mall"; // Add entity to current internal scope scope.Add(scopeKey, mall); // Save collections of related objects to current entity // Save objects related to current entity // Update // Close transaction if initiated by me if (!isGlobalTransaction) { dbTransaction.Commit(); } // Update new and changed flags mall.IsNew = false; mall.Changed = false; } catch (DbException dbException) { // Rollback transaction if (!isGlobalTransaction) { dbTransaction.Rollback(); } // Rethrow as custom exception throw new UtnEmallDataAccessException(dbException.Message, dbException); } finally { // Close connection if initiated by me if (!isGlobalTransaction) { dbConnection.Close(); dbConnection = null; dbTransaction = null; } } }
/// <summary> /// Function to Save a MallEntity in the database. /// </summary> /// <param name="mall">MallEntity to save</param> /// <exception cref="ArgumentNullException"> /// if <paramref name="mall"/> is not a <c>MallEntity</c>. /// </exception> /// <exception cref="UtnEmallDataAccessException"> /// If an DbException occurs in the try block while accessing the database. /// </exception> public void Save(MallEntity mall) { Save(mall, null); }
public IActionResult Mall(MallEntity mall) { return(Ok(new { code = 0, msg = "处理成功", data = mall })); }