public OnSaveInstance(IModelHelperContext context) { _context = context; // The context contains various model events you can subscribe to. You will usually // want to unsubscribe from the event in the implementation of Dispose() _context.ModelSaved += _context_ModelSaved; }
public IDisposable CreateInstance(IModelHelperContext context) { return(new OnSaveInstance(context)); }
/// <summary> /// Called at the time the model is loaded. /// </summary> /// <param name="context"></param> /// <returns></returns> public IDisposable CreateInstance(IModelHelperContext context) { DirectConnectUtils.DirectConnectLogPath = string.Empty; if ((bool)context.PropertyValues.FirstOrDefault(p => p.Name == "LogToDisk").Value) { DirectConnectUtils.DirectConnectLogPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "SimioAPI-DirectConnect.log"); File.Delete(DirectConnectUtils.DirectConnectLogPath); } Logit($"Info: Creating ModelHelper Instance. Model={context.Model.Name} #Tables={context.Model.Tables.Count}"); Logit($"Info: Creating ModelHelper Instance. API Name={Name} Description={Description} Guid={UniqueID}"); // Setup lists to be used by this plugin DirectConnectUtils.NewModelSetup(context.Model); // get connect string var connectionString = context.PropertyValues.FirstOrDefault(p => p.Name == "ConnectionString"); if (connectionString?.Value != null) { Logit($"Info: CreateInstance. ConnectionString={connectionString.Value}"); try { if (string.IsNullOrEmpty(connectionString.Value.ToString())) { throw new Exception("ConnectionString Is Blank. Define connection string and re-enable model helper."); } DirectConnectUtils.SetConnectionAndConnectionString(connectionString.Value.ToString()); } catch (Exception ex) { Logit(ex.Message); } } // get connection TimeOut var connectionTimeOut = context.PropertyValues.FirstOrDefault(p => p.Name == "ConnectionTimeOut"); if (connectionTimeOut?.Value != null) { Logit($"Info: CreateInstance. ConnectionTimeOut={connectionTimeOut.Value}"); try { if (string.IsNullOrEmpty(connectionTimeOut.Value.ToString())) { throw new Exception("Connection TimeOut Is Blank. Define connection timeout and re-enable model helper."); } Int32 connectionTimeOutInt = 0; if (Int32.TryParse(connectionTimeOut.Value.ToString(), out connectionTimeOutInt)) { DirectConnectUtils.SetConnectionTimeOut(connectionTimeOutInt); } else { throw new Exception($"Connection TimeOut ({connectionTimeOut.Value}) Is Not An Integer. Redefine connection timeout and re-enable model helper."); } } catch (Exception ex) { Logit(ex.Message); } } // get date time format var dateTimeFormatProp = context.PropertyValues.FirstOrDefault(p => p.Name == "DateTimeFormat"); if (dateTimeFormatProp?.Value != null) { Logit($"CreateInstance. DateTimeFormat={dateTimeFormatProp.Value}"); try { if (string.IsNullOrEmpty(dateTimeFormatProp.Value.ToString())) { throw new Exception("DateTimeFormat string Is Blank. Define the DateTime Format String and re-enable model helper."); } DirectConnectUtils.SetDateTimeFormatString(dateTimeFormatProp.Value.ToString()); } catch (Exception ex) { Logit(ex.Message); } } return(new DirectConnectOnSaveInstance(context)); }