Can Entity Developer create interfaces?

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
edowney
Posts: 14
Joined: Thu 07 Mar 2013 13:10

Can Entity Developer create interfaces?

Post by edowney » Thu 07 Mar 2013 15:27

Like the title says. I usually create an interface for my entities. Does ED do that as well?

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Can Entity Developer create interfaces?

Post by Shalex » Tue 12 Mar 2013 17:18

1. Add a new custom template in your model with the following code:

Code: Select all

<#@ template language="C#" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Data.Entity" #>
<#@ assembly name="System.Data.Entity.Design" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Linq" #>
<#@ property name="HeaderTimestampVersionControlTag" category="Generation" type="System.String" description="If this option is set, the standard date/time-stamp in the file header will be replaced with the specified tag (e.g. a version control tag for Subversion, Git, etc.)" #>
<#@ property name="InterfacesOutput" category="Output" type="OutputInfo" editor="OutputInfoEditor" description="Specifies output for the generated entity classes." #>
<#
  // Begin generation
  string namespaceName = codeProvider.GetValidIdentifier(model.Namespace);

  //------------------------------------------------------------------------------
  // Interface generation for entities
  //------------------------------------------------------------------------------
  foreach (EntityClass cls in model.Classes) {

            output.Extension = ".cs";
            output.PushOutputRedirection(InterfacesOutput, "", model.FileName + ".I" + cls.Name, OverwriteMode.Overwrite);
            GenerateFileHeader();
#>

    namespace <#= namespaceName #>
    {

        <#= codeProvider.FormatClassAccess(cls.Access) #> interface <#= codeProvider.GetValidIdentifier("I" + cls.Name) #>
        {

<#
          foreach (EntityProperty property in cls.Properties) {
         
            string currentPropertyType = codeProvider.GetNullableType(property.Nullable, property.Type);
            string inheritanceModifier = property.InheritanceModifier == MemberInheritanceModifier.None ? "" : " " + codeProvider.FormatMemberInheritanceModifier(property.InheritanceModifier);
#>
            <#= inheritanceModifier #> <#= currentPropertyType #> <#= codeProvider.GetValidIdentifier(property.Name) #>
            {
                get;
                set;
            }

<#
          }
         
          foreach (EntityRelationProperty relationProperty in cls.RelationProperties)
            if (relationProperty.Generate) {
              string inheritanceModifier = relationProperty.InheritanceModifier == MemberInheritanceModifier.None ? "" : " " + codeProvider.FormatMemberInheritanceModifier(relationProperty.InheritanceModifier);
              if (relationProperty.Multiplicity == Multiplicity.Many) { // if Multiplicity.Many
#>
            <#= inheritanceModifier #> EntityCollection<<#= codeProvider.GetValidIdentifier(relationProperty.RelationClass.Name) #>> <#= codeProvider.GetValidIdentifier(relationProperty.Name) #>
<#
              }
              else{
#>
            <#= inheritanceModifier #> <#= codeProvider.GetValidIdentifier(relationProperty.RelationClass.Name) #> <#= codeProvider.GetValidIdentifier(relationProperty.Name) #>
<#           
              }
#>
            {
                get;
                set;
            }

<#
            }
#>
        }
    }
<#
            output.PopOutputRedirection();
  }
// End of generation
#>
<#+
  //////////////////////////////////////////////////////////////////////////////////
  //
  // Method GenerateFileHeader()
  // Header comments for each generated file.
  //
  //////////////////////////////////////////////////////////////////////////////////
  private void GenerateFileHeader() {
#>
//------------------------------------------------------------------------------
// This is auto-generated code.
//------------------------------------------------------------------------------
// This code was generated by Devart Entity Developer tool using Entity Framework EntityObject template.
// <#= String.IsNullOrEmpty(HeaderTimestampVersionControlTag) ? "Code is generated on: " + DateTime.Now : HeaderTimestampVersionControlTag #>
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//------------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Runtime.Serialization;
using System.Xml.Serialization;
<#+
    if (model.Settings.EntityFrameworkVersion < EntityFrameworkVersion.Version6) {
#>
using System.Data.EntityClient;
using System.Data.Metadata.Edm;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
<#+
    }
    else {
#>
using System.Data.Entity.Core.EntityClient;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Core.Objects.DataClasses;
<#+
    }
  }
#>
2. Set the File Per Class property of the EnitytObject template to True and modify the template. Replace:

Code: Select all

    <#= classAccess #> <#= classInheritanceModifier #>partial class <#= currentClassName #> : <#= baseList #><# if (ImplementCloneable) { #>, ICloneable<# } #>
with

Code: Select all

            <#= classAccess #> <#= classInheritanceModifier #>partial class <#= currentClassName #> : <#= baseList #>, <#= codeProvider.GetValidIdentifier("I" + cls.Name) #> <# if (ImplementCloneable) { #>, ICloneable<# } #>
If you want the files with interfaces be generated in separate folders, implement the changes in the custom template which are described at http://forums.devart.com/viewtopic.php?f=32&t=26144.

edowney
Posts: 14
Joined: Thu 07 Mar 2013 13:10

Re: Can Entity Developer create interfaces?

Post by edowney » Tue 27 Aug 2013 12:36

Wow, that's great! I just finally got around to purchasing ED so I'm committed to learning it now ;)

I looked up adding a template in the help section and followed that to the point where you are at the "Add Template" window. Is that how I create a custom template? And if so which one of the types of templates do I select?

Thanx!

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Can Entity Developer create interfaces?

Post by Shalex » Wed 28 Aug 2013 15:50

This is the way how to add a new custom template in your model: navigate to Tools > Entity Developer > Model Explorer, right click on the Templates node > New Blank Template.

Another way: add any predefined template (right click on Templates > Add Template), then right click on a newly added template > Copy to Model Folder. After this, open it (by double click) and modify with embedded T4 Editor.

Believe2014
Posts: 1
Joined: Fri 25 Apr 2014 14:27

Re: Can Entity Developer create interfaces?

Post by Believe2014 » Fri 25 Apr 2014 14:28

Alternatively, you can download my project on Codeplex. I have a working example that you can copy.
https://entityinterfacegenerator.codeplex.com/

It generates the interface files that you need for IoC purposes.

nolme
Posts: 2
Joined: Tue 12 Sep 2017 20:07

Re: Can Entity Developer create interfaces?

Post by nolme » Tue 12 Sep 2017 20:10

3 years later. Is there any improvements on the way to implement Interface ?

Vincent

Pinturiccio
Devart Team
Posts: 2420
Joined: Wed 02 Nov 2011 09:44

Re: Can Entity Developer create interfaces?

Post by Pinturiccio » Fri 15 Sep 2017 11:40

We provided the code of a new custom template above. You can use it and create your custom template.

Please describe what improvements you would like to see.

Post Reply