Generating DTOs

Posts   
1  /  2
 
    
Roman
User
Posts: 23
Joined: 19-May-2006
# Posted on: 19-May-2006 00:44:03   

Hi,

I'm very new to LLBLGen (still evaluating it), so forgive me if the answers to some of my questions are obvious.

I have the requirement to pass business object data from a remote service webapp to a client via web services. The catch is that the client is non-.NET (Visual Fox Pro 9.0) and persists the data with its own data model. Passing entities generated by LLBLGen is not an option then. I see two possibilities (correct me if I'm wrong):

  1. Output the business entity to compact XML and send that - the client would load it up into an XML document and parse it. I don't really like that approach, plus I'm not even sure it would work.

  2. The other option is to have a Data Transfer Object (DTO) for each entity - a very lightweight class with just some properties - a minimum of what needs to be sent over. Entities could then be converted into DTOs and vice versa. These DTOs would only be used to transfer data over XML web services. I prefer this approach.

I could code the DTOs by hand, but I don't see why this couldn't be automated. So.. what are options for having the DTO lightweight classes generated along with the entity classes? Would I need to write a new task and add it to a custom template?

How would v2.0 help me out? I know that a new projection feature will be coming that should simplify this, but I don't know anything about how that would work.

Thank you!

sparmar2000 avatar
Posts: 341
Joined: 30-Nov-2003
# Posted on: 20-May-2006 17:45:32   

Hi Firstly Welcome to LLBLGen. Hope you love it.

Golden rule about web services is: 1. If your web services client is .Net and nothing but .Net then you can pass back .Net objects (after serialisation)

however

  1. If your client are non .Net, stick to 'base' object i.e. standard xml.

My guess is that you fall into catogory 2.

To ease application development you CAN use DTO on the .Net side. Please read this http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcongeneratingstronglytypeddataset.asp. I have used it in my projects as the DTO not only to non .Net clients, but also passing data between the layers. The xsd generated the CRUD code to manipulate the xml.

The generated XML then get sent to the 'other side'

The onther side will need to be 'hand coded' I am afraid.

Hope this helps.

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 25-May-2006 05:55:54   
mattcole
User
Posts: 15
Joined: 25-May-2006
# Posted on: 25-May-2006 08:13:48   

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

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 25-May-2006 10:17:18   

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 wink .

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 simple_smile 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 simple_smile

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 simple_smile

Frans Bouma | Lead developer LLBLGen Pro
npathuru
User
Posts: 17
Joined: 14-Jun-2005
# Posted on: 25-May-2006 17:04:25   

Hi Frans Would it be possible to add this as a feature in the UI of LLBLgen Pro. I notice a lot of projects i am working lately are following this approach of exchanging lightweight DTOs as messages in the form of xml. Small xml print is important. Having this as part of Code generation would be great. I have never tried the Templates stuff, so apologies if this is not relevant. Thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 25-May-2006 17:45:38   

The thing is that it is very project specific: some people want a static method in the entity classes, others want an external lib etc. So the template is pretty specific. In v2, we've added a more flexible way of adding additional tasks to what you're going to execute, so it's a matter of click and go.

To fill DTO's, we've added projection logic to v2, which means you can fetch a resultset and project that resultset onto DTO classes, which can have less fields if you want to. This means that you just have to generate empty classes, and you can fill them with the generated code, which eases teh usage of DTO's in many applications. So what's left is the template, however I think that's always the case.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 94
Joined: 26-Feb-2006
# Posted on: 25-May-2006 19:44:54   

Nice Template! could you add it to the template library?

mattcole
User
Posts: 15
Joined: 25-May-2006
# Posted on: 26-May-2006 01:42:50   

Hi Frans,

Thanks very much for that template, I'll give it a go now!

Matt

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 26-May-2006 13:03:50   

adrianporger wrote:

Nice Template! could you add it to the template library?

Perhaps for v2, where it's easy to add templates + tasks. For v1 I think I'll leave it to this posting.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 94
Joined: 26-Feb-2006
# Posted on: 26-May-2006 14:17:36   

of course for v2!

who is still working with v1???smile

Posts: 94
Joined: 26-Feb-2006
# Posted on: 26-May-2006 14:18:38   

And the GUI templates of course! ... sunglasses

joeL
User
Posts: 1
Joined: 04-Jul-2006
# Posted on: 04-Jul-2006 07:34:55   

Otis wrote:

adrianporger wrote:

Nice Template! could you add it to the template library?

Perhaps for v2, where it's easy to add templates + tasks. For v1 I think I'll leave it to this posting.

Hi I've downloaded v2 and was wondering if you could tell me the steps in getting the template and tasks working for version 2. Thanks

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 04-Jul-2006 08:09:16   

I believe those would be added soon.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 04-Jul-2006 09:04:35   

I'll put up a word doc with the changes descriptions later today, till the SDK docs are done, which will basicly contain the same info.

Frans Bouma | Lead developer LLBLGen Pro
banusi
User
Posts: 43
Joined: 08-Jul-2006
# Posted on: 08-Jul-2006 12:21:22   

So please tell, where is the word doc. Is it in the SDK download? I am new to llblgen.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 08-Jul-2006 18:43:14   

It has been merged into the SDK documentation already simple_smile So the doc wasn't needed anymore. So please download the SDK documentation

Frans Bouma | Lead developer LLBLGen Pro
KS
User
Posts: 55
Joined: 08-Aug-2006
# Posted on: 11-Sep-2006 23:40:35   

I dont know if anyone has used DTO template Frans has provided. It is really kool and easy to use. It had couple of corrections required, incase it is already updated in SVN repository then ignore this message, as I just copied it from here(can't access outside SVN coz of company policy) and modified it.

Here is the modified template Modificaiton 1 : Moved SuperType ":" in If loop, incase anyone doesn't have any SuperType/ Modification 2 : Included [Nullable] support, as it is there for LLBL Entities.

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]>
    }
}

prabhu
User
Posts: 77
Joined: 20-Dec-2006
# Posted on: 20-Apr-2007 11:42:21   

Hi,

I am not able to generate the DTO Classes based on the above template provided.

Help is very much appreciated. I am using v2

Regards

Prabhu

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 22-Apr-2007 14:48:32   

It always helps if you explain what exactly goes wrong. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
smith23
User
Posts: 3
Joined: 25-May-2007
# Posted on: 25-May-2007 16:02:34   

I'm using v2 and allready put the template into SharedTemplates folder, now my question is: what do I have to do to use it?disappointed Because when I'm trying to generate the entityclasses the interface doesn't allow me to choose the template that I createcry

am i missing something here?

P.D: I wanna create the DTO's classes in a different dll, how can i do that?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 28-May-2007 10:56:53   

You bound the template to a templateID in a templatebindings file?

Frans Bouma | Lead developer LLBLGen Pro
smith23
User
Posts: 3
Joined: 25-May-2007
# Posted on: 28-May-2007 16:02:12   

Yes i did and now i can choose from the list my template but i don't know where the DTO were generated or if i'm making something wrong.

Also i want to generate the DTO classes in a different dll, how can i do that?

jbb avatar
jbb
User
Posts: 267
Joined: 29-Nov-2005
# Posted on: 28-May-2007 16:33:28   

Hello,

if you want to create a dll by DTO, you will need to create a project by DTO. It can be very difficult to use it after(10 table=10 entity=10 project with the reference ..) Why don't you created a business class with all your DTO?

smith23
User
Posts: 3
Joined: 25-May-2007
# Posted on: 28-May-2007 16:53:03   

I think you r missunderstand me or i didn't xpress in a proper way, i wanna create 3 dll:

1-DTOs

2-Adapter Classes(DBSpecific)

3-LLBL entities

1  /  2