Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Guest
#1 Posted : Tuesday, September 7, 2010 10:49:47 PM(UTC)
Guest
Rank: Guest

Groups: Guest
Joined: 3/17/2009(UTC)
Posts: 1,084

Hi,
I'm trying to get a proof of concept working. I'm at my wit's end and ready to give up. Please help!

I've been following a tutorial located here http://www.olegsych.com/...complex-code-generators/

I have a generic Window Application (C# 3.5)
In it I have created a class

file: Database.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace MyNamespace
{
public partial class Database
{
................
// purpose and contents of class are not important


I've added a tt file (Add new item, generator) called GenerateASPX.tt (from the tutorial this file would be called CrudProcedureGenerator.tt)

file: GenerateASPX.tt
<#@ include file="DefineControl.tt" #>
<#+
public class GenerateASPX : Generator
{
public Database db; // <-- Problem
public DefineControl defineControl = new DefineControl();

protected override void RunCore()
{
.....


This file includes DefineControl.tt
<#+
// <copyright file="DefineControl.tt" company="XYZ">
// Copyright © XYZ Incorporated. All Rights Reserved.
// </copyright>
public class DefineControl :Template
{
public string DisplayAs;
public override string TransformText()
{
.............
return this.GenerationEnvironment.ToString();
}
}
#>


The last file is GeneratorInit.tt (from the tutorial this would be called NorthwindProcedures.tt)

<#@ template language="C#v3.5" hostspecific="True" debug="True" #>
<#@ output extension="txt" #>
<#@ include file="T4Toolbox.tt" #>
<#@ include file="GenerateASPX.tt" #>
<#
GenerateASPX generator = new GenerateASPX();
generator.defineControl = DBObject.GetDatabaseObject();
generator.Run();
#>


DBObject is a public static class that I seem to be able to interact with just fine.


The problem is that I cannot use the Database class on the highlighted line.
If I right click the GeneratorInit and select "run custom tool" I get the following error:

Error 1
Compiling transformation: The type or namespace name 'Database' could not be found (are you missing a using directive or an assembly reference?) c:\.........\GenerateASPX.tt

This error brings me a line number to "public Database db;"


No matter of adding anything like

<#@ assembly name="Database" #>
<#@ import namespace="Database" #>
<#@ assembly name="MyNamespace.Database" #>
<#@ import namespace="MyNamespace.Database" #>
<#@ include file="Database.cs" #>

to any of the .tt files resolves this problem.

Edited by user Monday, April 4, 2011 1:10:41 PM(UTC)  | Reason: Not specified

Guest
#2 Posted : Tuesday, September 7, 2010 10:53:48 PM(UTC)
Guest
Rank: Guest

Groups: Guest
Joined: 3/17/2009(UTC)
Posts: 1,084

In the Form.cs file (or any other code file in the project) I can easily :

Database x = DBObject.GetDatabaseObject();


w/o includes or references
tangible
#3 Posted : Wednesday, September 8, 2010 10:58:00 AM(UTC)
Guest
Rank: Guest

Groups: Guest
Joined: 3/17/2009(UTC)
Posts: 1,084

Hello Andrew,

I think the primary issue is that the Database Class you declared is not in an assembly DLL (Class Library) that you referenced via <#@assembly #>, nor is it in the T4 File using the <#+ #> synatax.

Let me explain what happens and how to use Classes in T4 or DLLs:

The T4 Language and runtime is designed by Microsoft and is part of Visual Studio we just provide the Editing experience.

You can imagine that T4 does actually creates a new “project” behind the scences for each T4 File. These virtual projects have their own
References to assemblies that are declared via <#@ assembly #> directive Also the <#include#> directive is needed in every file to know what other files to parse first.

Let me first explain this in more detail and then answer your questions inline below.

Scenario 1: You are trying to use a Class defined in a .NET DLL. This might mean that you have to move your class into a Class Library project first

In VS2010:

If you intent to call a self-written DLL from a T4 Code Generator you need to use the <#@ assembly name=”$(ProjectDir)\MyDLL.dll”#> Syntax
and place the DLL in the Project’s directory.

In VS2008

There was the possibility to have the T4 Engine resolve the location of an assaembly that is referenced by <#@ assembly name=”abc.dll”#> syntax
using the References on the project that contains this T4 Template. However we did not support that for intelli-sense as it was clear already that this
will not work in VS2010 and we did not want to encourage people to write templates that do not work in VS2010. In VS2008 so you need to provide the
FULL Path to the DLL.

Hint: Both in VS2010 and VS2008 the DLL that is referenced will be locked by VS when the Template is executed. IT will be unlocked when you open/close the solution.
To Work around this you can use the <#@ volatileassembly #> syntax provided by the T4 Toolbox.

Scenario 2: You just need to define a class as a helper in the T4 Template

Let’s assume you want to create an in-memory model of something you want to generate first. Then you need to use the <#+ syntax at the end of the file to add a class decleration there.
Note that the classes are actually inner classes of the outer class GeneratedTemplate that you Template is transformed to. If you need to define Extension Methods innerclasses are not ok,
So you need to use Scenario 1 to do this.

<#
Database myDatabase = new Database(string Name);
myDatabase.Tables.Add(new Column(“Hello”));

Generator.Run (myDatabase);
#>
<#+
class Database{
Database (String Name){
_Name=Name;
}
string _Name;
}

class Table
Table(String Name){
_Name=Name;
}
string _Name;
}

class Generator{
public Run(Database aDatabase){
#>
// Generate DDL for <#= aDatabase#>
<#+
foreach (Table c in aDatabase){
#>
CREATE Table <#= c.Name#> WITH ID…
<#+
};
}
}
#>


Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF | YAF © 2003-2020, Yet Another Forum.NET
This page was generated in 0.194 seconds.

Authorization on the site

For an authorized user opens up new possibilities

Login

Password