-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
976 additions
and
499 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="PoweredSoft.CodeGenerator" Version="1.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\PoweredSoft.DbUtils.EF.Generator.Core\PoweredSoft.DbUtils.EF.Generator.Core.csproj" /> | ||
<ProjectReference Include="..\PoweredSoft.DbUtils.Schema.Core\PoweredSoft.DbUtils.Schema.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
using System; | ||
using System.IO; | ||
using PoweredSoft.CodeGenerator; | ||
using PoweredSoft.CodeGenerator.Constants; | ||
using PoweredSoft.CodeGenerator.Extensions; | ||
using PoweredSoft.DbUtils.EF.Generator.Core; | ||
using PoweredSoft.DbUtils.Schema.Core; | ||
|
||
namespace Acme.DbUtils | ||
{ | ||
public class ContextInterceptor : IContextInterceptor | ||
{ | ||
public void InterceptContext(IGenerator generator) | ||
{ | ||
if (!(generator is IGeneratorUsingGenerationContext) || !(generator is IGeneratorWithMeta)) | ||
throw new Exception("Not the kind of generator expected."); | ||
|
||
var gen = generator as IGeneratorUsingGenerationContext; | ||
var genMeta = generator as IGeneratorWithMeta; | ||
var options = gen.GetOptions(); | ||
var gc = gen.GetGenerationContext(); | ||
var contextClassName = genMeta.ContextClassName(); | ||
var contextClass = gc.FindClass(contextClassName); | ||
contextClass.Method(m => m.ReturnType("void").Name("CreateMethodBlah")); | ||
} | ||
} | ||
|
||
public class ResolveTypeInterceptor : IResolveTypeInterceptor | ||
{ | ||
public Tuple<string, bool> InterceptResolveType(IColumn column) | ||
{ | ||
/* | ||
if (column.Table.Name == "Phone" && column.Name == "PhoneTypeId") | ||
return new Tuple<string, bool>("Acme.Enums.PhoneTypes", true);*/ | ||
|
||
return null; | ||
} | ||
} | ||
|
||
public class TableInterceptor : ITableInterceptor | ||
{ | ||
public void InterceptTable(IGenerator generator, ITable table) | ||
{ | ||
if (!(generator is IGeneratorUsingGenerationContext) || !(generator is IGeneratorWithMeta)) | ||
throw new Exception("Not the kind of generator expected."); | ||
|
||
var gen = generator as IGeneratorUsingGenerationContext; | ||
var genMeta = generator as IGeneratorWithMeta; | ||
var options = generator.GetOptions(); | ||
var ctx = gen.GetGenerationContext(); | ||
|
||
|
||
// model. | ||
var modelClassName = genMeta.ModelClassName(table); | ||
var modelClassNamespace = genMeta.ModelNamespace(table); | ||
var modelFullClassName = genMeta.ModelClassFullName(table); | ||
|
||
// poco. | ||
var pocoClassName = genMeta.TableClassName(table); | ||
var pocoClassNamespace = genMeta.TableNamespace(table); | ||
var pocoFullClassName = genMeta.TableClassFullName(table); | ||
|
||
// classes | ||
var pocoClass = ctx.FindClass(pocoClassName, pocoClassNamespace); | ||
|
||
var path = $"{options.OutputDir}{Path.DirectorySeparatorChar}transformations.generated.cs"; | ||
|
||
ctx.File(path, fb => | ||
{ | ||
fb.Namespace("Acme.Models", true, ns => | ||
{ | ||
ns.Class($"{table.Name}ModelTransformationService", true, c => | ||
{ | ||
c.Method(m => | ||
{ | ||
m | ||
.AccessModifier(AccessModifiers.Public) | ||
.Virtual(true) | ||
.Name("ToModel") | ||
.ReturnType("void") | ||
.Parameter(p => p.Name("source").Type(pocoFullClassName)) | ||
.Parameter(p => p.Name("model").Type(modelFullClassName)); | ||
|
||
table.Columns.ForEach(column => | ||
{ | ||
m.RawLine($"model.{column.Name} = source.{column.Name}"); | ||
}); | ||
}); | ||
|
||
c.Method(m => | ||
{ | ||
m | ||
.AccessModifier(AccessModifiers.Public) | ||
.Virtual(true) | ||
.Name("FromModel") | ||
.ReturnType("void") | ||
.Parameter(p => p.Name("model").Type(modelFullClassName)) | ||
.Parameter(p => p.Name("destination").Type(pocoFullClassName)); | ||
|
||
table.Columns.ForEach(column => | ||
{ | ||
bool isPropertyNullable = | ||
column.IsNullable || options.GenerateModelPropertyAsNullable; | ||
if (isPropertyNullable && !column.IsNullable) | ||
{ | ||
var matchingProp = pocoClass.FindByMeta<PropertyBuilder>(column); | ||
var ternary = TernaryBuilder | ||
.Create() | ||
.RawCondition(rc => rc.Condition($"model.{column.Name} != null")) | ||
.True(RawInlineBuilder.Create( | ||
$"destination.{column.Name} = ({matchingProp.GetTypeName()})model.{column.Name}")) | ||
.False(RawInlineBuilder.Create( | ||
$"destination.{column.Name} = default({matchingProp.GetTypeName()})")); | ||
|
||
m.RawLine($"destination.{column.Name} = {ternary.GenerateInline()}"); | ||
} | ||
else | ||
{ | ||
m.RawLine($"destination.{column.Name} = model.{column.Name}"); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
PoweredSoft.DbUtils.EF.Generator.Core/IContextInterceptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using PoweredSoft.CodeGenerator; | ||
|
||
namespace PoweredSoft.DbUtils.EF.Generator.Core | ||
{ | ||
public interface IContextInterceptor | ||
{ | ||
void InterceptContext(IGenerator generator); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
PoweredSoft.DbUtils.EF.Generator.Core/ITableInterceptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using PoweredSoft.CodeGenerator; | ||
using PoweredSoft.DbUtils.Schema.Core; | ||
|
||
namespace PoweredSoft.DbUtils.EF.Generator.Core | ||
{ | ||
public interface ITableInterceptor | ||
{ | ||
void InterceptTable(IGenerator generator, ITable table); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
PoweredSoft.DbUtils.EF.Generator.Core/ResolveTypeInterceptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using PoweredSoft.DbUtils.Schema.Core; | ||
|
||
namespace PoweredSoft.DbUtils.EF.Generator.Core | ||
{ | ||
public interface IResolveTypeInterceptor | ||
{ | ||
/// <summary> | ||
/// Must return type name and if its a value type. | ||
/// </summary> | ||
/// <param name="column"></param> | ||
/// <returns></returns> | ||
Tuple<string, bool> InterceptResolveType(IColumn column); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.