Use real and live data to populate a LPT template

Posts   
 
    
evdinursan avatar
evdinursan
User
Posts: 25
Joined: 22-Jul-2005
# Posted on: 21-Oct-2009 11:14:01   

Hi,

First of all I would like to tell you from where I'm coming and where I would like to arrive. Until now I've used the combination of LLBL for DAL generation and CodeSmith for the BL generation (for the common interaction methods for save / delete / get entity / get entity by UC / etc; ofcourse everything based on LLBL); the CodeSmith was used to populate the BL methods by connecting directly to the database and read the schema.

Due to some reasons I've decided to use LLBL for both those tasks because I had to separate the tables that I had into 2 databases and then I created some synonyms and so on... The good news is that LLBL can see those synonyms but CodeSmith can't. The good news is that I can create some custom relation into the LLBL Designer across entities from those 2 databases but at this point all the pleasure working with CodeSmith is gone because I cannot do what I'm doing into LLBL... So, I've decided to try and use the LLBL Template Studio and I can say that I'm pretty amazed about the results... Everything is running smoothly...

Now, my problem is the following: everything that I have into the LLBL Designer (entities, relations, fields, etc) can be used for interogation into the Template Studio's LPT templates, BUT in CodeSmith I had one thing that I can't find it here and maybe (if somehow is possible to do this) I would like to ask for some guideance - how can I get live data and use it into LPT template? Reason: I have a set of tables on which I'm generating some c# enums...

Thank you for your support. Evdin

PS: Technical Info

LLBL Template Studio 2.6.06102008 Final LLBLGen 2.6 Final 27 July 2009 Build.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39771
Joined: 17-Aug-2003
# Posted on: 21-Oct-2009 12:59:16   

two ways: 1) using a custom task performer (see sdk), in where you load the data from the database into the taskcache (which is an object in the generator passed into the task performer, the one for example which is used to store filenames of files generated). In the preset, you then create a task which calls this taskperformer. Place this task before the template executing task which executes the template to consume this data. In an .lpt template you then read the taskcache and consume the data.

2) in an lpt template you read the data from the db (as it's just C#) and directly consume it.

Frans Bouma | Lead developer LLBLGen Pro
evdinursan avatar
evdinursan
User
Posts: 25
Joined: 22-Jul-2005
# Posted on: 21-Oct-2009 13:07:49   

Hi,

Thank you for your message... To be honestly I've tryed before getting your answer the #2 but I've got an error that the System namespace doesn't contain Data and I thought that maybe the System.Data assembly is not referenced. Did I missed something?

Thank you.

Walaa avatar
Walaa
Support Team
Posts: 14987
Joined: 21-Aug-2005
# Posted on: 21-Oct-2009 16:35:02   

To be honestly I've tryed before getting your answer the #2 but I've got an error that the System namespace doesn't contain Data and I thought that maybe the System.Data assembly is not referenced. Did I missed something?

Please post complete error text/info.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39771
Joined: 17-Aug-2003
# Posted on: 21-Oct-2009 17:42:49   

Did you add the System.Data assembly reference in the .lpt template? (see sdk docs for details) ?

Frans Bouma | Lead developer LLBLGen Pro
evdinursan avatar
evdinursan
User
Posts: 25
Joined: 22-Jul-2005
# Posted on: 28-Oct-2009 11:25:17   

Hi,

Sorry for not replying back for long time but I did it and it's working. Thank you guys for your help.

Please find a sample of how I did it.

<$ system.data.dll $>
<[System.Data.SqlClient]>
<~
    EntityDefinition currentEntity = null;
    BLGeneratedClasses blClasses = new BLGeneratedClasses();
~>
<%
    this.currentEntity = _activeObject as EntityDefinition;
    string className = this.currentEntity.Name;
    
    Project currentProject = _executingGenerator.ProjectDefinition;
    
    if (!className.ToLower().StartsWith("solid"))
        return;
%>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;

namespace xxx.Enums
{
    /// <summary>
    /// Enum values for <%= className %>.
    /// </summary>
    public enum Enum<%= className %>
    {
        /// <summary>
        /// No value.
        /// </summary>
        None = 0,
<%
    using(SqlConnection sqlConnection = new SqlConnection(currentProject.ConnectionString))
    {
        string sqlQuery = String.Format("select * from {0}", this.currentEntity.FullTargetName);
        SqlCommand command = new SqlCommand(sqlQuery, sqlConnection);
        sqlConnection.Open();
        SqlDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
        %>
        /// <summary>
        /// <%= reader[1].ToString() %>
        /// </summary>
        <%= blClasses.CreateName(reader[1].ToString(), false)%> = <%= reader[0].ToString() %>,
        <%
        }

        // Call Close when done reading.
        reader.Close();
    }
%>
    }
}
Walaa avatar
Walaa
Support Team
Posts: 14987
Joined: 21-Aug-2005
# Posted on: 28-Oct-2009 16:39:02   

Thanks for the feedback, I'm sure this would be very helpful to others.