How can I dynamically create an IDatabase?

Any template questions or comments or suggestions

How can I dynamically create an IDatabase?

Postby donker on Fri Aug 19, 2005 1:29 pm

I want to be able to just browse for an Access mdb file, then instantiate that as a database. In CodeSmith that would look like this:

Dim dbp As SchemaExplorer.IDbSchemaProvider = CType(Activator.CreateInstance(objType), SchemaExplorer.IDbSchemaProvider)
Dim SourceDatabase As New SchemaExplorer.DatabaseSchema(dbp, \"PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source=\" & InputFile & \";\")

How would I do that in MyGeneration/MyMeta?

Peter
donker
Private First Class
 
Posts: 13
Joined: Fri Aug 19, 2005 8:23 am

Postby mike.griffin on Tue Aug 30, 2005 11:51 pm

You can dynamically open another instance of MyMeta in your template, see this C# template example:

Code: Select all
<%
public class GeneratedTemplate : DotNetScriptTemplate
{
   public GeneratedTemplate(ZeusContext context) : base(context) {}

   //---------------------------------------------------
   // Render() is where you want to write your logic   
   //---------------------------------------------------
   public override void Render()
   {
      MyMeta.dbRoot MyMeta2 = new MyMeta.dbRoot();
      MyMeta2.Connect(\"SQL\", \"Provider=SQLOLEDB.1;Password=;User ID=;yadayada\");
      MyMeta2.LanguageMappingFileName = input[\"__LanguageMappingFileName\"] as string;
      MyMeta2.DbTargetMappingFileName = input[\"__dbTargetMappingFileName\"] as string;      
      MyMeta2.UserMetaDataFileName    = input[\"____userMetaDataFileName\"] as string;
      MyMeta.DbTarget = \"SqlClient\";
      MyMeta.Language = \"C#\";
   }

}
%>


See the help on the \"ZeusInput Class\" for variables available in your input class, the example above pulls some of the data out of the intrinsic MyMeta object available to all templates and uses them to initialize another instance. Is this what you're looking for?
User avatar
mike.griffin
Site Admin
 
Posts: 3290
Joined: Sat Apr 03, 2004 6:10 am
Location: Indianapolis, IN

Postby donker on Wed Aug 31, 2005 7:40 am

Thanks Mike,
It also touched upone another question I had: how to keep the language setting. For some reason, when executing sub templates, this gets lost. That is: when I attempt to connect to a database again in a subtemplate, the language mapping is lost. Let me illustrate:

Main template:

Try
MyMeta.Connect(Helper.ConnectionType, Helper.ConnectionString)
MyMeta.Language = \"VB.NET\"
MyMeta.DbTarget = \"SqlClient\"
Catch ex As Exception
Exit Sub
End Try

context.ExecuteTemplate(\"Project.zeus\")
Output.Clear
context.ExecuteTemplate(\"ModuleSettings.zeus\")
Output.Clear

ete etc

Template 'Project.zeus':

Try
MyMeta.Connect(Helper.ConnectionType, Helper.ConnectionString)
Catch ex As Exception
Exit Sub
End Try

(the Helper object is just there to help me keep track of some settings). Now in the 'Project.zeus' template the column.LanguageType will return 'unknown' until I comment out the connect in this template (after all, in the context we are already connected). My motivation to have the second connect is so that I can use the subtemplates independently.

Is this a bug or a feature?

Peter
donker
Private First Class
 
Posts: 13
Joined: Fri Aug 19, 2005 8:23 am

Postby mike.griffin on Wed Aug 31, 2005 1:39 pm

Ahh, no, well first there is no reason to create a MyMeta object unless you want to have two connections to different databases? The MyMeta object with all of the default settings is availble to all tempates. And if you manually create a MyMeta it doesn't use the default settings, it is the MyGeneration.exe app that feeds the xml definitions to MyMeta. that is why my example showed you how to pull the settings from the intrinsic MyMeta object, you can do that for language and dbtarget, are you sure you need to be creating a 2nd MyMeta? I'm confused here, you really don't need to do any of the stuff you are doing with MyMeta for sub-template execution ....
User avatar
mike.griffin
Site Admin
 
Posts: 3290
Joined: Sat Apr 03, 2004 6:10 am
Location: Indianapolis, IN

Postby donker on Wed Aug 31, 2005 2:01 pm

Mike,

It is confusing, yes. My goal is the following: I have a number of loose templates to build all kind of stuff for a DNN module. For instance I have a EditPane template that will create Edit ascx, vb, and resx files. This is done on the basis of a single input file with all necessary parameters (in xml of course, but that is beside the point here).
To group these templates together I have a 'main entry point' template that takes as input this same settings file and calls each sub template. So far so good. However, each template would, upon starting to render, link to the database in question (the connectionstring is in the settings file) and start to do its stuff. What I ran into is that when a sub template does this, it effectively loses the language setting from the parent template.
Now, during normal operation, we can just take out the child's code that creates the connection because we are already connected due to the parent template. However, having taken this out means the child template will not run independently anymore (without the parent).
I have a work-around though. I just add a variable to the general context and check for its existence in the child template. If so then skip database connection. If not there, then begin connection itself.

Peter
donker
Private First Class
 
Posts: 13
Joined: Fri Aug 19, 2005 8:23 am

Postby mike.griffin on Wed Aug 31, 2005 2:15 pm

donker wrote: (the connectionstring is in the settings file)


The MyGeneration default settings file? If so then don't bother creating a MyMeta object all tempates (including sub-templates have access to the same exact MyMeta object). If you have found a big then we certianly will fix it in this months maintenance release. I will verify this.

Also, there is something called a template group which might just be your ticket. You can create a template with nothing but a UI to gather all of the user input to run a group of tempates. Try this (on a sample template), toggle in the Template Settingss (where you can change the Code/UI language settings) and underneath the Namespace is the Type you can change it to group and then a box will appear, you can add a bunch of templates to it and they will all run using the Zues.input[] variables you set.

Does that make sense?
User avatar
mike.griffin
Site Admin
 
Posts: 3290
Joined: Sat Apr 03, 2004 6:10 am
Location: Indianapolis, IN

Postby donker on Wed Aug 31, 2005 2:29 pm

Could be. The settings file is not the MyGeneration settings file but my own. It is transportable/copyable/modifiable to produce DNN modules.
donker
Private First Class
 
Posts: 13
Joined: Fri Aug 19, 2005 8:23 am

Postby justin.greenwood on Wed Aug 31, 2005 2:42 pm

Sub-Template Execution:
Let me go through how the subtemplate execution works real quick. There are two ways to execute a subtemplate. First, using the depricated method \"void ExecuteTemplate(string path)\". This will pass the current ZuesContext object as it is (with the MyMeta, Input, and Output objects attached) into the subTemplate as if it was just pushing that template code in the spot of the ExecuteTemplate command. The second way to execute a subtemplate is with the \"Execute(string path, bool copyContext)\". If you set the copyContext parameter to false, it behaves just as the depricated ExecuteTemplate method does. If you set the copyContext parameter to true, a new ZeusContext object is created and the objects and data are copied into it. So basically, when you write to the output object in the subtemplate, it's writing to it's own context object. This saves you from having to call output.clear().

Template Groups
I use template groups more than anything in my real job. A template group is just a type of template that has an input form for collecting data into the context, and then a list of temlplates to execute. Each template can run on it's own, but when the template group is executed, you only have to collect data once. There is a Sample TemplateGroup that comes with MyGeneration at \"Samples.Other.Template Group Example\".

I think using Template Groups is a better solution to your problem that subtemplates. Either way, you should be able to get everything working without a hitch. If you're losing the MyMeta Language property, it may have to do with you reconnecting MyMeta every time you start the template. Try setting it again from the input variable that is always in the context.

Here are the variables that exist in the Context (populated from the default settings):
  • __version The MyGeneration Application Version.
  • __defaultTemplatePath The location of templates that should be displayed in the template browser.
  • __defaultOutputPath The default output path.
  • __dbDriver The default database driver.
  • __dbConnectionString The default database connection string.
  • __domainOverride True or false based on the Domain Override setting in the default Application Settings.
  • __dbTarget The selected DB Target
  • __dbTargetMappingFileName The DB Targets file path.
  • __dbLanguageMappingFileName The Language Mapping file path.
  • __language The selected language.
  • __userMetaDataFileName The User Meta-Data file path.
User avatar
justin.greenwood
Site Admin
 
Posts: 441
Joined: Sat Apr 03, 2004 6:10 am
Location: Indianapolis, IN

Postby donker on Tue Sep 27, 2005 12:52 pm

The Template Group does look like a good idea, but in your enthusiasm to bring power, I fear the price has been control (as always). As a template programmer I would like to use templates as objects. They get instantiated, then fed with certain input and return certain output. So for the purpose of our example we have a number of DNN module generation templates that have, for instance, an output directory. Now I don't want all results to go to the same directory. However, using the group I have no more control over processing. So I can't set the output dir differently for each template. I can see the reason you guys thought about having a 'context' which holds a pool of input variables, but that doesn't work very well in my OO world.
By the way, I'm still not having luck with the output language setting.
Peter
donker
Private First Class
 
Posts: 13
Joined: Fri Aug 19, 2005 8:23 am

Postby justin.greenwood on Tue Sep 27, 2005 2:40 pm

With a template group you can have each template save to a different directory. In fact, I think all of my templates in groups do that. I collect the input, and if I need multiple output directories, I can put them in different input variables. At the end of each template, you call the \"save()\" function in which you can specify any path you want. It's really as flexible as you want it to be. MyGeneration provides a set of variables in the context, but you can add as many extra variables in there as you want.
As far as the MyMeta.Language setting is concerned, this is what I suggest. First, make sure you have the very latest version of MyGeneration. Second, check to make sure the language is set in Default Settings. And third, make sure you arn't setting the MyMeta.Language to an empty string manually in your code. We have made all this work without any hiccups. If you have template you can zip and send to me, I'm sure I can find the problem quickly, or possibly discover and fix a bug if one exists.
User avatar
justin.greenwood
Site Admin
 
Posts: 441
Joined: Sat Apr 03, 2004 6:10 am
Location: Indianapolis, IN


Return to General Template Questions/Comments

Who is online

Users browsing this forum: No registered users and 1 guest