Skip to content

Commit

Permalink
Refactor - squash db interface into single Apply method
Browse files Browse the repository at this point in the history
  • Loading branch information
timabell committed Feb 21, 2015
1 parent b0163a3 commit 7726620
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
6 changes: 1 addition & 5 deletions EfEnumToLookup/LookupGenerator/EnumToLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ public void Apply(DbContext context)
Values = GetLookupValues(enm),
}).ToList();

sqlServerHandler.CreateTables(lookups, (sql) => ExecuteSqlCommand(context, sql));
sqlServerHandler.PopulateLookups(lookups, (sql, parameters) => ExecuteSqlCommand(context, sql, parameters));

// add fks from all referencing tables
sqlServerHandler.AddForeignKeys(enumReferences, (sql) => ExecuteSqlCommand(context, sql));
sqlServerHandler.Apply(lookups, enumReferences, (sql, parameters) => ExecuteSqlCommand(context, sql, parameters));
}

private static int ExecuteSqlCommand(DbContext context, string sql, IEnumerable<SqlParameter> parameters = null)
Expand Down
14 changes: 11 additions & 3 deletions EfEnumToLookup/LookupGenerator/SqlServerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ class SqlServerHandler
/// </summary>
public string TableNameSuffix { get; set; }

internal void CreateTables(IEnumerable<LookupData> enums, Action<string> runSql)

internal void Apply(List<LookupData> lookups, IList<EnumReference> enumReferences, Action<string, IEnumerable<SqlParameter>> runSql)
{
CreateTables(lookups, (sql) => runSql(sql, null));
PopulateLookups(lookups, runSql);
AddForeignKeys(enumReferences, (sql) => runSql(sql, null));
}

private void CreateTables(IEnumerable<LookupData> enums, Action<string> runSql)
{
foreach (var lookup in enums)
{
Expand All @@ -37,7 +45,7 @@ internal void CreateTables(IEnumerable<LookupData> enums, Action<string> runSql)
}
}

internal void AddForeignKeys(IEnumerable<EnumReference> refs, Action<string> runSql)
private void AddForeignKeys(IEnumerable<EnumReference> refs, Action<string> runSql)
{
foreach (var enumReference in refs)
{
Expand All @@ -52,7 +60,7 @@ internal void AddForeignKeys(IEnumerable<EnumReference> refs, Action<string> run
}
}

internal void PopulateLookups(IEnumerable<LookupData> lookupData, Action<string, IEnumerable<SqlParameter>> runSql)
private void PopulateLookups(IEnumerable<LookupData> lookupData, Action<string, IEnumerable<SqlParameter>> runSql)
{
foreach (var lookup in lookupData)
{
Expand Down

0 comments on commit 7726620

Please sign in to comment.