views:

100

answers:

2

Hello there, i'm having a issue with the T4 templates for generating code.

I'm wondering if anyone can help me with an issue I have. I want to store the connection string for use with SubSonic 3.0 in a default location such as the root of the website (Web.Config or a seperate .config file).

This is fine providing the t4 Templates are "run" within the project that has the config file.

What I want to do is seperate them out from the initial project into seperate class/project files and run the templates from there.

This all works fine providing I place a App.Config file in those projects also with the connection string but this is not what I want as this will then hardcode those connection strings within those class files. (Note the other class file projects are in a seperate location altogether)

Best way I can describe the setup (Web Application) is as follows:

  • DB Class (Project) | ---- Models > T4 Templates (NOTE1) |
  • Core Class (Project) |
  • CMS class (Project) |
  • Website (Project) | ---- Web.Config <<< ConnectionString

NOTE1: The T4 Templates in that directory need to read from any centeral file in the Website (Project) root folder. Since the database server could change this needs to happen.

The DB Class (Project) is in a different location altogether from the Website (Project).

This has been accomplished in dashCommerce but using SubSonic 2.0 (which was most likely the command line compiler that did it, i'm not too sure)

Just incase anyone is wondering why I am doing this like this. I am compiling a set of base DLLs to be used in many projects.

The DB Class will be unique to each project and will be compiled based on that project's database using the T4 teplates in that DLL. The other DLLs will reference the DB class DLL and these will be common among projects.

Basically, the DB class is literally the gateway to my database for any other DLLs I write and hence why the DB class needs to reference the connection string in the root project.

(I realise I can just create and compile the DB DLL to be used BUT in development/testing the project will hang on the other DLLS as they too need to reference the connection string IF I include a reference to SubSonic which I need to to access the table objects etc from those DLLs UNLESS I create all the bridge code in the DB class which will just be crazy and pointless for this exercise)

EDIT:

I admit it is hard to explain. I'll base on a simpler example. I have two locations: A.- D:\Web Application\Core.DLL AND B.- D:\Web Application\Website I create a new C#.net "Web Application" solution in the "Web Application" folder and add the two projects above to it. (Note that it is one solution containing two seperate projects in different directories, which could be anywhere) I need the connection string data to be stored in the "Website" project and have the Core.DLL project reference it from there.

The Core.Dll will store the ttfiles and corresponding code generated for use with SubSonic 3.0. The tt files will be "run" from within Visual Studio within Core.Dll project. The two problems are:

  • A.- The tt files can't reference the web.config file (or can they?) AND
  • B.- The SubSonic code still needs to be able to read a connection string at run time. Can I have App.config read a web.config connectionstring section? Or how would I do this.
A: 

OK I'm not sure I'm 100% on what you're asking here but I'll do my best

  • A.- The tt files can't reference the web.config file (or can they?) AND

No, but they really don't need to. The App.config for D:\Web Application\Core.DLL can contain the connection string. If you're worried about having the connection string duplicated in both web.config and app.config then you could add a custom build event that copies the connection string from one to the other.

  • B.- The SubSonic code still needs to be able to read a connection string at run time. Can I have App.config read a web.config connectionstring section? Or how would I do this.

At runtime the app.config won't be referenced by your website the web.config will.

Adam
A: 

Thanks for getting back to me Adam. I just figured it out as you posted and thought I'd let people know my result.

It wasn't until I scoured the net about T4 Templates that my mind jumped into action. I was mistaking how the T4 Templates work. I thought the tt files had to stay with the generated code files.

What I did was create a second project just soley for running the T4 Templates which is seperate from the main project. I put in the connection string that I am currently using and likewise will only use for development, not production, so I can easily modify it. This worked perfectly and allowed me to copy the generated code files to the DB DLL class project for use.

What I was unaware, like much what you said above, was when the project ran in it's entirety it made available the Web.config file to any DLLs etc associated with the Website. So this solved my problem with the connection string also and being to modify it easily in production.

Silly but it's just one of those things, if you don't know and no one tells you, you assume it can't be done.

I can now access my database objects anywhere within the project (including any DLLs which reference the DB DLL class) using SubSonic.

I have to say, SubSonic is definitley going to make my life easier. A single line of code to get data, now that's good thinking :)

Anthony