Steps to create DTOs.

Posts   
 
    
npathuru
User
Posts: 17
Joined: 14-Jun-2005
# Posted on: 12-Jul-2007 17:01:00   

Following are some steps i have followed to create DTOs using Pro: 1. Create a DTO.templatebindings using Template Studio. Under the TemplateBindings section: a. Open Template Studio. b. Click on the New button. c. Enter any Name: Dto. d. Enter FileName: C:\Program Files\Solutions Design\LLBLGen Pro v2.0\Templates\DTO.templatebindings e. In Platforms: Select ".Net 2.0".

Under the TemplateID section:
f. Select Target Language: C#
g. Enter TemplateID: Dto
h. Enter FileName: SharedTemplates\Net2.x\C#\dto.template
i. Enter LogiLang: TDL
j. Save and Close.
  1. You are now in Template Bindings Viewer tab.
  2. Click on Edit Selected Files button.
  3. Paste the following code:

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 <[If IsNullable]><[If IsValueType]>Nullable<<[TypeOfField]>><[Else]><[TypeOfField]><[EndIf]><[Else]><[TypeOfField]><[EndIf]> _<[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]>Entity 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]> <[If IsNullable]><[If IsValueType]>Nullable<<[TypeOfField]>><[Else]><[TypeOfField]><[EndIf]><[Else]><[TypeOfField]><[EndIf]> <[EntityFieldName]> { get { return _<[CaseCamel EntityFieldName]>;} set { _<[CaseCamel EntityFieldName]> = value; } }<[NextForeach]> } }

  1. In the folder: C:\Program Files\Solutions Design\LLBLGen Pro v2.0\Tasks, create a file called DTO.Tasks
  2. Paste the following and save:

<?xml version="1.0"?>

    name="DTO Templates" isOptional ="false"
    description="General group of tasks which are used DTO templates.">
<supportedPlatforms>
    <platform name=".NET 2.0" />
</supportedPlatforms>
<supportedTemplateGroups>
    <templateGroup name="SelfServicing" />
</supportedTemplateGroups>

<taskGroup name="Create Directories">
    <task name="DTODirectoryCreator" assemblyFilename="SD.LLBLGen.Pro.TaskPerformers.dll" taskPerformerClass="SD.LLBLGen.Pro.TaskPerformers.DirectoryCreator">
        <parameters>
            <parameter name="folderToCreate" defaultValue="DTO" isOptional="false" description="The folder to create"/>
            <parameter name="failWhenExistent" defaultValue="false" isOptional="true" description="Flag to signal what to do when the folder already exists. Overrules clearWhenExistent" valueType="boolean"/>
            <parameter name="clearWhenExistent" defaultValue="false" isOptional="true" description="Flag to signal if an existing folder has to be cleared first. Overruled by failWhenExistent" valueType="boolean"/>
        </parameters>
    </task>
</taskGroup>

<taskGroup name="Create DTO Classes" description="Create DTO Classes">
    <!--<task name="DTOClassCreator" assemblyFilename="SD.LLBLGen.Pro.LptParser.dll" taskPerformerClass="SD.LLBLGen.Pro.LptParser.DotNetTemplateEngine">-->
    <task name="DTOClassCreator" assemblyFilename="SD.LLBLGen.Pro.TaskPerformers.dll" taskPerformerClass="SD.LLBLGen.Pro.TaskPerformers.CodeEmitter">
        <parameters>
            <parameter isOptional="false" name="destinationFolder" defaultValue="DTO"/>
            <parameter isOptional="false" name="failWhenExistent" defaultValue="false"/>
            <parameter isOptional="false" name="filenameFormat" defaultValue="[elementName]DTO.[extension]"/>
            <!--<parameter isOptional="false" name="templateID" defaultValue="SD_DTOTemplate"/>-->
            <parameter isOptional="false" name="templateID" defaultValue="Dto"/>
            <parameter isOptional="false" name="emitType" defaultValue="allEntities"/>
        </parameters>
    </task>
</taskGroup>

</taskGroup>

  1. Open any of your Llblgen Pro project.
  2. Select Project -> Generate
  3. Click on Template Bindings tab.
  4. You should find Dto, in the listbox Template Bindings Sorted on precedence.
  5. Select it.
  6. Click on Task queue to execute.
  7. Click on Add Tasks button.
  8. Select DTODirectoryCreator, under Create Directories, under DTO Templates.
  9. Click on ok.
  10. Scroll down and locate "SD.Tasks.Generic.FileCreators", Click on it.
  11. Click on Add Tasks button.
  12. Click on Create DTO Classes, under DTO Templates.
  13. Click on ok.
  14. Click on Generate.

Thanks.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 13-Jul-2007 10:18:53   

Thanks for the contribution.

npathuru
User
Posts: 17
Joined: 14-Jun-2005
# Posted on: 18-Jul-2007 23:37:06   

Any ideas, how to create a common base class called DTOBase for the dtos. I looked at EntityBase.template and tried using that but i ended up creating dtoBase for each dto, instead i would like to generate 1 dtobase for all the dtos. Thanks

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 19-Jul-2007 11:12:47   

I don't have a good experience here but I guess you have to exactly follow the steps mentiones above to create the DTO baseClass, but in the Tasks XML config., set the emitType codeEmitter parameter to generic instead of allEntities.

<parameter isOptional="false" name="emitType" defaultValue="generic"/>

Then change the above DTO template let the class inherit from the baseClass you are going to generate.

npathuru
User
Posts: 17
Joined: 14-Jun-2005
# Posted on: 19-Jul-2007 18:50:22   

Thanks Walaa. Much appreciate it.

prabhu
User
Posts: 77
Joined: 20-Dec-2006
# Posted on: 29-Oct-2007 05:12:26   

Hi NPathuru,

The template you had defined above is a single DTO for an individual LLBLGen entity. In our case we are using a single DTO object to transport data between client and WCF Services from multiple entities like typedlist.

Is there a way to produce a template based on the above Business logic or else need to use only the projection available in LLBLGen.

Regards

Prabhu

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 29-Oct-2007 09:38:49   

Why don't you just use TypedLists/DynamicLists in that case?

Otherwise how and where are you going to define such complex DTOs? I guess you'll need some sort of a Designer UI? While you already have the TypedList Designer.