API Entity Code Generator
Our Code Generator is used to automatically create the actual classes for entities, or complex types, you have designed as interfaces against our API. This means you only need create interface definitions of your entities, and complex types, and can refer to them throughout you solution using just the interfaces. This generator will then create actual classes for you that implement those interfaces correctly. You can then add functionality to those classes and use them throughout the solution instead of just the interfaces if you so wish.
API Entity Code Generator Features
When you are designing entities in code using our API, the system can assist you in the manual process of creating all the code to implement those interfaces as classes. Once you have those entities defined in a simple interface you can either implement that interface yourself or use this code generator to automatically create the code that implements those interfaces for you.
Caution
The generated code implements all the interface signatures correctly with the functionality LemonEdge expects. If you are writing these implementations manually you should use the auto-generated code as a template to make sure you don't miss any implementation details.
Tip
If you are using our Custom Entities Auto-Code Designer then that designer can export your entities into code similarly to this generator. The code that designer generates will include the interface definition of the entity itself along with the class implementation of that interface.
The Code Generator will create classes for every interface marked with the following attributes:
- EntityDefinition Attribute
These are your entity interface definitions that implement LemonEdge.API.Core.IBaseEntity. - ComplexDefinition Attribute
These are your complex types, that aren't entities, but are used for data reporting/etc.
Note
See our API documentation for more information on how you define entities by simply creating an interface using our API.
Future Proof
Using the code generator instead of manually implementing all your own classes bring the following benefits:
- Your code is generated much quicker
- There is no chance of typos, or manual errors, involved in writing the same repetitive code for every entity you have
- The generated code implements all aspects of your entity definition including:
- All properties with INotifyPropertyChanged implementations
- Correctly implements ICanTrackProperties
- Adds ability to track properties that are marked as needing tracking
- Implements IDisposable
- Implements ICloneableAsync to ensure your entities can automatically be copied by users
- Implements ISetCopier for interfaces that can be exported into xml packages
- The class also has partial method implementations for every property so you can easily hook into relevant processes of the class however you like without having to modify the generated file.
- The class has the correct attributes for validations
- If we change the way underlying processes work for performance, or other enhancement, reasons this generator would automatically provide that benefit without you having to do anything. You are future proofed with the enhancements we provide.
Views
The Code Generator doesn't have any views for you to interact with. Instead it follows this process:
- Prompts for the location of a compiled dll that holds your LemonEdge entities that have been built against our API dll.
- Prompts for a directory (preferably empty) that you want it to generate all the c# files into.
- The system will then scan that dll for entity, and complex type, declarations in your dll.
- For every entity, or complex type, found it will generate a c# file with the code to implement that interface as an actual class.
The process runs incredibly quickly and will fill the folder you specify with c# code files you can integrate into your dll anyway you like.
Example
In our AssetManagement Accelerator we have a simple entity called Strategy which simply holds the Name of a strategy. Defined as so against our API:
using System;
using System.Collections.Generic;
using System.Text;
using LemonEdge.API.Attributes;
using LemonEdge.API.Attributes.Validation;
using LemonEdge.Utils;
using LemonEdge.Utils.Database;
namespace AssetManagementCore.Entities
{
[EntityDefinition(EntityHelper.StrategyTypeIDString, tableName: "dbo.LT_Strategies", itemName: "Strategy",
LabelColumn = nameof(Name), IsStandingDataEntity = true)]
[DefaultEntityIcon(LemonEdge.Client.Core.Images.ImageType.Strategy)]
public interface IStrategy : LemonEdge.API.Core.IBaseEntityWithPermissions
{
[EntityProperty(LemonEdge.Utils.Database.SQLType.NVarChar, 500, false)]
[EntityDescription("The unique name of this strategy.")]
[System.ComponentModel.DataAnnotations.Required]
[EntityKeyProperty()]
string Name { get; set; }
}
}
Using the Auto Code Generator the system would scan the dll, find this interface implemented against our API, and generate the following class (file name called Auto_Strategy.cs) for it:
/*
Auto Generated file by LemonEdge © 2020
Warning: Any changes to this file will be overwritten if it is regenerated.
*/
using LemonEdge.Utils;
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Text;
using LemonEdge.Entities;
using LemonEdge.API.Core;
using LemonEdge.API.Entities;
using System.Linq;
using System.Threading.Tasks;
using System.Reflection;
using LemonEdge.Core;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Runtime.Serialization;
using AssetManagementCore.Entities;
namespace LemonEdge.Entities
{
[DataContract]
public partial class Strategy :
LemonEdge.API.Core.BaseEntityWithPermissions,
IStrategy,
LemonEdge.Utils.Interfaces.ICloneableAsync
{
[DataMember]
private String _name;
[System.ComponentModel.DataAnnotations.Required]
[System.ComponentModel.DataAnnotations.StringLength(500)]
public String Name
{
get => _name;
set
{
if(_name != value)
{
OnPropertyChanging(nameof(Name));
_name = value;
OnPropertyChanged(nameof(Name));
OnNameChanged();
}
}
}
partial void OnNameChanged();
#region ICloneableAsync Implementation
public void CopyFromSource(IStrategy source) => CopyFromEntity(source);
async Task LemonEdge.Utils.Interfaces.ICloneableAsync.Clone(object context) => (IStrategy) await Clone(context);
protected override void CopyFromEntity(IBaseEntity src)
{
base.CopyFromEntity(src);
var source = (IStrategy)src;
_name = source.Name;
OnCloning(src);
}
partial void OnCloning(IBaseEntity src);
#endregion
public override string ToString() => Name;
}
}