Bunch of ReSharper suggestions

This commit is contained in:
2015-11-02 20:29:00 -05:00
parent ff2ef0fd46
commit d8d7a9b5ba
15 changed files with 43 additions and 215 deletions

View File

@@ -12,7 +12,8 @@ namespace FeedCenter
{ {
public partial class App public partial class App
{ {
public static bool IsDebugBuild // ReSharper disable ConvertPropertyToExpressionBody
private static bool IsDebugBuild
{ {
get get
{ {
@@ -23,6 +24,7 @@ namespace FeedCenter
#endif #endif
} }
} }
// ReSharper restore ConvertPropertyToExpressionBody
[STAThread] [STAThread]
public static void Main() public static void Main()

View File

@@ -43,7 +43,7 @@ namespace FeedCenter
return true; return true;
// Add quotes around the URL for safety // Add quotes around the URL for safety
url = string.Format("\"{0}\"", url); url = $"\"{url}\"";
// Start the browser // Start the browser
if (browser == null) if (browser == null)

View File

@@ -69,17 +69,14 @@ namespace FeedCenter.Data
#endregion #endregion
public static bool DatabaseExists public static bool DatabaseExists => File.Exists(DatabasePath);
{
get { return File.Exists(DatabasePath); }
}
public static void CreateDatabase() public static void CreateDatabase()
{ {
Tracer.WriteLine("Creating database engine"); Tracer.WriteLine("Creating database engine");
// Create the database engine // Create the database engine
using (var engine = new SqlCeEngine(string.Format("Data Source={0}", DatabasePath))) using (var engine = new SqlCeEngine($"Data Source={DatabasePath}"))
{ {
Tracer.WriteLine("Creating database"); Tracer.WriteLine("Creating database");
@@ -133,7 +130,7 @@ namespace FeedCenter.Data
Tracer.WriteLine("Creating database engine"); Tracer.WriteLine("Creating database engine");
// Create the database engine // Create the database engine
using (var engine = new SqlCeEngine(string.Format("Data Source={0}", DatabasePath))) using (var engine = new SqlCeEngine($"Data Source={DatabasePath}"))
{ {
Tracer.WriteLine("Upgrading database"); Tracer.WriteLine("Upgrading database");
@@ -145,7 +142,7 @@ namespace FeedCenter.Data
Tracer.WriteLine("Getting database version"); Tracer.WriteLine("Getting database version");
// Create a database connection // Create a database connection
using (var connection = new SqlCeConnection(string.Format("Data Source={0}", DatabasePath))) using (var connection = new SqlCeConnection($"Data Source={DatabasePath}"))
{ {
// Open the connection // Open the connection
connection.Open(); connection.Open();
@@ -190,7 +187,7 @@ namespace FeedCenter.Data
Tracer.WriteLine("Creating database engine"); Tracer.WriteLine("Creating database engine");
// Create the database engine // Create the database engine
using (var engine = new SqlCeEngine(string.Format("Data Source={0}", DatabasePath))) using (var engine = new SqlCeEngine($"Data Source={DatabasePath}"))
{ {
Tracer.WriteLine("Shrinking database"); Tracer.WriteLine("Shrinking database");
@@ -202,7 +199,7 @@ namespace FeedCenter.Data
private static void ExecuteScript(string scriptText) private static void ExecuteScript(string scriptText)
{ {
// Create a database connection // Create a database connection
using (var connection = new SqlCeConnection(string.Format("Data Source={0}", DatabasePath))) using (var connection = new SqlCeConnection($"Data Source={DatabasePath}"))
{ {
// Open the connection // Open the connection
connection.Open(); connection.Open();

View File

@@ -1,7 +1,4 @@
using System; using System.Data.SqlTypes;
using System.Data;
using System.Data.SqlServerCe;
using System.Data.SqlTypes;
namespace FeedCenter.Data namespace FeedCenter.Data
{ {
@@ -12,144 +9,5 @@ namespace FeedCenter.Data
public static SqlDateTime SqlDateTimeZero = new SqlDateTime(0, 0); public static SqlDateTime SqlDateTimeZero = new SqlDateTime(0, 0);
#endregion #endregion
#region DataSet
public static DataRow GetFirstDataRow(this DataSet dataSet)
{
// If we get no data set then return nothing
if (dataSet == null)
return null;
// If there were no tables returns then return nothing
if (dataSet.Tables.Count == 0)
return null;
// Get the first table
var firstTable = dataSet.Tables[0];
// If the table has no rows then return nothing
if (firstTable.Rows.Count == 0)
return null;
// Return the first row
return firstTable.Rows[0];
}
#endregion
#region SqlCeCommand
public static void SetStatement(this SqlCeCommand command, string statement, params object[] parameters)
{
// Create a new array to hold the updated parameters
var formattedParameters = new object[parameters.Length];
// Initialize our position
var position = 0;
// Loop over each parameter
foreach (var parameter in parameters)
{
// If the parameter is a DateTime then we need to reformat
if (parameter == null)
{
// Use a explicit null value
formattedParameters[position++] = "NULL";
}
else if (parameter is DateTime)
{
// Cast the parameter back to a DateTime
var dateTime = (DateTime) parameter;
// Convert the DateTime to sortable format
var formatted = dateTime.ToString("s");
// Set into the formatted array
formattedParameters[position++] = formatted;
}
else if (parameter is bool)
{
// Convert the boolean to a number
formattedParameters[position++] = Convert.ToInt32(parameter);
}
else if (parameter.GetType().IsEnum)
{
// Convert the enum to a number
formattedParameters[position++] = Convert.ToInt32(parameter);
}
else if (parameter is string)
{
// Escape single quotes
formattedParameters[position++] = (parameter as string).Replace("'", "''");
}
else
{
// Just put the original value in
formattedParameters[position++] = parameter;
}
}
// Build the full statement
command.CommandText = string.Format(statement, formattedParameters);
}
#endregion
#region SqlCeConnection
public static void ExecuteNonQuery(this SqlCeConnection connection, string query, params object[] parameters)
{
// Create the command object
var command = connection.CreateCommand();
// Set the statement based on the query and parameters
command.SetStatement(query, parameters);
//Tracer.WriteLine("Executing SQL statement: {0}", command.CommandText);
// Execute the command
command.ExecuteNonQuery();
}
public static DataSet ExecuteDataSet(this SqlCeConnection connection, string query, params object[] parameters)
{
// Create the command object
var command = connection.CreateCommand();
// Set the statement based on the query and parameters
command.SetStatement(query, parameters);
// Create a new data adapter
using (var adapter = new SqlCeDataAdapter(command))
{
// Create the new data set
using (var dataSet = new DataSet())
{
//Tracer.WriteLine("Executing SQL query: {0}", command.CommandText);
// Fill the data set
adapter.Fill(dataSet);
return dataSet;
}
}
}
public static object ExecuteScalar(this SqlCeConnection connection, string query, params object[] parameters)
{
// Create the command object
var command = connection.CreateCommand();
// Set the statement based on the query and parameters
command.SetStatement(query, parameters);
//Tracer.WriteLine("Executing SQL statement: {0}", command.CommandText);
// Execute the command
return command.ExecuteScalar();
}
#endregion
} }
} }

View File

@@ -86,12 +86,14 @@ namespace FeedCenter
void HandleObjectStateManagerObjectStateManagerChanged(object sender, CollectionChangeEventArgs e) void HandleObjectStateManagerObjectStateManagerChanged(object sender, CollectionChangeEventArgs e)
{ {
if (e.Element is Category) var element = e.Element as Category;
if (element != null)
{ {
if (_allCategories == null) if (_allCategories == null)
return; return;
var category = e.Element as Category; var category = element;
switch (e.Action) switch (e.Action)
{ {
@@ -113,7 +115,7 @@ namespace FeedCenter
if (_allFeeds == null) if (_allFeeds == null)
return; return;
var feed = e.Element as Feed; var feed = (Feed) e.Element;
switch (e.Action) switch (e.Action)
{ {

View File

@@ -1,5 +1,4 @@
using Common.Debug; using Common.Debug;
using Common.Xml;
using System.Xml; using System.Xml;
namespace FeedCenter.FeedParsers namespace FeedCenter.FeedParsers
@@ -18,9 +17,6 @@ namespace FeedCenter.FeedParsers
// Load the XML document from the text // Load the XML document from the text
document.LoadXml(feedText); document.LoadXml(feedText);
// Create the namespace manager
var namespaceManager = document.GetAllNamespaces();
// Get the root node // Get the root node
XmlNode rootNode = document.DocumentElement; XmlNode rootNode = document.DocumentElement;
@@ -62,7 +58,7 @@ namespace FeedCenter.FeedParsers
break; break;
case "entry": case "entry":
HandleFeedItem(namespaceManager, node, ref sequence); HandleFeedItem(node, ref sequence);
break; break;
} }
} }
@@ -77,7 +73,7 @@ namespace FeedCenter.FeedParsers
} }
} }
protected override FeedItem ParseFeedItem(XmlNamespaceManager namespaceManager, XmlNode node) protected override FeedItem ParseFeedItem(XmlNode node)
{ {
// Create a new feed item // Create a new feed item
var feedItem = FeedItem.Create(); var feedItem = FeedItem.Create();

View File

@@ -35,12 +35,12 @@ namespace FeedCenter.FeedParsers
public abstract FeedReadResult ParseFeed(string feedText); public abstract FeedReadResult ParseFeed(string feedText);
protected abstract FeedItem ParseFeedItem(XmlNamespaceManager namespaceManager, XmlNode node); protected abstract FeedItem ParseFeedItem(XmlNode node);
protected void HandleFeedItem(XmlNamespaceManager namespaceManager, XmlNode node, ref int sequence) protected void HandleFeedItem(XmlNode node, ref int sequence)
{ {
// Build a feed item from the node // Build a feed item from the node
FeedItem newFeedItem = ParseFeedItem(namespaceManager, node); FeedItem newFeedItem = ParseFeedItem(node);
if (newFeedItem == null) if (newFeedItem == null)
return; return;
@@ -116,10 +116,10 @@ namespace FeedCenter.FeedParsers
return new AtomParser(feed); return new AtomParser(feed);
} }
throw new ArgumentException(string.Format("Feed type {0} is not supported", feedType)); throw new ArgumentException($"Feed type {feedType} is not supported");
} }
public static FeedType DetectFeedType(string feedText) private static FeedType DetectFeedType(string feedText)
{ {
try try
{ {

View File

@@ -64,7 +64,7 @@ namespace FeedCenter.FeedParsers
switch (node.Name) switch (node.Name)
{ {
case "item": case "item":
HandleFeedItem(namespaceManager, node, ref sequence); HandleFeedItem(node, ref sequence);
break; break;
} }
} }
@@ -79,7 +79,7 @@ namespace FeedCenter.FeedParsers
} }
} }
protected override FeedItem ParseFeedItem(XmlNamespaceManager namespaceManager, XmlNode node) protected override FeedItem ParseFeedItem(XmlNode node)
{ {
// Create a new feed item // Create a new feed item
FeedItem feedItem = FeedItem.Create(); FeedItem feedItem = FeedItem.Create();

View File

@@ -57,7 +57,7 @@ namespace FeedCenter.FeedParsers
break; break;
case "item": case "item":
HandleFeedItem(namespaceManager, node, ref sequence); HandleFeedItem(node, ref sequence);
break; break;
} }
} }
@@ -72,7 +72,7 @@ namespace FeedCenter.FeedParsers
} }
} }
protected override FeedItem ParseFeedItem(XmlNamespaceManager namespaceManager, XmlNode node) protected override FeedItem ParseFeedItem(XmlNode node)
{ {
// Create a new feed item // Create a new feed item
FeedItem feedItem = FeedItem.Create(); FeedItem feedItem = FeedItem.Create();

View File

@@ -9,14 +9,9 @@ namespace FeedCenter
return new Category { ID = Guid.NewGuid() }; return new Category { ID = Guid.NewGuid() };
} }
public bool IsDefault public bool IsDefault => Name == "< default >";
{
get { return Name == "< default >"; }
}
public int SortKey // ReSharper disable once UnusedMember.Global
{ public int SortKey => IsDefault ? 0 : 1;
get { return IsDefault ? 0 : 1; }
}
} }
} }

View File

@@ -58,12 +58,6 @@ namespace FeedCenter
return new Feed { ID = Guid.NewGuid(), CategoryID = database.DefaultCategory.ID }; return new Feed { ID = Guid.NewGuid(), CategoryID = database.DefaultCategory.ID };
} }
#region Event delegates
public delegate void ErrorEventHandler(WebException webException);
#endregion
#region Reading #region Reading
public FeedReadResult Read(FeedCenterEntities database, bool forceRead = false) public FeedReadResult Read(FeedCenterEntities database, bool forceRead = false)
@@ -317,6 +311,7 @@ namespace FeedCenter
#endregion #endregion
// ReSharper disable once UnusedMember.Global
public string LastReadResultDescription public string LastReadResultDescription
{ {
get get
@@ -325,7 +320,7 @@ namespace FeedCenter
var lastReadResult = LastReadResult; var lastReadResult = LastReadResult;
// Build the name of the resource using the enum name and the value // Build the name of the resource using the enum name and the value
var resourceName = string.Format("{0}_{1}", typeof(FeedReadResult).Name, lastReadResult); var resourceName = $"{typeof (FeedReadResult).Name}_{lastReadResult}";
// Try to get the value from the resources // Try to get the value from the resources
var resourceValue = Properties.Resources.ResourceManager.GetString(resourceName); var resourceValue = Properties.Resources.ResourceManager.GetString(resourceName);

View File

@@ -838,7 +838,7 @@ namespace FeedCenter
private void ResetDatabase() private void ResetDatabase()
{ {
// Get the ID of the current feed // Get the ID of the current feed
var currentId = _currentFeed == null ? Guid.Empty : _currentFeed.ID; var currentId = _currentFeed?.ID ?? Guid.Empty;
// Create a new database object // Create a new database object
_database = new FeedCenterEntities(); _database = new FeedCenterEntities();
@@ -977,7 +977,7 @@ namespace FeedCenter
foreach (var feed in _database.Feeds.OrderBy(feed => feed.Name)) foreach (var feed in _database.Feeds.OrderBy(feed => feed.Name))
{ {
// Build a string to display the feed name and the unread count // Build a string to display the feed name and the unread count
var display = string.Format("{0} ({1:d})", feed.Name, feed.Items.Count(item => !item.BeenRead)); var display = $"{feed.Name} ({feed.Items.Count(item => !item.BeenRead):d})";
// Create a menu item // Create a menu item
var menuItem = new MenuItem var menuItem = new MenuItem

View File

@@ -73,7 +73,7 @@ namespace FeedCenter
ShowBalloonTip(text, icon, Settings.Default.BalloonTipTimeout); ShowBalloonTip(text, icon, Settings.Default.BalloonTipTimeout);
} }
public static void ShowBalloonTip(string text, ToolTipIcon icon, int timeout) private static void ShowBalloonTip(string text, ToolTipIcon icon, int timeout)
{ {
_notificationIcon.ShowBalloonTip(timeout, Resources.ApplicationDisplayName, text, icon); _notificationIcon.ShowBalloonTip(timeout, Resources.ApplicationDisplayName, text, icon);
} }

View File

@@ -36,7 +36,7 @@ namespace FeedCenter
var setting = entities.Settings.FirstOrDefault(s => s.Name == name && s.Version == versionString); var setting = entities.Settings.FirstOrDefault(s => s.Name == name && s.Version == versionString);
return setting == null ? null : setting.Value; return setting?.Value;
} }
public static void SetSettingValue(object dataStore, string name, Version version, string value) public static void SetSettingValue(object dataStore, string name, Version version, string value)
@@ -69,14 +69,11 @@ namespace FeedCenter
{ {
var entities = (FeedCenterEntities) dataStore; var entities = (FeedCenterEntities) dataStore;
if (entities == null)
return null;
// Get a distinct list of version strings // Get a distinct list of version strings
var versions = entities.Settings.Select(s => s.Version).Distinct().ToList(); var versions = entities?.Settings.Select(s => s.Version).Distinct().ToList();
// Create a version object for each string and return the list // Create a version object for each string and return the list
return versions.Select(s => new Version(s)).ToList(); return versions?.Select(s => new Version(s)).ToList();
} }
public static void DeleteSettingsForVersion(object dataStore, Version version) public static void DeleteSettingsForVersion(object dataStore, Version version)

View File

@@ -1,9 +1,7 @@
using Common.Wpf.Extensions; using Microsoft.Win32;
using Microsoft.Win32;
using System; using System;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using System.Windows;
using FeedCenter.Properties; using FeedCenter.Properties;
namespace FeedCenter namespace FeedCenter
@@ -44,7 +42,7 @@ namespace FeedCenter
if (subKey != null) if (subKey != null)
{ {
// Write the assembly location and parameter // Write the assembly location and parameter
subKey.SetValue(string.Empty, string.Format("\"{0}\" %1", assemblyLocation)); subKey.SetValue(string.Empty, $"\"{assemblyLocation}\" %1");
// Close the subkey // Close the subkey
subKey.Close(); subKey.Close();
@@ -56,21 +54,9 @@ namespace FeedCenter
} }
} }
public static bool UseDebugPath private static bool UseDebugPath => Environment.CommandLine.IndexOf("/debugPath", StringComparison.InvariantCultureIgnoreCase) != -1;
{
get
{
return Environment.CommandLine.IndexOf("/debugPath", StringComparison.InvariantCultureIgnoreCase) != -1;
}
}
public static string DataDirectory public static string DataDirectory => UseDebugPath ? Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) : UserSettingsPath;
{
get
{
return UseDebugPath ? Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) : UserSettingsPath;
}
}
public static string UserSettingsPath public static string UserSettingsPath
{ {
@@ -83,7 +69,7 @@ namespace FeedCenter
// Get the path to the local application data directory // Get the path to the local application data directory
var path = Path.Combine( var path = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
Properties.Resources.ApplicationName); Resources.ApplicationName);
// Make sure it exists - create it if needed // Make sure it exists - create it if needed
if (!Directory.Exists(path)) if (!Directory.Exists(path))