33
44// Modifications copyright(C) 2018 Tony Sneed.
55
6+ using System ;
67using System . IO ;
8+ using EntityFrameworkCore . Scaffolding . Handlebars . Helpers ;
79using Microsoft . EntityFrameworkCore . Metadata ;
810using Microsoft . EntityFrameworkCore . Metadata . Internal ;
911using Microsoft . EntityFrameworkCore . Scaffolding ;
@@ -18,6 +20,16 @@ public class HbsCSharpModelGenerator : ModelCodeGenerator
1820 {
1921 private const string FileExtension = ".cs" ;
2022
23+ /// <summary>
24+ /// DbContext template service.
25+ /// </summary>
26+ public virtual IDbContextTemplateService DbContextTemplateService { get ; }
27+
28+ /// <summary>
29+ /// Entity type template service.
30+ /// </summary>
31+ public virtual IEntityTypeTemplateService EntityTypeTemplateService { get ; }
32+
2133 /// <summary>
2234 /// DbContext generator.
2335 /// </summary>
@@ -32,14 +44,20 @@ public class HbsCSharpModelGenerator : ModelCodeGenerator
3244 /// Constructor for the HbsCSharpModelGenerator.
3345 /// </summary>
3446 /// <param name="dependencies">Service dependencies parameter class for HbsCSharpModelGenerator.</param>
47+ /// <param name="dbContextTemplateService">Template service for DbContext generator.</param>
48+ /// <param name="entityTypeTemplateService">Template service for the entity types generator.</param>
3549 /// <param name="cSharpDbContextGenerator">DbContext generator.</param>
3650 /// <param name="cSharpEntityTypeGenerator">Entity type generator.</param>
3751 public HbsCSharpModelGenerator ( ModelCodeGeneratorDependencies dependencies ,
52+ IDbContextTemplateService dbContextTemplateService ,
53+ IEntityTypeTemplateService entityTypeTemplateService ,
3854 ICSharpDbContextGenerator cSharpDbContextGenerator ,
3955 ICSharpEntityTypeGenerator cSharpEntityTypeGenerator ) : base ( dependencies )
4056 {
41- CSharpDbContextGenerator = cSharpDbContextGenerator ;
42- CSharpEntityTypeGenerator = cSharpEntityTypeGenerator ;
57+ DbContextTemplateService = dbContextTemplateService ?? throw new ArgumentNullException ( nameof ( dbContextTemplateService ) ) ;
58+ EntityTypeTemplateService = entityTypeTemplateService ?? throw new ArgumentNullException ( nameof ( entityTypeTemplateService ) ) ;
59+ CSharpDbContextGenerator = cSharpDbContextGenerator ?? throw new ArgumentNullException ( nameof ( cSharpDbContextGenerator ) ) ;
60+ CSharpEntityTypeGenerator = cSharpEntityTypeGenerator ?? throw new ArgumentNullException ( nameof ( cSharpEntityTypeGenerator ) ) ;
4361 }
4462
4563 /// <summary>Generates code for a model.</summary>
@@ -57,19 +75,40 @@ public override ScaffoldedModel GenerateModel(IModel model,
5775 string connectionString ,
5876 ModelCodeGenerationOptions options )
5977 {
78+ if ( model == null ) throw new ArgumentNullException ( nameof ( model ) ) ;
79+ if ( string . IsNullOrWhiteSpace ( @namespace ) ) throw new ArgumentNullException ( nameof ( @namespace ) ) ;
80+ if ( contextDir == null ) throw new ArgumentNullException ( nameof ( contextDir ) ) ;
81+ if ( string . IsNullOrWhiteSpace ( contextName ) ) throw new ArgumentNullException ( nameof ( contextName ) ) ;
82+ if ( string . IsNullOrWhiteSpace ( connectionString ) ) throw new ArgumentNullException ( nameof ( connectionString ) ) ;
83+ if ( contextDir == null ) throw new ArgumentNullException ( nameof ( contextDir ) ) ;
84+ if ( options == null ) throw new ArgumentNullException ( nameof ( options ) ) ;
85+
86+ // Register Hbs helpers and partial templates
87+ DbContextTemplateService . RegisterHelper ( Constants . SpacesHelper , HandlebarsHelpers . GetSpacesHelper ( ) ) ;
88+ DbContextTemplateService . RegisterPartialTemplates ( ) ;
89+ EntityTypeTemplateService . RegisterPartialTemplates ( ) ;
90+
6091 var resultingFiles = new ScaffoldedModel ( ) ;
6192
62- var generatedCode = CSharpDbContextGenerator . WriteCode ( model , @namespace , contextName , connectionString , options . UseDataAnnotations , options . SuppressConnectionStringWarning ) ;
93+ string generatedCode ;
6394
64- var dbContextFileName = contextName + FileExtension ;
65- resultingFiles . ContextFile = new ScaffoldedFile { Path = Path . Combine ( contextDir , dbContextFileName ) , Code = generatedCode } ;
95+ if ( ! ( CSharpDbContextGenerator is NullCSharpDbContextGenerator ) )
96+ {
97+ generatedCode = CSharpDbContextGenerator . WriteCode ( model , @namespace , contextName , connectionString , options . UseDataAnnotations , options . SuppressConnectionStringWarning ) ;
98+
99+ var dbContextFileName = contextName + FileExtension ;
100+ resultingFiles . ContextFile = new ScaffoldedFile { Path = Path . Combine ( contextDir , dbContextFileName ) , Code = generatedCode } ;
101+ }
66102
67- foreach ( var entityType in model . GetEntityTypes ( ) )
103+ if ( ! ( CSharpEntityTypeGenerator is NullCSharpEntityTypeGenerator ) )
68104 {
69- generatedCode = CSharpEntityTypeGenerator . WriteCode ( entityType , @namespace , options . UseDataAnnotations ) ;
105+ foreach ( var entityType in model . GetEntityTypes ( ) )
106+ {
107+ generatedCode = CSharpEntityTypeGenerator . WriteCode ( entityType , @namespace , options . UseDataAnnotations ) ;
70108
71- var entityTypeFileName = entityType . DisplayName ( ) + FileExtension ;
72- resultingFiles . AdditionalFiles . Add ( new ScaffoldedFile { Path = entityTypeFileName , Code = generatedCode } ) ;
109+ var entityTypeFileName = entityType . DisplayName ( ) + FileExtension ;
110+ resultingFiles . AdditionalFiles . Add ( new ScaffoldedFile { Path = entityTypeFileName , Code = generatedCode } ) ;
111+ }
73112 }
74113
75114 return resultingFiles ;
0 commit comments