Skip to content

Commit 8ac084d

Browse files
author
David Lebee
committed
Merge branch 'release/1.1.91'
2 parents a485a51 + 0df992c commit 8ac084d

File tree

36 files changed

+976
-499
lines changed

36 files changed

+976
-499
lines changed

Acme.DbUtils/Acme.DbUtils.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="PoweredSoft.CodeGenerator" Version="1.0.1" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\PoweredSoft.DbUtils.EF.Generator.Core\PoweredSoft.DbUtils.EF.Generator.Core.csproj" />
13+
<ProjectReference Include="..\PoweredSoft.DbUtils.Schema.Core\PoweredSoft.DbUtils.Schema.Core.csproj" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
using System;
2+
using System.IO;
3+
using PoweredSoft.CodeGenerator;
4+
using PoweredSoft.CodeGenerator.Constants;
5+
using PoweredSoft.CodeGenerator.Extensions;
6+
using PoweredSoft.DbUtils.EF.Generator.Core;
7+
using PoweredSoft.DbUtils.Schema.Core;
8+
9+
namespace Acme.DbUtils
10+
{
11+
public class ContextInterceptor : IContextInterceptor
12+
{
13+
public void InterceptContext(IGenerator generator)
14+
{
15+
if (!(generator is IGeneratorUsingGenerationContext) || !(generator is IGeneratorWithMeta))
16+
throw new Exception("Not the kind of generator expected.");
17+
18+
var gen = generator as IGeneratorUsingGenerationContext;
19+
var genMeta = generator as IGeneratorWithMeta;
20+
var options = gen.GetOptions();
21+
var gc = gen.GetGenerationContext();
22+
var contextClassName = genMeta.ContextClassName();
23+
var contextClass = gc.FindClass(contextClassName);
24+
contextClass.Method(m => m.ReturnType("void").Name("CreateMethodBlah"));
25+
}
26+
}
27+
28+
public class ResolveTypeInterceptor : IResolveTypeInterceptor
29+
{
30+
public Tuple<string, bool> InterceptResolveType(IColumn column)
31+
{
32+
/*
33+
if (column.Table.Name == "Phone" && column.Name == "PhoneTypeId")
34+
return new Tuple<string, bool>("Acme.Enums.PhoneTypes", true);*/
35+
36+
return null;
37+
}
38+
}
39+
40+
public class TableInterceptor : ITableInterceptor
41+
{
42+
public void InterceptTable(IGenerator generator, ITable table)
43+
{
44+
if (!(generator is IGeneratorUsingGenerationContext) || !(generator is IGeneratorWithMeta))
45+
throw new Exception("Not the kind of generator expected.");
46+
47+
var gen = generator as IGeneratorUsingGenerationContext;
48+
var genMeta = generator as IGeneratorWithMeta;
49+
var options = generator.GetOptions();
50+
var ctx = gen.GetGenerationContext();
51+
52+
53+
// model.
54+
var modelClassName = genMeta.ModelClassName(table);
55+
var modelClassNamespace = genMeta.ModelNamespace(table);
56+
var modelFullClassName = genMeta.ModelClassFullName(table);
57+
58+
// poco.
59+
var pocoClassName = genMeta.TableClassName(table);
60+
var pocoClassNamespace = genMeta.TableNamespace(table);
61+
var pocoFullClassName = genMeta.TableClassFullName(table);
62+
63+
// classes
64+
var pocoClass = ctx.FindClass(pocoClassName, pocoClassNamespace);
65+
66+
var path = $"{options.OutputDir}{Path.DirectorySeparatorChar}transformations.generated.cs";
67+
68+
ctx.File(path, fb =>
69+
{
70+
fb.Namespace("Acme.Models", true, ns =>
71+
{
72+
ns.Class($"{table.Name}ModelTransformationService", true, c =>
73+
{
74+
c.Method(m =>
75+
{
76+
m
77+
.AccessModifier(AccessModifiers.Public)
78+
.Virtual(true)
79+
.Name("ToModel")
80+
.ReturnType("void")
81+
.Parameter(p => p.Name("source").Type(pocoFullClassName))
82+
.Parameter(p => p.Name("model").Type(modelFullClassName));
83+
84+
table.Columns.ForEach(column =>
85+
{
86+
m.RawLine($"model.{column.Name} = source.{column.Name}");
87+
});
88+
});
89+
90+
c.Method(m =>
91+
{
92+
m
93+
.AccessModifier(AccessModifiers.Public)
94+
.Virtual(true)
95+
.Name("FromModel")
96+
.ReturnType("void")
97+
.Parameter(p => p.Name("model").Type(modelFullClassName))
98+
.Parameter(p => p.Name("destination").Type(pocoFullClassName));
99+
100+
table.Columns.ForEach(column =>
101+
{
102+
bool isPropertyNullable =
103+
column.IsNullable || options.GenerateModelPropertyAsNullable;
104+
if (isPropertyNullable && !column.IsNullable)
105+
{
106+
var matchingProp = pocoClass.FindByMeta<PropertyBuilder>(column);
107+
var ternary = TernaryBuilder
108+
.Create()
109+
.RawCondition(rc => rc.Condition($"model.{column.Name} != null"))
110+
.True(RawInlineBuilder.Create(
111+
$"destination.{column.Name} = ({matchingProp.GetTypeName()})model.{column.Name}"))
112+
.False(RawInlineBuilder.Create(
113+
$"destination.{column.Name} = default({matchingProp.GetTypeName()})"));
114+
115+
m.RawLine($"destination.{column.Name} = {ternary.GenerateInline()}");
116+
}
117+
else
118+
{
119+
m.RawLine($"destination.{column.Name} = model.{column.Name}");
120+
}
121+
});
122+
});
123+
});
124+
});
125+
});
126+
}
127+
}
128+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using PoweredSoft.CodeGenerator;
5+
6+
namespace PoweredSoft.DbUtils.EF.Generator.Core
7+
{
8+
public interface IContextInterceptor
9+
{
10+
void InterceptContext(IGenerator generator);
11+
}
12+
}

PoweredSoft.DbUtils.EF.Generator.Core/IGenerator.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using PoweredSoft.CodeGenerator;
24
using PoweredSoft.DbUtils.Schema.Core;
35

46
namespace PoweredSoft.DbUtils.EF.Generator.Core
@@ -11,6 +13,7 @@ public interface IGenerator
1113
List<ISequence> ResolveSequencesToGenerate();
1214
IGeneratorOptions GetOptions();
1315
IGeneratorOptions GetDefaultOptions();
16+
IDataTypeResolver DataTypeResolver { get; }
1417
void InitializeOptionsWithDefault();
1518
}
1619

@@ -19,4 +22,33 @@ public interface IGenerator<TOptions> : IGenerator
1922
{
2023
TOptions Options { get; }
2124
}
25+
26+
public interface IGeneratorUsingGenerationContext : IGenerator
27+
{
28+
GenerationContext GetGenerationContext();
29+
}
30+
31+
public interface IGeneratorWithMeta : IGenerator
32+
{
33+
string ContextClassName();
34+
string ContextFullClassName();
35+
string ContextNamespace();
36+
string EmptyMetas(string text);
37+
string ModelClassName(ITable table);
38+
string ModelNamespace(ITable table);
39+
string ModelClassFullName(ITable table);
40+
string ModelInterfaceName(ITable table);
41+
string Pluralize(string text);
42+
string ReplaceMetas(string text);
43+
string TableClassFullName(ITable table);
44+
string TableClassName(ITable table);
45+
string TableInterfaceName(ITable table);
46+
string TableInterfaceNamespace(ITable table);
47+
string TableNamespace(ITable table);
48+
string ModelInterfaceNamespace(ITable table);
49+
Tuple<string, bool> GetColumnTypeInfo(IColumn column);
50+
string GetColumnTypeName(IColumn column, bool alwaysAsNullable = false);
51+
}
52+
53+
2254
}

PoweredSoft.DbUtils.EF.Generator.Core/IGeneratorOptions.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public interface IGeneratorOptions
1212
string ConnectionString { get; set; }
1313
string OutputDir { get; set; }
1414
bool CleanOutputDir { get; set; }
15-
bool OutputToSingleFile { get; }
1615
bool GenerateContextSequenceMethods { get; set; }
1716
string OutputSingleFileName { get; set; }
1817
bool GenerateInterfaces { get; set; }
@@ -25,5 +24,28 @@ public interface IGeneratorOptions
2524
List<string> ModelInheritances { get; set; }
2625
string Version { get; }
2726
string Engine { get; }
27+
28+
// namespace overrides.
29+
string EntityNamespace { get; set; }
30+
string EntityInterfaceNamespace { get; set; }
31+
string ModelInterfaceNamespace { get; set; }
32+
string ModelNamespace { get; set; }
33+
string ContextNamespace { get; set; }
34+
35+
// file overrides.
36+
string EntitiesOutputSingleFileName { get; set; }
37+
string EntitiesInterfacesOutputSingleFileName { get; set; }
38+
string ModelsInterfacesOutputSingleFileName { get; set; }
39+
string ModelsOutputSingleFileName { get; set; }
40+
string ContextOutputSingleFileName { get; set; }
41+
42+
// output dir overrides.
43+
string EntitiesOutputDir { get; set; }
44+
string EntitiesInterfacesOutputDir { get; set; }
45+
string ModelsInterfacesOutputDir { get; set; }
46+
string ModelsOutputDir { get; set; }
47+
string ContextOutputDir { get; set; }
48+
49+
List<string> DynamicAssemblies { get; set; }
2850
}
2951
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using PoweredSoft.CodeGenerator;
5+
using PoweredSoft.DbUtils.Schema.Core;
6+
7+
namespace PoweredSoft.DbUtils.EF.Generator.Core
8+
{
9+
public interface ITableInterceptor
10+
{
11+
void InterceptTable(IGenerator generator, ITable table);
12+
}
13+
}

PoweredSoft.DbUtils.EF.Generator.Core/PoweredSoft.DbUtils.EF.Generator.Core.csproj

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,21 @@
77
<PackageProjectUrl>https://github.com/PoweredSoft/DbUtils</PackageProjectUrl>
88
<RepositoryUrl>https://github.com/PoweredSoft/DbUtils/PoweredSoft.DbUtils.EF.Generator.Core</RepositoryUrl>
99
<RepositoryType>github</RepositoryType>
10-
<PackageIconUrl></PackageIconUrl>
11-
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
12-
<Version>1.0.4</Version>
10+
<PackageIconUrl>https://secure.gravatar.com/avatar/4e32f73820c16718909a06c2927f1f8b?s=512&amp;r=g&amp;d=retro</PackageIconUrl>
11+
<Version>1.1.0$(VersionSuffix)</Version>
12+
<Product>PoweredSoft.DbUtils.EF.Generator.Core</Product>
13+
<Description>update</Description>
14+
<PackageId>PoweredSoft.DbUtils.EF.Generator.Core</PackageId>
15+
<PackageReleaseNotes>made something virtual</PackageReleaseNotes>
16+
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
17+
<Company>PoweredSoft.DbUtils.EF.Generator.Core</Company>
18+
<Authors>PoweredSoft.DbUtils.EF.Generator.Core</Authors>
1319
</PropertyGroup>
1420

21+
<ItemGroup>
22+
<PackageReference Include="PoweredSoft.CodeGenerator" Version="1.0.1" />
23+
</ItemGroup>
24+
1525
<ItemGroup>
1626
<ProjectReference Include="..\PoweredSoft.DbUtils.Schema.Core\PoweredSoft.DbUtils.Schema.Core.csproj" />
1727
</ItemGroup>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using PoweredSoft.DbUtils.Schema.Core;
5+
6+
namespace PoweredSoft.DbUtils.EF.Generator.Core
7+
{
8+
public interface IResolveTypeInterceptor
9+
{
10+
/// <summary>
11+
/// Must return type name and if its a value type.
12+
/// </summary>
13+
/// <param name="column"></param>
14+
/// <returns></returns>
15+
Tuple<string, bool> InterceptResolveType(IColumn column);
16+
}
17+
}

PoweredSoft.DbUtils.EF.Generator.EF6.Core/PoweredSoft.DbUtils.EF.Generator.EF6.Core.csproj

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<Version>1.0.4</Version>
5+
<Version>1.1.0$(VersionSuffix)</Version>
76
<Copyright>Powered Softwares Inc.</Copyright>
87
<PackageLicenseUrl>https://github.com/PoweredSoft/DbUtils/blob/master/LICENSE.md</PackageLicenseUrl>
98
<PackageProjectUrl>https://github.com/PoweredSoft/DbUtils</PackageProjectUrl>
109
<RepositoryUrl>https://github.com/PoweredSoft/DbUtils/PoweredSoft.DbUtils.EF.Generator.EF6.Core</RepositoryUrl>
1110
<RepositoryType>github</RepositoryType>
12-
<PackageIconUrl></PackageIconUrl>
13-
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
11+
<PackageIconUrl>https://secure.gravatar.com/avatar/4e32f73820c16718909a06c2927f1f8b?s=512&amp;r=g&amp;d=retro</PackageIconUrl>
1412
</PropertyGroup>
1513

14+
<ItemGroup>
15+
<PackageReference Include="PoweredSoft.CodeGenerator" Version="1.0.1" />
16+
</ItemGroup>
17+
1618
<ItemGroup>
1719
<ProjectReference Include="..\PoweredSoft.DbUtils.EF.Generator.Core\PoweredSoft.DbUtils.EF.Generator.Core.csproj" />
1820
</ItemGroup>

PoweredSoft.DbUtils.EF.Generator.EF6.SqlServer/DatabaseGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace PoweredSoft.DbUtils.EF.Generator.EF6.SqlServer
1010
{
1111
public class DatabaseGenerator : EF6DatabaseGeneratorBase<IDatabaseSchema, GeneratorOptions>
1212
{
13-
protected override IDataTypeResolver DataTypeResolver { get; } = new DataTypeResolver();
13+
public override IDataTypeResolver DataTypeResolver { get; } = new DataTypeResolver();
1414
public override IDatabaseSchema CreateSchema() => new DatabaseSchema();
1515
public override IGeneratorOptions GetDefaultOptions() => new GeneratorOptions();
1616

@@ -25,8 +25,8 @@ protected override void GenerateGetNextSequenceLines(MethodBuilder method, strin
2525
method.RawLine($"return Database.SqlQuery<{outputType}>(\"SELECT NEXT VALUE FOR [{sqlServerSequence.Schema}].[{sequence.Name}];\").First()");
2626
}
2727

28-
protected override string EmptyMetas(string text) => base.EmptyMetas(text).EmptyMetas();
29-
protected override string ReplaceMetas(string text, ITable table) => base.ReplaceMetas(text, table).ReplaceMetas((Table)table);
28+
public override string EmptyMetas(string text) => base.EmptyMetas(text).EmptyMetas();
29+
public override string ReplaceMetas(string text, ITable table) => base.ReplaceMetas(text, table).ReplaceMetas((Table)table);
3030
public override List<ITable> ResolveTablesToGenerate() => base.ResolveTablesToGenerate().ShouldGenerate(Options);
3131
public override List<ISequence> ResolveSequencesToGenerate() => base.ResolveSequencesToGenerate().ShouldGenerate(Options);
3232

PoweredSoft.DbUtils.EF.Generator.EF6.SqlServer/GeneratorOptions.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public class GeneratorOptions : IEF6GeneratorOptions, ISqlServerGeneratorOptions
2020
public string ConnectionString { get; set; }
2121
public string OutputDir { get; set; }
2222
public bool CleanOutputDir { get; set; } = false;
23-
public bool OutputToSingleFile => !string.IsNullOrWhiteSpace(OutputSingleFileName);
2423
public bool GenerateContextSequenceMethods { get; set; }
2524
public string OutputSingleFileName { get; set; }
2625
public bool GenerateInterfaces { get; set; } = false;
@@ -33,6 +32,22 @@ public class GeneratorOptions : IEF6GeneratorOptions, ISqlServerGeneratorOptions
3332
public List<string> ModelInheritances { get; set; } = new List<string>();
3433
public string Version => "6";
3534
public string Engine => "SqlServer";
35+
public string EntityNamespace { get; set; }
36+
public string EntityInterfaceNamespace { get; set; }
37+
public string ModelInterfaceNamespace { get; set; }
38+
public string ModelNamespace { get; set; }
39+
public string ContextNamespace { get; set; }
40+
public string EntitiesOutputSingleFileName { get; set; }
41+
public string EntitiesInterfacesOutputSingleFileName { get; set; }
42+
public string ModelsInterfacesOutputSingleFileName { get; set; }
43+
public string ModelsOutputSingleFileName { get; set; }
44+
public string ContextOutputSingleFileName { get; set; }
45+
public string EntitiesOutputDir { get; set; }
46+
public string EntitiesInterfacesOutputDir { get; set; }
47+
public string ModelsInterfacesOutputDir { get; set; }
48+
public string ModelsOutputDir { get; set; }
49+
public string ContextOutputDir { get; set; }
50+
public List<string> DynamicAssemblies { get; set; }
3651
public string ConnectionStringName { get; set; }
3752
public string FluentConfigurationClassSuffix { get; set; } = "FluentConfiguration";
3853
public List<string> IncludedSchemas { get; } = new List<string>();

0 commit comments

Comments
 (0)