/// <summary> /// Initializes a new instance of ImmediateNotificationRegister class. /// </summary> /// <param name="context">an instance of DbContext is used to get an ObjectQuery object</param> /// <param name="query">an instance of IQueryable is used to get ObjectQuery object, and then get /// connection string and command string to register SqlDependency nitification. </param> public ImmediateNotificationRegister(DbContext context, IQueryable query) { try { this.iquery = query; // Get the ObjectQuery directly or convert the DbQuery to ObjectQuery. oquery = QueryExtension.GetObjectQuery <TEntity>(context, iquery); QueryExtension.GetSqlCommand(oquery, ref connection, ref command); RegisterSqlDependency(); } catch (ArgumentException ex) { if (ex.ParamName == "context") { throw new ArgumentException("Paramter cannot be null", "context", ex); } else { throw new ArgumentException("Paramter cannot be null", "query", ex); } } catch (Exception ex) { throw new Exception( "Fails to initialize a new instance of ImmediateNotificationRegister class.", ex); } }
/// <summary> /// Initializes a new instance of RegularlyNotificationRegister class. /// </summary> /// <param name="context">an instance of DbContext is used to get an ObjectQuery object</param> /// <param name="query">an instance of IQueryable is used to get ObjectQuery object, and then get /// connection string and command string to register SqlDependency nitification. </param> /// <param name="interval">The time interval between invocations of callback, in milliseconds.</param> public RegularlyNotificationRegister(DbContext context, IQueryable query, Int32 interval) { try { this.iquery = query; this.interval = interval; oquery = QueryExtension.GetObjectQuery <TEntity>(context, iquery); QueryExtension.GetSqlCommand(oquery, ref connection, ref command); RegisterSqlDependency(); } catch (ArgumentException ex) { if (ex.ParamName == "context") { throw new ArgumentException("Paramter cannot be null", "context", ex); } else { throw new ArgumentException("Paramter cannot be null", "query", ex); } } catch (Exception ex) { throw new Exception( "Fails to initialize a new instance of RegularlyNotificationRegister class.", ex); } }
/// <summary> /// Use ObjectQuery to get SqlConnection and SqlCommand. /// </summary> public static void GetSqlCommand <TEntity>(IDbContextBase context, IQueryable query, ref SqlConnection connection, ref SqlCommand command) where TEntity : class { var queryobject = GetObjectQuery <TEntity>(context, query); if (queryobject == null) { throw new System.ArgumentException("Paramter cannot be null", "queryobject"); } if (connection == null) { connection = new SqlConnection(QueryExtension.GetConnectionString(queryobject)); } if (command == null) { command = new SqlCommand(QueryExtension.GetSqlString(queryobject), connection); // Add all the paramters used in query. foreach (ObjectParameter parameter in queryobject.Parameters) { command.Parameters.AddWithValue(parameter.Name, parameter.Value); } } }
/// <summary> /// Initializes a new instance of ImmediateNotificationRegister class. /// </summary> /// <param name="query">an instance of ObjectQuery is used to get connection string and /// command string to register SqlDependency nitification. </param> public ImmediateNotificationRegister(ObjectQuery query) { try { this.oquery = query; QueryExtension.GetSqlCommand(oquery, ref connection, ref command); RegisterSqlDependency(); } catch (ArgumentException ex) { throw new ArgumentException("Paramter cannot be null", "query", ex); } catch (Exception ex) { throw new Exception( "Fails to initialize a new instance of ImmediateNotificationRegister class.", ex); } }
/// <summary> /// Use ObjectQuery to get SqlConnection and SqlCommand. /// </summary> public static void GetSqlCommand(ObjectQuery query, ref SqlConnection connection, ref SqlCommand command) { if (query == null) { throw new System.ArgumentException("Paramter cannot be null", "query"); } if (connection == null) { connection = new SqlConnection(QueryExtension.GetConnectionString(query)); } if (command == null) { command = new SqlCommand(QueryExtension.GetSqlString(query), connection); // Add all the paramters used in query. foreach (ObjectParameter parameter in query.Parameters) { command.Parameters.AddWithValue(parameter.Name, parameter.Value); } } }