Viewing a foreign key in a Janus Gridex control

Posts   
 
    
hplloyd
User
Posts: 191
Joined: 29-Oct-2004
# Posted on: 10-Feb-2005 10:25:07   

Hi

I am using the gridex control to view data - as a matter of principle I dont let users update the grid directly, I only let users select rows in the grid and use add, modify and delete dialogs to control changes to the grid. This way I have more control over the validation etc as well as ensuring that the database is kept up to date with changes as they happen.

Up until now I have been using TypedLists to generate datatables that I use to bind to grids. However, with all the great stuff that LLBL does with collections, I want to move away from using datatables as thge source in favour of collections.

But, I have come accross an issue I need help with.

Supose we have two tables Customers and Countries where each customer has only one Country assigned so

Customers

CustomerID CustomerName CountryID

Countries

CountryID CountryName

I can bind a collection of customers to a Gridex control but obvoiusly it displays the CountryID instead of the Country name..

How can I display 3 text columns Customer ID, CustomerName and CountryName in my grid control?

Many thanks in advance

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 10-Feb-2005 12:25:43   

For now you have to add a property to the Customer entity which returns the value of the related country entity. (this will be settable in the designer soon).

This can be a bit of a pain, maintenance wise. So you can setup an include template to do this for you, every time you re-generate the project.

Open template studio and create a new template set based on th template set you're using, for example the CS template set. (see the tutorials how to do this)

Add a binding to Custom_EntityAdapterTemplate and bind it to a new file: entityInclude.template (I determined from another posting you're using adapter simple_smile )

Open entityInclude.template in the editor and add the following code:


<[If StringValueEquals StringValueName "Customer"]>
public string CountryName
{
    get
    {
        if(_country==null)
        {
            return string.Empty;
        }
        else
        {
            return _country.CountryName;
        }
    }
}
<[EndIf]>

Save the template and template set file, open llblgen pro and your project, select this new template set, generate code and your customer entity should have the property.

Frans Bouma | Lead developer LLBLGen Pro
hplloyd
User
Posts: 191
Joined: 29-Oct-2004
# Posted on: 10-Feb-2005 14:56:19   

I find that I have this issue a great deal for a whole host of tables in my ever expanding database.

I have messed around with template studio a bit and have a whole load of files and projects being generated in both lpt and TDL, but I have not come across include templates... I will look into this.

I guess my approach will be to have a large include file that does the same thing for all my tables (for some tables there may be many foreign keys). However initially you thew me a bit there because I assume


<[If StringValueEquals StringValueName "Customer"]>

Should be


<[If StringValueEquals CurrentEntityName "Customer"]>

If that is the case then I understand that the following property will be added to the customer entity code and only the customer entity code


public string CountryName
{
    get
    {
        if(_country==null)
        {
            return string.Empty;
        }
        else
        {
            return _country.CountryName;
        }
    }
}

BUT it is this code that confuses me, what is _country? Obvoiusly it is the related CountryEntity for the customer but when is the _country fetched from the database? are there other _entity objects for all the other foreign keys for a table - where in the generated template are they created?

Finally, I assuyme therefore that if i had an employee entity that had a foreign key to a department entity then I could also add the following block to the same include template:


<[If StringValueEquals StringValueName "Employee"]>
public string DepartmentName
{
    get
    {
        if(_department==null)
        {
            return string.Empty;
        }
        else
        {
            return _department.DepartmentName;
        }
    }
}
<[EndIf]>

....and so on and so on for each of my foreign keys for each of my tables?

If so then LLBL is even more brilliant than I thought!!

When this is available in the designer then I assume that I will not need these include templates and all the relevant properties will be generated form me - is that correct?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 10-Feb-2005 16:01:57   

hplloyd wrote:

I find that I have this issue a great deal for a whole host of tables in my ever expanding database.

I have messed around with template studio a bit and have a whole load of files and projects being generated in both lpt and TDL, but I have not come across include templates... I will look into this.

It's easier than you might think up front. If you have a lot of statements to include for a lot of entities, you can make per-entity include files using your own templateid's, so you do this in your file you bind to Custom_EntityAdapterTemplate:


<[If StringValueEquals CurrentEntityName "Customer"]>
<# Customer_Include#>
<[EndIf[>

<[If StringValueEquals CurrentEntityName "Order"]>
<# Order_Include#>
<[EndIf[>

etc...

and in your template set definition, you'll bind Customer_Include to the template in which you'll specify all code which has to be included into the customer entity.

I guess my approach will be to have a large include file that does the same thing for all my tables (for some tables there may be many foreign keys).

If you have code which is the same for all entities, you don't have to surround it with that if statement of course, but just add it to the include template. Also, the text in the template is merged with the template at generation time, so you can use TDL statements in the text in the include template simple_smile

However initially you thew me a bit there because I assume


<[If StringValueEquals StringValueName "Customer"]>

Should be


<[If StringValueEquals CurrentEntityName "Customer"]>

Yes, my bad, I copied the TDL statement from the SDK docs, forgot to change that name. .

If that is the case then I understand that the following property will be added to the customer entity code and only the customer entity code


public string CountryName
{
    get
    {
        if(_country==null)
        {
            return string.Empty;
        }
        else
        {
            return _country.CountryName;
        }
    }
}

BUT it is this code that confuses me, what is _country? Obvoiusly it is the related CountryEntity for the customer but when is the _country fetched from the database? are there other _entity objects for all the other foreign keys for a table - where in the generated template are they created?

_country is the private class member which is reserved to hold the related entity 'Country', and it's the private member variable which is used by the CustomerEntity.Country property simple_smile . Every field mapped onto a relation will end up as fieldName (camel cased with a '' prefix).

You have to fetch this data with a prefetch path when you're fetching customers.

Finally, I assuyme therefore that if i had an employee entity that had a foreign key to a department entity then I could also add the following block to the same include template:


<[If StringValueEquals StringValueName "Employee"]>
public string DepartmentName
{
    get
    {
        if(_department==null)
        {
            return string.Empty;
        }
        else
        {
            return _department.DepartmentName;
        }
    }
}
<[EndIf]>

....and so on and so on for each of my foreign keys for each of my tables?

yes. simple_smile

If so then LLBL is even more brilliant than I thought!!

smile

When this is available in the designer then I assume that I will not need these include templates and all the relevant properties will be generated form me - is that correct?

True. I hope to cram in this feature so you can add a 'related field' field to an entity which returns the value of a 1:1 / m:1 related entity. (also with expressions).

Also, the code generator engines will be changed so that they can handle user regions in the code which are preserved between code generation cycles. This then allows you to add code to the generated code without worrying the code is overwritten.

Frans Bouma | Lead developer LLBLGen Pro
hplloyd
User
Posts: 191
Joined: 29-Oct-2004
# Posted on: 10-Feb-2005 16:11:19   

Well what can I say - FANTASTIC!!

When do you expect to have the designer stuff ready - weeks or months?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 10-Feb-2005 16:39:56   

I hope a beta is done by the end of february. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
hplloyd
User
Posts: 191
Joined: 29-Oct-2004
# Posted on: 10-Feb-2005 16:47:59   

I think I will be able to contain my excitement till then!! frowning