Managing Overwrite Behavior with Templates

In Entity Developer, you can use templates to manage the overwrite behavior of the application.

Changing the output parameters of a template, including management of the override behavior, is implemented using the PushOutputRedirection function call for the internal reserved variable of a template, that is called Output.

The function has many overloads and has the following signature in its full-scale form:

 public void PushOutputRedirection(OutputInfo outputInfo,
                                  string parentFileName,
                                  string targetFileName,
                                  OverwriteMode overwriteMode,
                                  string buildAction,
                                  CopyToOutputDirectory copyToOutputDirectory)

In custom templates you can use the following additional parameters with the PushOutputRedirection function that can be called inside the template code:

the name of the parent project item (the so-called Code Behind). This allows flexibly defining where all generated files should be stored within a large project.
OverwriteBehavior. For partial classes, the Overwrite Behavior functionality enables the developer to manage how already generated files are treated during subsequent generations. This also allows creating and storing unchanged sample files that can be re-used within a different solution. Using predefined templates enables the fine-tuning of overwrite behavior for code generation. At each generation, it is possible to define whether code should be overwritten, left unchanged or whether the newly generated code is to be appended to the existing one.
BuildAction. In Visual Studio, this capability allows selecting an option of how a file should be processed during compilation.
CopyToOutputDirectory. In Visual Studio, this capability allows selecting an option that defines the circumstances, in which the source code should be placed alongside the compiled file.

Typically, every time you save your project, all generated files are overwritten. However, such overwrite behavior might not always be convenient.

The OverwriteMode parameter can have the following values:

None - generated files are not overwritten when the project is saved;
Merge - the new version of generated files is appended to the previous one when the project is saved;
Overwrite - generated files are overwritten when the project is saved.

Additionally, using this technique you can create and store unchanged your sample files for re-use in subsequent iterations.

Below is the example of a template that illustrates how to manage the overwrite behavior.

<#
// Empty template for Devart Entity Developer C# code generation.
// Copyright (c) 2008-2019 Devart. All rights reserved.
#>
<#@ template language="C#" #>
<#@ import namespace="EntityDeveloper.TemplateEngine" #>
<#@ property name="InterfacesOutput" category="Output" type="OutputInfo" #>
<#@ property name="ClassesOutput" category="Output" type="OutputInfo" #>
<#@ extended name="GenerateRepository" type="System.Boolean" owner="Class" default="true" #>
<#
// Settings
  output.Extension = ".cs";
  foreach(Class cls in model.Classes) {
    if (!(bool)cls.GetProperty("GenerateRepository"))
      continue;
 
    output.PushOutputRedirection(InterfacesOutput, "",  "I" + cls.Name + "Repository", OverwriteMode.None);
#>
using System;
using System.Collections.Generic;
 
namespace DataContext {
 
  public interface I<#= codeProvider.GetValidIdentifier(cls.FullName) #>Repository {
     void Insert<#= codeProvider.GetValidIdentifier(cls.FullName) #>(<#= codeProvider.GetValidIdentifier(cls.FullName) #> item);
     void Delete<#= codeProvider.GetValidIdentifier(cls.FullName) #>(<#= codeProvider.GetValidIdentifier(cls.FullName) #> item);
  }
}
<#
    output.PopOutputRedirection();
 
    output.PushOutputRedirection(ClassesOutput, "", cls.Name + "Repository", OverwriteMode.Merge);
#>
using System.Linq;
using System.Collections.Generic;
namespace DataContext {
  public class <#= codeProvider.GetValidIdentifier(cls.FullName) #>Repository : I<#= codeProvider.GetValidIdentifier(cls.FullName) #>Repository {
 
    public <#= codeProvider.GetValidIdentifier(cls.FullName) #>Repository(IDatasource datasource) {
 
      this.Datasource = datasource;
    }
    public void Insert<#= codeProvider.GetValidIdentifier(cls.FullName) #>(<#= codeProvider.GetValidIdentifier(cls.FullName) #> item) {
 
      this.Datasource.Insert<#= codeProvider.GetValidIdentifier(cls.FullName) #>(item);
    }
 
    public void Delete<#= codeProvider.GetValidIdentifier(cls.FullName) #>(<#= codeProvider.GetValidIdentifier(cls.FullName) #> item) {
 
       this.Datasource.Delete<#= codeProvider.GetValidIdentifier(cls.FullName) #>(item);
    }
 
    public IDatasource Datasource {
      get;
      private set;
    }
  }
}
<#
    output.PopOutputRedirection();
  }
#>

 

ExpandedToggleIcon        See Also


Send feedback on this topic

© 2008 - 2024 Devart. All rights reserved.