Skip to content

Commit

Permalink
Merge pull request #18 from fdonnet/xUnit_testting2
Browse files Browse the repository at this point in the history
Big PR that uses C# string literal to much cleaner generator and put in place very basic tests
  • Loading branch information
fdonnet authored Dec 2, 2022
2 parents e08c2d8 + fc9a523 commit 303f3a5
Show file tree
Hide file tree
Showing 85 changed files with 3,615 additions and 1,240 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using Dapper_Layers_Generator.Core.Converters;
using Dapper_Layers_Generator.Core.Settings;
using System.Text;

namespace Dapper_Layers_Generator.Core.Generators
{
public abstract class GeneratorContextForProvider : GeneratorContextTemplate
public abstract class GeneratorContextForProvider : GeneratorContextTemplate
{
protected abstract string UsingDbProviderSpecific { get; init; }
protected abstract string DbProviderString { get; init; }
Expand All @@ -23,45 +22,108 @@ public GeneratorContextForProvider(SettingsGlobal settingsGlobal

public override string Generate()
{
var builder = new StringBuilder();
//Header
builder.Append(WriteContextHeaderComment());

//Db context class
builder.Append(WriteFullClassContent());
return
$"""
{WriteUsingStatements()}
{WriteContextHeaderComment()}
{WriteFullClassContent()}
""";
}

return builder.ToString();
protected override string WriteUsingStatements()
{
return
$"""
using {_settings.TargetNamespaceForRepo};
using System.Data;
{UsingDbProviderSpecific}
using Dapper;
using Microsoft.Extensions.Configuration;
""";
}

protected override string WriteContextHeaderComment()
{
return $@"{@WriteUsingStatements()}
// =================================================================
// DBContext implements all repo management + a small context factory
// Inherits from DbContext base abstract class
// Specific for DB provider {DbProviderString}
// Author: {_settings.AuthorName}
// Context name: {_settings.DbContextClassName}
// Generated: {DateTime.Now}
// WARNING: Never change this file manually (re-generate it)
// =================================================================
namespace {_settings.TargetNamespaceForDbContext}
{{";

return
$$"""
// =================================================================
// DBContext implements all repo management + a small context factory
// Inherits from DbContext base abstract class
// Specific for DB provider {{DbProviderString}}
// Author: {{_settings.AuthorName}}
// Context name: {{_settings.DbContextClassName}}
// Generated: {{_settings.GenerationTimestamp.ToString("yyyy-MM-dd HH:mm:ss")}} UTC
// WARNING: Never change this file manually (re-generate it)
// =================================================================
namespace {{_settings.TargetNamespaceForDbContext}}
{
""";
}

protected override string WriteUsingStatements()
protected override string WriteFullClassContent()
{
string output = $@"using {_settings.TargetNamespaceForRepo};
using System.Data;
{UsingDbProviderSpecific}
using Dapper;
using Microsoft.Extensions.Configuration;
";

return output;
return
$$"""
{{tab}}/// <summary>
{{tab}}/// Used when the DBcontext itself is not suffisent to manage its lifecycle
{{tab}}/// Factory specific for the dbprovider {{DbProviderString}}
{{tab}}/// Inherits from factory base
{{tab}}/// </summary>
{{tab}}public class {{_settings.DbContextClassName}}{{DbProviderString}}Factory : {{_settings.DbContextClassName}}FactoryBase, {{("I" + _settings.DbContextClassName)}}Factory
{{tab}}{
{{tab}}{{tab}}public {{_settings.DbContextClassName}}{{DbProviderString}}Factory(IConfiguration config) : base (config)
{{tab}}{{tab}}{
{{tab}}{{tab}}}
{{tab}}{{tab}}
{{tab}}{{tab}}public override I{{_settings.DbContextClassName}} Create()
{{tab}}{{tab}}{
{{tab}}{{tab}}{{tab}}return new {{_settings.DbContextClassName}}{{DbProviderString}}(_config);
{{tab}}{{tab}}}
{{tab}}}
{{tab}}/// <summary>
{{tab}}/// Used when the DBcontext
{{tab}}/// Specific for the dbprovider {{DbProviderString}}
{{tab}}/// Inherits from {{_settings.DbContextClassName}}Base
{{tab}}/// </summary>
{{tab}}public class {{_settings.DbContextClassName}}{{DbProviderString}} : {{_settings.DbContextClassName}}Base, {{("I" + _settings.DbContextClassName)}}
{{tab}}{
{{tab}}{{tab}}public override IDbConnection Connection {get;init;}
{{tab}}{{tab}}
{{@WriteClassRepoMembers()}}
{{tab}}{{tab}}/// <summary>
{{tab}}{{tab}}/// Main constructor, inject standard config : Default connection string
{{tab}}{{tab}}/// </summary>
{{tab}}{{tab}}public {{_settings.DbContextClassName}}{{DbProviderString}}(IConfiguration config) : base (config)
{{tab}}{{tab}}{
{{tab}}{{tab}}{{tab}}{{DapperDefaultMapStrat}}
{{tab}}{{tab}}{{tab}}{{DapperCommandTimeOut}}
{{tab}}{{tab}}{{tab}}{{ConnectionStringInject}}
{{tab}}{{tab}}}
{{tab}}{{tab}}/// <summary>
{{tab}}{{tab}}/// Open a transaction with a specified isolation level
{{tab}}{{tab}}/// </summary>
{{tab}}{{tab}}public override async Task<IDbTransaction> OpenTransactionAsync(IsolationLevel? level = null)
{{tab}}{{tab}}{
{{tab}}{{tab}}{{tab}}if(_trans != null)
{{tab}}{{tab}}{{tab}}{{tab}}throw new Exception("A transaction is already open, you need to use a new {{_settings.DbContextClassName}} for parallel job.");
{{tab}}{{tab}}{{tab}}
{{tab}}{{tab}}{{tab}}if (Connection.State == ConnectionState.Closed)
{{tab}}{{tab}}{{tab}}{
{{tab}}{{tab}}{{tab}}{{tab}}await (({{ConnectionClassName}})Connection).OpenAsync();
{{tab}}{{tab}}{{tab}}}
{{tab}}{{tab}}{{tab}}
{{tab}}{{tab}}{{tab}}_trans = level == null ? Connection.BeginTransaction() : Connection.BeginTransaction((IsolationLevel)level);
{{tab}}{{tab}}{{tab}}
{{tab}}{{tab}}{{tab}}return _trans;
{{tab}}{{tab}}}
{{tab}}}
}
""";
}
protected override string WriteClassRepoMembers()
{
Expand All @@ -74,82 +136,19 @@ protected override string WriteClassRepoMembers()
var repoClassName = tableName + "Repo";
var repoProtectedFieldName = $"_{_stringTransform.FirstCharToLower(repoClassName)}";

return $@"{tab}{tab}public override {interfaceName} {repoClassName}
{tab}{tab}{{
{tab}{tab}{tab}get {{
{tab}{tab}{tab}{tab}{repoProtectedFieldName} ??= new {repoClassName}{DbProviderString}(this);
{tab}{tab}{tab}{tab}return {repoProtectedFieldName};
{tab}{tab}{tab}}}
{tab}{tab}}}
";
return
$$"""
{{tab}}{{tab}}public override {{interfaceName}} {{repoClassName}}
{{tab}}{{tab}}{
{{tab}}{{tab}}{{tab}}get {
{{tab}}{{tab}}{{tab}}{{tab}}{{repoProtectedFieldName}} ??= new {{repoClassName}}{{DbProviderString}}(this);
{{tab}}{{tab}}{{tab}}{{tab}}return {{repoProtectedFieldName}};
{{tab}}{{tab}}{{tab}}}
{{tab}}{{tab}}}
""";
}));

return membersDeclaration;
}

protected override string WriteFullClassContent()
{
return $@"
{tab}/// <summary>
{tab}/// Used when the DBcontext itself is not suffisent to manage its lifecycle
{tab}/// Factory specific for the dbprovider {DbProviderString}
{tab}/// Inherits from factory base
{tab}/// </summary>
{tab}public class {_settings.DbContextClassName}{DbProviderString}Factory : {_settings.DbContextClassName}FactoryBase, {("I" + _settings.DbContextClassName)}Factory
{tab}{{
{tab}{tab}public {_settings.DbContextClassName}{DbProviderString}Factory(IConfiguration config) : base (config)
{tab}{tab}{{
{tab}{tab}}}
{tab}{tab}
{tab}{tab}public override I{_settings.DbContextClassName} Create()
{tab}{tab}{{
{tab}{tab}{tab}return new {_settings.DbContextClassName}{DbProviderString}(_config);
{tab}{tab}}}
{tab}}}
{tab}
{tab}/// <summary>
{tab}/// Used when the DBcontext
{tab}/// Specific for the dbprovider {DbProviderString}
{tab}/// Inherits from {_settings.DbContextClassName}Base
{tab}/// </summary>
{tab}public class {_settings.DbContextClassName}{DbProviderString} : {_settings.DbContextClassName}Base, {("I" + _settings.DbContextClassName)}
{tab}{{
{tab}{tab}public override IDbConnection Connection {{get;init;}}
{tab}{tab}
{@WriteClassRepoMembers()}
{tab}{tab}/// <summary>
{tab}{tab}/// Main constructor, inject standard config : Default connection string
{tab}{tab}/// </summary>
{tab}{tab}public {_settings.DbContextClassName}{DbProviderString}(IConfiguration config) : base (config)
{tab}{tab}{{
{tab}{tab}{tab}{DapperDefaultMapStrat}
{tab}{tab}{tab}{DapperCommandTimeOut}
{tab}{tab}{tab}{ConnectionStringInject}
{tab}{tab}}}
{tab}{tab}
{tab}{tab}/// <summary>
{tab}{tab}/// Open a transaction with a specified isolation level
{tab}{tab}/// </summary>
{tab}{tab}public override async Task<IDbTransaction> OpenTransactionAsync(IsolationLevel? level = null)
{tab}{tab}{{
{tab}{tab}{tab}if(_trans != null)
{tab}{tab}{tab}{tab}throw new Exception(""A transaction is already open, you need to use a new {_settings.DbContextClassName} for parallel job."");
{tab}{tab}{tab}
{tab}{tab}{tab}if (Connection.State == ConnectionState.Closed)
{tab}{tab}{tab}{{
{tab}{tab}{tab}{tab}await (({ConnectionClassName})Connection).OpenAsync();
{tab}{tab}{tab}}}
{tab}{tab}{tab}
{tab}{tab}{tab}_trans = level == null ? Connection.BeginTransaction() : Connection.BeginTransaction((IsolationLevel)level);
{tab}{tab}{tab}
{tab}{tab}{tab}return _trans;
{tab}{tab}}}
{tab}}}
}}
";


}
}
}
Loading

0 comments on commit 303f3a5

Please sign in to comment.