Skip to content

Commit

Permalink
Refactor - extract interface for db handling
Browse files Browse the repository at this point in the history
To assist with multi-db support

issue #16
  • Loading branch information
timabell committed Feb 21, 2015
1 parent 7726620 commit 960570e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
1 change: 1 addition & 0 deletions EfEnumToLookup/EfEnumToLookup.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<ItemGroup>
<Compile Include="LookupGenerator\EnumGeneratorException.cs" />
<Compile Include="LookupGenerator\EnumToLookup.cs" />
<Compile Include="LookupGenerator\IDbHandler.cs" />
<Compile Include="LookupGenerator\IEnumToLookup.cs" />
<Compile Include="LookupGenerator\EnumReference.cs" />
<Compile Include="LookupGenerator\LookupData.cs" />
Expand Down
19 changes: 7 additions & 12 deletions EfEnumToLookup/LookupGenerator/EnumToLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,9 @@ public void Apply(DbContext context)
// recurse through dbsets and references finding anything that uses an enum
var enumReferences = FindEnumReferences(context);

var sqlServerHandler = new SqlServerHandler
{
NameFieldLength = NameFieldLength,
TableNamePrefix = TableNamePrefix,
TableNameSuffix = TableNameSuffix,
};

// todo: fold the populate method into the create tables method, providing all the data to the db layer in one hit,
// this will require merging the two sql callbacks into one signature

// for the list of enums generate and missing tables
var enums = enumReferences.Select(r => r.EnumType).Distinct().ToList();

// merge values into these tables
var lookups =
(from enm in enums
select new LookupData
Expand All @@ -93,7 +82,13 @@ public void Apply(DbContext context)
Values = GetLookupValues(enm),
}).ToList();

sqlServerHandler.Apply(lookups, enumReferences, (sql, parameters) => ExecuteSqlCommand(context, sql, parameters));
// todo: support MariaDb etc. Issue #16
IDbHandler dbHandler = new SqlServerHandler();
dbHandler.NameFieldLength = NameFieldLength;
dbHandler.TableNamePrefix = TableNamePrefix;
dbHandler.TableNameSuffix = TableNameSuffix;

dbHandler.Apply(lookups, enumReferences, (sql, parameters) => ExecuteSqlCommand(context, sql, parameters));
}

private static int ExecuteSqlCommand(DbContext context, string sql, IEnumerable<SqlParameter> parameters = null)
Expand Down
29 changes: 29 additions & 0 deletions EfEnumToLookup/LookupGenerator/IDbHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace EfEnumToLookup.LookupGenerator
{
using System;
using System.Collections.Generic;
using System.Data.SqlClient;

internal interface IDbHandler
{
/// <summary>
/// The size of the Name field that will be added to the generated lookup tables.
/// Adjust to suit your data if required.
/// </summary>
int NameFieldLength { get; set; }

/// <summary>
/// Prefix to add to all the generated tables to separate help group them together
/// and make them stand out as different from other tables.
/// </summary>
string TableNamePrefix { get; set; }

/// <summary>
/// Suffix to add to all the generated tables to separate help group them together
/// and make them stand out as different from other tables.
/// </summary>
string TableNameSuffix { get; set; }

void Apply(List<LookupData> lookups, IList<EnumReference> enumReferences, Action<string, IEnumerable<SqlParameter>> runSql);
}
}
4 changes: 2 additions & 2 deletions EfEnumToLookup/LookupGenerator/SqlServerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Data.SqlClient;
using System.Text;

class SqlServerHandler
class SqlServerHandler : IDbHandler
{
/// <summary>
/// The size of the Name field that will be added to the generated lookup tables.
Expand All @@ -28,7 +28,7 @@ class SqlServerHandler
public string TableNameSuffix { get; set; }


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

0 comments on commit 960570e

Please sign in to comment.