Generated code

Posts   
 
    
Kris Nobels avatar
Posts: 118
Joined: 02-Apr-2008
# Posted on: 22-Oct-2013 15:03:30   

I would like to use llblgen to generate other project and class files.

For example model (CustomerModel)

But how do i

  • Create custom method that remains in the class after code generation. (No partial class) (Custom method Fullname -> returns first and last name)

  • If i add attribute for 1 new property for example regex, how do i keep that attribute after code generation ?

  • When i add new field in SQL server table, it's automaticly added in my classfile.

  • When I create 1 condition in template -> result is empty line, How can i avoid that ?

thanks

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 22-Oct-2013 19:16:15   

I assume you are using LLBLGen v.4.x

•Create custom method that remains in the class after code generation. (No partial class) (Custom method Fullname -> returns first and last name)

I wonder why don't want to use partial files, but anyway, there are custom code regions in the generated class files.

•If i add attribute for 1 new property for example regex, how do i keep that attribute after code generation ?

You can define attributes in the LLBLGen Designer.

•When i add new field in SQL server table, it's automaticly added in my classfile.

There is a setting for that "Add new fields after refresh " in the Proejct Properties\Catalog Refresher, But you need to refresh the Catalog, of-course.

When I create 1 condition in template -> result is empty line, How can i avoid that ?

Could you please explain this one in more details.

Kris Nobels avatar
Posts: 118
Joined: 02-Apr-2008
# Posted on: 22-Oct-2013 22:22:27   

Here is what i would like to have / do:

Step 1 (Create model) in my template i get info from the entity to add attributes


[Display(Name = "<[EntityFieldName]>", ResourceType = typeof(Resources.Resources))]         
<[If Not IsNullable]>[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "<[EntityFieldName]>_Required")]<[EndIf]>
<[If IsStringField]>[StringLength(<[FieldMaxLength]>, ErrorMessageResourceName = "<[EntityFieldName]>_StringLength", ErrorMessageResourceType = typeof(Resources.Resources))]

i can create this

 public class CustomerModel
    {
        [Display(Name = "FirstName", ResourceType = typeof(Resources.Resources))]
        [Required(ErrorMessageResourceName = "FirstName_Required", ErrorMessageResourceType = typeof(Resources.Resources))]
        [StringLength(255, ErrorMessageResourceName = "FirstName_StringLength", ErrorMessageResourceType = typeof(Resources.Resources))]
        public string FirstName { get; set; }

        [Display(Name = "Surname", ResourceType = typeof(Resources.Resources))]
        [Required(ErrorMessageResourceName = "Surname_Required", ErrorMessageResourceType = typeof(Resources.Resources))]
        [StringLength(255, ErrorMessageResourceName = "Surname_StringLength", ErrorMessageResourceType = typeof(Resources.Resources))]
        public string Surname { get; set; }
    }

So far so good.

Step 2: (add custom content) Know i would like to add this with llblgen generation (i know that we can use of partial classes here.)

public string GetFullName()
        {
            return string.Format("{0} {1}", FirstName, Surname);
        }

Step 3: (add new field email)

        [Display(Name = "EmailAddress", ResourceType = typeof(Resources.Resources))]
        [Required(ErrorMessageResourceName = "EmailAddress_Required", ErrorMessageResourceType = typeof(Resources.Resources))]
        [StringLength(255, ErrorMessageResourceName = "EmailAddress_StringLength", ErrorMessageResourceType = typeof(Resources.Resources))]
        public string EmailAddress { get; set; }

but after generation i would like to add custom email validation attribute.

[RegularExpression(@"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$", ErrorMessageResourceName = "EmailAddress_Regex", ErrorMessageResourceType = typeof(Resources.Resources))]

But can you generate code each step with llblgen with parts that are custom added.

 public class CustomerModel
    {
        [Display(Name = "FirstName", ResourceType = typeof(Resources.Resources))]
        [Required(ErrorMessageResourceName = "FirstName_Required", ErrorMessageResourceType = typeof(Resources.Resources))]
        [StringLength(255, ErrorMessageResourceName = "FirstName_StringLength", ErrorMessageResourceType = typeof(Resources.Resources))]
        public string FirstName { get; set; }

        [Display(Name = "Surname", ResourceType = typeof(Resources.Resources))]
        [Required(ErrorMessageResourceName = "Surname_Required", ErrorMessageResourceType = typeof(Resources.Resources))]
        [StringLength(255, ErrorMessageResourceName = "Surname_StringLength", ErrorMessageResourceType = typeof(Resources.Resources))]
        public string Surname { get; set; }
        
        [Display(Name = "EmailAddress", ResourceType = typeof(Resources.Resources))]
        [Required(ErrorMessageResourceName = "EmailAddress_Required", ErrorMessageResourceType = typeof(Resources.Resources))]
        [StringLength(255, ErrorMessageResourceName = "EmailAddress_StringLength", ErrorMessageResourceType = typeof(Resources.Resources))]
        [RegularExpression(@"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$", ErrorMessageResourceName = "EmailAddress_Regex", ErrorMessageResourceType = typeof(Resources.Resources))]
        public string EmailAddress { get; set; }
        
    
    public string GetFullName()
        {
            return string.Format("{0} {1}", FirstName, Surname);
        }
}

Is this possible or are these limitations of LLBLGen.

If there is an other way to get this result please let me know . simple_smile

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Oct-2013 08:05:03   

I don't know exactly in what of your 3 steps you have problem with. It seems that you don't know how to manage the step 3.

Anyway, you can do all of that, please take a look at the attribute system, you can add them at top level (all entities, all fields, etc) or specific by each entity/field/navigator. This should solve it.

This is an example of using MetaData annotations for validation purposes, using MVC4 and partial classes: http://llblgen.com/tinyforum/Messages.aspx?ThreadID=18673&StartAtMessage=0&#104946

David Elizondo | LLBLGen Support Team
Kris Nobels avatar
Posts: 118
Joined: 02-Apr-2008
# Posted on: 24-Oct-2013 23:47:56   

Can you please give me an example please for attribute.

I can not find any good example. disappointed

(even in the current templates from LLBLgen)

Here i found some TDL statements http://www.llblgen.com/documentation/4.0/SDK/hh_start.htm

<[Foreach CustomProperty Argument Separator]> text <[NextForeach]>

Is it possible to debug your template ?

you can hit F7 and there is a debug button ...

thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-Oct-2013 07:36:37   

Did you check the zip of the example in the link I posted? http://llblgen.com/tinyforum/Messages.aspx?ThreadID=18673&StartAtMessage=0&#104946

David Elizondo | LLBLGen Support Team
Kris Nobels avatar
Posts: 118
Joined: 02-Apr-2008
# Posted on: 25-Oct-2013 19:09:43   

Yes i used your template but it does not give me any information what i need.

please create 1 template and paste this inside it.

using System;
using System.ComponentModel.DataAnnotations;

namespace <[RootNamespace]>.DTO
{       
        public  class <[CurrentEntityName]>Model
        {
            <[Foreach EntityField]>
            [Display(Name = "<[EntityFieldName]>", ResourceType = typeof(Resources.Resources))]
            <[If Not IsNullable]>[Required(ErrorMessageResourceName = "<[EntityFieldName]>_Required", ErrorMessageResourceType = typeof(Resources.Resources))]<[EndIf]>
            <[If IsStringField]>[StringLength(<[FieldMaxLength]>, ErrorMessageResourceName = "<[EntityFieldName]>_StringLength", ErrorMessageResourceType = typeof(Resources.Resources))]<[EndIf]>
            //[RegularExpression(@"XXXXXX- REGEX -XXXXXX", ErrorMessageResourceName = "<[EntityFieldName]>_Regex", ErrorMessageResourceType = typeof(Resources.Resources))]
            public <[TypeOfField]> <[EntityFieldName]> { get; set; }    

            <[NextForeach]>
        }   
}

Please look at the included file. you will see that line 11, 39, 53, 60 is empty because it's not a string.

line 24 is empty because it's not required.

What i would like to do is: Remove the empty lines because the condition is 1line. add condition to show regex or not.

is it possible or not ?

Attachments
Filename File size Added on Approval
Addressmodel.cs 3,882 25-Oct-2013 19:10.17 Approved
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-Oct-2013 06:53:46   

I think I'm kind of lost with your last post and the beginnings of this thread. Mmm, Could you please try to explain the very crucial problem again?

David Elizondo | LLBLGen Support Team
Kris Nobels avatar
Posts: 118
Joined: 02-Apr-2008
# Posted on: 26-Oct-2013 07:35:10   

OK, no problem

First of all i can now create generated files with llblgen.

But the result is not what i would like to have.

I have a template where my model is created with attributes on each property.

I use the foreach entityfield to walk to all fields.

On each of that field i add Display attribute. (for translation)

If the field "is required" add the Required attribute. [Required]

But i noticed that when it's not required it shows 1 empty line.

My generated code:


[Display(Name = "AddressLine2", ResourceType = typeof(Resources.Resources))]
            
[StringLength(60, ErrorMessageResourceName = "AddressLine2_StringLength", ErrorMessageResourceType = typeof(Resources.Resources))]
//[RegularExpression(@"XXXXXX- REGEX -XXXXXX", ErrorMessageResourceName = "AddressLine2_Regex", ErrorMessageResourceType = typeof(Resources.Resources))]
public System.String AddressLine2 { get; set; } 

How can i avoid for "the required condition" returning empty line?

My Template condition:

<[If Not IsNullable]>[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "<[EntityFieldName]>_Required")]<[EndIf]>

Let's start with this question first. :-)

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 27-Oct-2013 21:55:05   

Kris wrote:

How can i avoid for "the required condition" returning empty line?

My Template condition:

<[If Not IsNullable]>[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "<[EntityFieldName]>_Required")]<[EndIf]>

Let's start with this question first. :-)

Ok, to remove the blank line, remove the end line return, I mean, put together the previous statement or the next one, as if they were both together. Play with it, and see similar templates in the built-in LLBLGen template files.

David Elizondo | LLBLGen Support Team
Kris Nobels avatar
Posts: 118
Joined: 02-Apr-2008
# Posted on: 29-Oct-2013 23:18:50   

Ok, to remove the blank line, remove the end line return, I mean, put together the previous statement or the next one, as if they were both together. Play with it, and see similar templates in the built-in LLBLGen template files.

I found the solution.

If you write multiple conditions you have to do it like this:

[Display(Name = "<[EntityFieldName]>", ResourceType = typeof(Resources.Resources))]<[If Not IsNullable]>
            [Required(ErrorMessageResourceName = "<[EntityFieldName]>_Required", ErrorMessageResourceType = typeof(Resources.Resources))]<[EndIf]><[If IsStringField]>
            [StringLength(<[FieldMaxLength]>, ErrorMessageResourceName = "<[EntityFieldName]>_StringLength", ErrorMessageResourceType = typeof(Resources.Resources))]<[EndIf]>

After the condition enter "the character newline." example: <[EndIf]><[If IsStringField]>Newline here <[EndIf]>

Thanks for the tip btw. simple_smile

I have my next question: How do i create custom conditions: for example:

if Entity field contains Email or Phone then

end if

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 30-Oct-2013 07:46:20   

Kris wrote:

I found the solution.

If you write multiple conditions you have to do it like this:

[Display(Name = "<[EntityFieldName]>", ResourceType = typeof(Resources.Resources))]<[If Not IsNullable]>
            [Required(ErrorMessageResourceName = "<[EntityFieldName]>_Required", ErrorMessageResourceType = typeof(Resources.Resources))]<[EndIf]><[If IsStringField]>
            [StringLength(<[FieldMaxLength]>, ErrorMessageResourceName = "<[EntityFieldName]>_StringLength", ErrorMessageResourceType = typeof(Resources.Resources))]<[EndIf]>

After the condition enter "the character newline." example: <[EndIf]><[If IsStringField]>Newline here <[EndIf]>

Thanks for the tip btw. simple_smile

Good sunglasses

Kris wrote:

I have my next question: How do i create custom conditions: for example:

if Entity field contains Email or Phone then

end if

There is not TDL token for that. You can write your own if you extend the TDL system.

Other option is: use a custom Settings to mark a field like "this is email", and use that to discriminate in your template:

 <[If Not SettingValueEquals ElementType SettingName QuotedString]>
text
<[EndIf]>

Or, use lpt templates.

David Elizondo | LLBLGen Support Team