Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What is really wrong with your program logic - logic is not obvious. It is so obscure, that you can't understand where error is. So, my advise is following - if you can't find the error, try to simplify your code.</p> <p>Currently your method has many responsibilities - it queries database, it dumps data to file, it publishes data somewhere and logs the results. And you stuck with all that stuff. If someone will need to change database query, or publishing logic - he will need to review all other stuff. </p> <p>So, separate logic first:</p> <pre><code>private void PublishLoop() { string previousIDs = String.Empty; int timeout = Int32.Parse(ConfigurationManager.AppSettings["publishTimeout"]); while (Running) { string currentIDs = ConcatenateList(LoadIDs()); Dump(currentIDs); if (!previousIDs.Equals(currentIDs)) { try { Publish(currentIDs); _log.Info("Published successfuly"); } catch (PublicationException exception) { _log.Error("Publication failed"); } previousIDs = currentIDs; } Thread.Sleep(timeout); } } </code></pre> <p>Well, I don't know much about your domain, so you probably can think about better names for variables and methods.</p> <p>Here you have data access logic extracted to separate method (it's ok for first step of refactoring and for small applications). Keep in mind, that wrapping connection object into using block guarantee that connection will be closed in case of exception:</p> <pre><code>private IList&lt;int&gt; LoadIDs() { List&lt;int&gt; ids = new List&lt;int&gt;(); String connectionString = ConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = connection.CreateCommand(); command.CommandText = "select ID from Tab1"; command.Notification = null; connection.Open(); using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection)) { while ((reader.Read())) ids.Add((int)reader["ID"]); } } return ids; } </code></pre> <p>Next - simple method for concatenating ids into one string:</p> <pre><code>private string ConcatenateList(IList&lt;int&gt; values) { return String.Join(" ", values.Select(value =&gt; value.ToString()).ToArray()); } </code></pre> <p>Dumping (mind, that file name moved to configuration file):</p> <pre><code>private void Dump(string ids) { using (StreamWriter writer = new StreamWriter(ConfigurationManager.AppSettings["dumpFilePath"])) writer.WriteLine(ids); } </code></pre> <p>And publishing logic:</p> <pre><code>private void Publish(string ids) { PublisherArgs args = new PublisherArgs { DomainKey = "c80cb405-eb77-4574-9405-5ba51832f5e6", DomainName = "localhost" }; Publisher publisher = new Publisher(args); Publication publication = publisher.Publish("/test", JSON.Serialize(ids)); if (!publication.Successful) throw new PublicationException(); } </code></pre> <p>I think that failures are exceptional and they not occur very often (so I decided to use exceptions for that case). But if it's something ordinary - you can simply use boolean method like TryPublish.</p> <p>BTW you can use some logging library like log4net for logging successful and failure publishing. Or you can extract logging logic to separate method - this will make primary logic cleaner and easier to understand.</p> <p>PS try to avoid comparison of boolean variables with true/false (publication.Successful == true) - you can occasionally assign value to your variable.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload