Skip to content

Commit

Permalink
Refactor: Use var instead of comcrete types
Browse files Browse the repository at this point in the history
  • Loading branch information
tajbender committed Aug 4, 2024
1 parent 23dfb45 commit 3e301c5
Show file tree
Hide file tree
Showing 22 changed files with 131 additions and 131 deletions.
6 changes: 3 additions & 3 deletions src/EntityLighter/Collections/EntityBaseSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public bool Include(T item)

public int IndexOf(T item)
{
for (int i = 0; i < this.Count; i++)
for (var i = 0; i < this.Count; i++)
{
if (this.items[i] == item)
return i;
Expand All @@ -276,7 +276,7 @@ public void Insert(int index, T item)

public int LastIndexOf(T item)
{
int i = this.Count;
var i = this.Count;

while (i > 0)
{
Expand All @@ -290,7 +290,7 @@ public int LastIndexOf(T item)

public bool Remove(T item)
{
int i = this.IndexOf(item);
var i = this.IndexOf(item);
if (i < 0)
return false;

Expand Down
44 changes: 22 additions & 22 deletions src/EntityLighter/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public BaseColumnAttribute(DataType dataType)
/// <returns></returns>
public string ToSQLSnippet(string columnName)
{
StringBuilder snippet = new StringBuilder($"{ columnName } { this.DataType.ToSQLSnippet() } { this.Constraints.ToSQLSnippet() } ");
var snippet = new StringBuilder($"{ columnName } { this.DataType.ToSQLSnippet() } { this.Constraints.ToSQLSnippet() } ");

if (!string.IsNullOrWhiteSpace(this.DefaultValue))
snippet.Append($"DEFAULT { this.DefaultValue }");
Expand Down Expand Up @@ -363,18 +363,18 @@ public void AddColumn(string columnName, BaseColumnAttribute attributes)

public string ToSQLStatement()
{
int columnCount = this.TableColumns.Count;
var columnCount = this.TableColumns.Count;

if (columnCount < 1)
return string.Empty;

StringBuilder statement = new StringBuilder($"CREATE TABLE IF NOT EXISTS '{ this.EntityName }' ( ");
List<string> primaryKey = new List<string>();
var statement = new StringBuilder($"CREATE TABLE IF NOT EXISTS '{ this.EntityName }' ( ");
var primaryKey = new List<string>();

// Add Column snippets
for (int i = 0; i < columnCount;)
for (var i = 0; i < columnCount;)
{
this.TableColumns[i].Deconstruct(out string columnName, out BaseColumnAttribute attributes);
this.TableColumns[i].Deconstruct(out var columnName, out var attributes);

statement.Append(attributes.ToSQLSnippet(columnName));

Expand All @@ -390,7 +390,7 @@ public string ToSQLStatement()
{
statement.Append(", PRIMARY KEY (");

for (int i = 0; i < primaryKey.Count;)
for (var i = 0; i < primaryKey.Count;)
{
statement.Append(primaryKey[i]);
statement.Append(++i < primaryKey.Count ? ", " : ")");
Expand All @@ -414,9 +414,9 @@ public DataContext(string storage, bool createIfNotExists = false)
this.Storage = storage ?? throw new ArgumentNullException(nameof(storage));


SqliteResult returnCode = this.OpenDatabase(createIfNotExists); /* ThrowIfFailed */
var returnCode = this.OpenDatabase(createIfNotExists); /* ThrowIfFailed */

StorageModel storageModel = new StorageModel(this);
var storageModel = new StorageModel(this);

//EntityBaseSet<EntityBase> test =
// Select<EntityBase>
Expand Down Expand Up @@ -545,8 +545,8 @@ public class EntitySet<TEntity>
/// <param name="CreateIfNotExists"></param>
protected SqliteResult OpenDatabase(bool CreateIfNotExists = false)
{
SqliteOpenMode openMode = SqliteOpenMode.ReadWriteCreate;
int errorCode = 0;
var openMode = SqliteOpenMode.ReadWriteCreate;
var errorCode = 0;

try
{
Expand Down Expand Up @@ -581,7 +581,7 @@ protected SqliteResult OpenDatabase(bool CreateIfNotExists = false)
/// <returns>The number of rows inserted, updated or delated. -1 for SELECT statements.</returns>
public int ExecuteNonQuery(string statement)
{
using (SqliteCommand cmd = this.SqliteConnection.CreateCommand())
using (var cmd = this.SqliteConnection.CreateCommand())
{
cmd.CommandText = statement;
return cmd.ExecuteNonQuery();
Expand All @@ -590,7 +590,7 @@ public int ExecuteNonQuery(string statement)

public object ExecuteScalar(string statement)
{
using (SqliteCommand cmd = this.SqliteConnection.CreateCommand())
using (var cmd = this.SqliteConnection.CreateCommand())
{
cmd.CommandText = statement;
return cmd.ExecuteScalar();
Expand All @@ -599,7 +599,7 @@ public object ExecuteScalar(string statement)

public void SetPragmaValue(string pragmaName, string pragmaValue)
{
using (SqliteCommand cmd = this.SqliteConnection.CreateCommand())
using (var cmd = this.SqliteConnection.CreateCommand())
{
cmd.CommandText = $"PRAGMA { pragmaName } = '{ pragmaValue }';";
cmd.ExecuteNonQuery();
Expand All @@ -610,7 +610,7 @@ public void SetPragmaValue(string pragmaName, string pragmaValue)

public bool TableExists(string tableName)
{
object queryResult = this.ExecuteScalar($"SELECT COUNT(*) FROM SQLite_Master WHERE Type='table' AND UPPER(Name)='{ tableName.ToUpperInvariant() }'");
var queryResult = this.ExecuteScalar($"SELECT COUNT(*) FROM SQLite_Master WHERE Type='table' AND UPPER(Name)='{ tableName.ToUpperInvariant() }'");

if (queryResult is long resint)
return (1 == resint);
Expand All @@ -628,17 +628,17 @@ public void CreateEntityModel(Type entityType)
if (!Attribute.IsDefined(entityType, typeof(TableAttribute)))
throw new ArgumentException("No Table Attribute defined", nameof(entityType));

TableAttribute table = Attribute.GetCustomAttribute(entityType, typeof(TableAttribute)) as TableAttribute;
var table = Attribute.GetCustomAttribute(entityType, typeof(TableAttribute)) as TableAttribute;

string tableName = table.Name ?? entityType.Name;
var tableName = table.Name ?? entityType.Name;

if (this.TableExists(tableName))
{
// TODO: Compare table columns and update model if necessary
return;
}

TableStatementBuilder tableStatement = new TableStatementBuilder(tableName);
var tableStatement = new TableStatementBuilder(tableName);

// Iterate through all properties and process ColumnAttributes
foreach (var property in entityType.GetProperties())
Expand Down Expand Up @@ -670,7 +670,7 @@ public long CreateNewEntity(Type entityType, SetEntityCreationParamsCallback set

try
{
using (SqliteCommand sqlCmd = this.SqliteConnection.CreateCommand())
using (var sqlCmd = this.SqliteConnection.CreateCommand())
{
// TODO: 24/05/20: Create the WHOLE statement dynamically!; Use Tables attributes for dynamic binding
setEntityCreationParams(sqlCmd);
Expand All @@ -682,7 +682,7 @@ public long CreateNewEntity(Type entityType, SetEntityCreationParamsCallback set
// Finally fetch the Primary key for the new record
sqlCmd.CommandText = $"SELECT Id FROM { EntityBase.GetStorageTable(entityType) } WHERE RowId = Last_Insert_RowId()";

object entityId = sqlCmd.ExecuteScalar();
var entityId = sqlCmd.ExecuteScalar();

if (null != entityId)
return (long)entityId;
Expand All @@ -704,7 +704,7 @@ public int UpdateEntity(Type entityType, SetEntityUpdateParamsCallback callback)
if (null == callback)
throw new ArgumentNullException(nameof(callback));

using (SqliteCommand sqlCmd = this.SqliteConnection.CreateCommand())
using (var sqlCmd = this.SqliteConnection.CreateCommand())
{
// TODO: 24/05/20: Create the WHOLE statement dynamically!; Use Tables attributes for dynamic binding
callback(sqlCmd);
Expand All @@ -720,7 +720,7 @@ public int UpdateEntity(Type entityType, SetEntityUpdateParamsCallback callback)
public static DateTime ConvertToDateTime(string datetimeString)
{
// TODO: Put this into an default type converting place SQLite <-> C#-Entities, see also ApplicationWindow.fspFormStatePersistor_LoadingFormState
if (DateTime.TryParse(datetimeString, out DateTime result))
if (DateTime.TryParse(datetimeString, out var result))
return result;

return default;
Expand Down
2 changes: 1 addition & 1 deletion src/EntityLighter/EntityBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static string GetStorageTable(Type entityType)
if (!Attribute.IsDefined(entityType, typeof(TableAttribute)))
throw new ArgumentException($"No Table Attribute defined for { entityType }");

TableAttribute tableAttribute = Attribute.GetCustomAttribute(entityType, typeof(TableAttribute)) as TableAttribute;
var tableAttribute = Attribute.GetCustomAttribute(entityType, typeof(TableAttribute)) as TableAttribute;

return string.IsNullOrEmpty(tableAttribute.Name) ? entityType.Name : tableAttribute.Name;
}
Expand Down
6 changes: 3 additions & 3 deletions src/EntityLighter/Queries/Select.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public EntityBaseSet<TEntity> RunNow(DataContext dataContext, CreateEntityFromDa

private EntityBaseSet<TEntity> Execute(DataContext dataContext, CreateEntityFromDataReader dataReader)
{
EntityBaseSet<TEntity> loadedItems = new EntityBaseSet<TEntity>(dataContext);
var loadedItems = new EntityBaseSet<TEntity>(dataContext);
SqliteCommand sqliteCommand;

using (sqliteCommand = dataContext.SqliteConnection.CreateCommand())
Expand All @@ -148,7 +148,7 @@ private EntityBaseSet<TEntity> Execute(DataContext dataContext, CreateEntityFrom
{
sqliteCommand.CommandText = this.PrepareSQLStatement();

using (SqliteDataReader reader = sqliteCommand.ExecuteReader())
using (var reader = sqliteCommand.ExecuteReader())
{
//loadedItems.Grow(reader => count); // TODO: Specify size here!

Expand All @@ -167,7 +167,7 @@ private EntityBaseSet<TEntity> Execute(DataContext dataContext, CreateEntityFrom

private string PrepareSQLStatement()
{
StringBuilder queryBuilder = new StringBuilder($"SELECT * FROM { this.storageTableName }");
var queryBuilder = new StringBuilder($"SELECT * FROM { this.storageTableName }");

// Append where conditions to selection
if (this.whereConditions.Count > 0)
Expand Down
34 changes: 17 additions & 17 deletions src/electrifier.Core/AppContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public static string AssemblyTitle
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
var attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);

if (attributes.Length > 0)
{
AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
var titleAttribute = (AssemblyTitleAttribute)attributes[0];

if (!string.IsNullOrWhiteSpace(titleAttribute.Title))
return titleAttribute.Title;
Expand All @@ -51,7 +51,7 @@ public static string AssemblyDescription
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
var attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);

return (attributes.Length == 0) ? string.Empty : ((AssemblyDescriptionAttribute)attributes[0]).Description;
}
Expand All @@ -61,7 +61,7 @@ public static string AssemblyProduct
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
var attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);

return (attributes.Length == 0) ? string.Empty : ((AssemblyProductAttribute)attributes[0]).Product;
}
Expand All @@ -71,7 +71,7 @@ public static string AssemblyCopyright
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
var attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);

return (attributes.Length == 0) ? string.Empty : ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
}
Expand All @@ -81,7 +81,7 @@ public static string AssemblyCompany
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
var attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);

return (attributes.Length == 0) ? string.Empty : ((AssemblyCompanyAttribute)attributes[0]).Company;
}
Expand Down Expand Up @@ -120,7 +120,7 @@ public AppContext(string[] args, Icon appIcon, Bitmap appLogo, Form splashScreen
AppDomain.CurrentDomain.UnhandledException += this.CurrentDomain_UnhandledException;

// Parse command line paramaters
foreach (string arg in args)
foreach (var arg in args)
{
// Portable: Issue #5: Add "-portable" command switch, storing configuration in application directory instead of "LocalApplicationData"
if (arg.Equals("/portable", StringComparison.OrdinalIgnoreCase))
Expand All @@ -143,7 +143,7 @@ public AppContext(string[] args, Icon appIcon, Bitmap appLogo, Form splashScreen
try
{
// TODO: Store sessioncontext?!?
SessionContext sessionContext = new SessionContext(this.Icon, DetermineBaseDirectory(isPortable), isIncognito);
var sessionContext = new SessionContext(this.Icon, DetermineBaseDirectory(isPortable), isIncognito);
sessionContext.MainFormChange += this.SessionContext_MainFormChange;
sessionContext.CreateInitialForm(this);

Expand Down Expand Up @@ -294,7 +294,7 @@ private void SessionContext_MainFormChange(object sender, MainFormChangeEventArg

public static string BuildDefaultFormText(string baseFormText = null)
{
string text = string.IsNullOrWhiteSpace(baseFormText) ?
var text = string.IsNullOrWhiteSpace(baseFormText) ?
FormTitleAffix :
$"{baseFormText} - {FormTitleAffix}";

Expand Down Expand Up @@ -322,9 +322,9 @@ private void AppContext_ThreadExit(object sender, EventArgs e)
private void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
// TODO: Use Vanara.Windows.Forms.TaskDialog;
string exMessage = $"Thread exception occured: { e.Exception.GetType().FullName }\n" +
$"\n{ e.Exception.Message }\n" +
$"\n{ e.Exception.StackTrace }\n";
var exMessage = $"Thread exception occured: { e.Exception.GetType().FullName }\n" +
$"\n{ e.Exception.Message }\n" +
$"\n{ e.Exception.StackTrace }\n";

if (null != e.Exception.InnerException)
exMessage += $"\nInner Exception: { e.Exception.InnerException.Message }\n";
Expand All @@ -336,8 +336,8 @@ private void Application_ThreadException(object sender, ThreadExceptionEventArgs
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// TODO: Use Vanara.Windows.Forms.TaskDialog;
string exMessage = $"Domain exception occured: { e.ExceptionObject.GetType().FullName }" +
$"\n\n{ e.ExceptionObject }\n";
var exMessage = $"Domain exception occured: { e.ExceptionObject.GetType().FullName }" +
$"\n\n{ e.ExceptionObject }\n";

LogContext.Error(exMessage);
MessageBox.Show(exMessage, "D'oh! That shouldn't have happened...");
Expand All @@ -348,7 +348,7 @@ private static string DetermineBaseDirectory(bool isPortable)
if (isPortable)
return Application.StartupPath;

string baseDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
var baseDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
AppContext.AssemblyCompany);

// Ensure the directory exists
Expand All @@ -374,9 +374,9 @@ public static string GetDotNetFrameworkVersion(bool check64Bit = true)
const string regSubKey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
const string regValue = @"Release";

using (RegistryKey regKeyLocalMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32))
using (var regKeyLocalMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32))
{
using (RegistryKey regKeyNetDeveloperPlatform = regKeyLocalMachine?.OpenSubKey(regSubKey))
using (var regKeyNetDeveloperPlatform = regKeyLocalMachine?.OpenSubKey(regSubKey))
{
if (null != regKeyNetDeveloperPlatform?.GetValue(regValue))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static IEnumerable<string> EnumerateAvailableThemes(this IThemedControl c
if (null == control)
throw new ArgumentNullException(nameof(control));

IEnumerable<string> resourceNames = Assembly.GetExecutingAssembly().GetManifestResourceNames().Where(
var resourceNames = Assembly.GetExecutingAssembly().GetManifestResourceNames().Where(
Name =>
Name.StartsWith(control.ThemeResourceNamespace, StringComparison.InvariantCultureIgnoreCase) &&
Name.EndsWith(ThemeFileExtension, StringComparison.InvariantCultureIgnoreCase));
Expand All @@ -62,18 +62,18 @@ public static ImageList LoadThemeImageListFromResource(this IThemedControl contr
if (string.IsNullOrWhiteSpace(themeName))
throw new ArgumentOutOfRangeException(nameof(themeName), "No theme name provided");

string resourceName = control.ThemeResourceNamespace + themeName + ThemeFileExtension;
var resourceName = control.ThemeResourceNamespace + themeName + ThemeFileExtension;

try
{
ImageList imageList = new ImageList
var imageList = new ImageList
{
/// Fix bug with alpha channels by enabling transparency before adding any images to the list.
/// <seealso href="https://www.codeproject.com/articles/9142/adding-and-using-32-bit-alphablended-images-and-ic"/>
ColorDepth = ColorDepth.Depth32Bit
};

using (Stream bmpStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
using (var bmpStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
var bitmap = new Bitmap(bmpStream);

Expand Down
Loading

0 comments on commit 3e301c5

Please sign in to comment.