mattcole wrote:
Hi,
I'm also new to LLBLGen, and was wondering about the potential of having a template that produced light weight "DTO" objects with casting operators to their corresponding LLBL Entities, although I think for a slightly different purpose to you Walaa. I basically want to have my presentation tier use the DTOs and not have knowledge particularly of the LLBLgen generated entities.
Is this something that can be done with a template, and if so does anyone have a template that does this or can point me in the direction of one? I'm only evaluating the software at this stage too, but at this point it seems to fit pretty perfectly with what I am doing.
Thanks,
Matt
That's very easy to do indeed. Something like:
using System;
using System.ComponentModel;
using System.Collections;
using System.Runtime.Serialization;
using <[RootNamespace]>.EntityClasses;
namespace <[RootNamespace]>.DTOClasses
{
/// <summary>
/// DTO class for the entity '<[CurrentEntityName]>'.
/// </summary>
[Serializable]
public <[If UsePartialClasses]>partial <[EndIf]>class <[CurrentEntityName]>DTO : <[ If IsSubType ]><[ SuperTypeName ]>DTO<[ EndIf]>
{
#region Class Member Declarations
<[Foreach EntityField CrLf]> private <[TypeOfField]> _<[CaseCamel EntityFieldName]>;<[NextForeach]>
#endregion
/// <summary>
/// CTor
/// </summary>
public <[CurrentEntityName]>DTO()
{
}
/// <summary>
/// CTor which initializes the DTO with values from its corresponding entity
/// </summary>
/// <param name="entityInstance">The entity instance which holds the values for this DTO</param>
public <[CurrentEntityName]>DTO(<[CurrentEntityName]>Entity entityInstance)<[ If IsSubType ]> : base(entityInstance)<[ EndIf]>
{
<[Foreach EntityField CrLf]> _<[CaseCamel EntityFieldName]> = entityInstance.<[EntityFieldName]>;<[NextForeach]>
}
/// <summary>
/// Creates a new entity instance and copies over the values of this DTO
/// </summary>
public <[CurrentEntityName]>Entity ToEntity()
{
return ToEntity(new <[CurrentEntityName]>Entity());
}
/// <summary>
/// Copies over the values of this DTO into the entity passed in.
/// </summary>
public <[CurrentEntityName]>Entity ToEntity(<[CurrentEntityName]> toFill)
{
<[Foreach EntityField CrLf]> toFill.<[EntityFieldName]> = _<[CaseCamel EntityFieldName]>;<[NextForeach]>
<[ If IsSubType ]> base.ToEntity(toFill);<[ EndIf]>
return toFill;
}
<[Foreach EntityField CrLf]>
/// <summary> The <[EntityFieldName]> property of the Entity <[CurrentEntityName]></summary>
public <[If EntityFieldOverrides]>override<[Else]>virtual<[EndIf]> <[TypeOfField]> <[EntityFieldName]>
{
get { return _<[CaseCamel EntityFieldName]>;}
set { _<[CaseCamel EntityFieldName]> = value; }
}<[NextForeach]>
}
}
Haven't tested it, so it could be that I forgot a ; here and there .
Now, to run this template, you have to add a task to the generator config you're using, e.g. AdapterScenarioFullSafe2003.config
so right ABOVE the last task in that file, add:
<task name="EntitiesDirectoryCreator" assemblyFilename="SD.LLBLGen.Pro.TaskPerformers.dll" taskPerformerClass="SD.LLBLGen.Pro.TaskPerformers.DirectoryCreator">
<parameter name="folderToCreate" value="[dbgenericSubFolder]\DTOClasses"/>
<parameter name="failWhenExistent" value="false"/>
<parameter name="clearWhenExistent" value="false"/>
</task>
<task name="EntityClassGenerator" assemblyFilename="SD.LLBLGen.Pro.TaskPerformers.dll" taskPerformerClass="SD.LLBLGen.Pro.TaskPerformers.CodeEmitter">
<parameter name="destinationFolder" value="[dbgenericSubFolder]\DTOClasses"/>
<parameter name="failWhenExistent" value="false"/>
<parameter name="filenameFormat" value="[elementName]DTO.[extension]"/>
<parameter name="templateID" value="SD_EntityDTOAdapterTemplate"/>
<parameter name="emitType" value="allEntities"/>
</task>
Which will create the folder DTOClasses and will generate for all entities their corresponding class using the template bound to the ID SD_EntityDTOAdapterTemplate.
SD_EntityDTOAdapterTemplate isn't an existing ID, you've to add it to the templateset you're using, in this case for example the SqlServer C# template set. So, go to the Drivers\SqlServer\Templates folder and open CSharpTemplateset.config in notepad. (there's also a tutorial for this in the manual).
Right below the last <templateBinding> tag you add:
<templateBinding templateID="EntityDTOAdapterTemplate" templateFilename="..\..\..\SharedTemplates\C#\entityDTO.template" />
The template I specified above should be saved in the file entityDTO.template and be stored in the folder SharedTemplates\C#
That's it Now you choose Adapter generator configuration for 2003 in the top combobox and the C# templateset in the bottom combobox when you press F7 and you'll get DTO classes for each entity
In v2 of LLBLGen Pro (now in beta) we've made the template/config alternation much easier so in V2 you don't have to mess with config files which contain other tasks or templateset configs which contain other templateID definitions