|
-
Aug 1st, 2011, 05:19 PM
#1
Reusing a very specific project for multiple solutions
Hi,
First of all, I wasn't sure whether this was VB.NET or General Developer content... It's not technically about VB.NET code, but it's not very general either since it is specifically about Visual Studio. Feel free to move it if required.
A while back I realized that all of my database connection code was getting very biolerplate. I had 'developed' my own method of connecting to an Access database and storing the results. Basically, for each table I create one 'Manager' class and one 'Entity' class. The Entity class represents a single record in the database and has properties for each field. The Manager class has methods to save and load instances of this Entity class.
Writing this code became tedious quickly though, and when I eventually found out about T4 Text Templating in Visual Studio, I decided to go with that. T4 is basically a very simple way to generate source files automatically, on the fly during design-time. You can view the form designer as a special case of the T4 files; you edit something visually (drag controls on a form) and the source code for that is generated behind the scenes (in the Form.Designer.vb file).
T4 works very much the same, except in my case there is no 'designer' but rather a simple XML file that I write. The XML file contains an enumeration of the tables and their fields (and types), as well as relations between tables. The T4 file reads this XML file and generates all the Manager and Entity classes for me.
All I have to do now is write the XML file (this takes seconds) and hit 'generate', and I'm done.
Another important detail is the fact that all generated classes are partial classes (just like the Designer classes for the form designer). This allows me to extend the generated classes with more methods and properties. I cannot put that code in the generated class file because it will be overwritten the next time I change the XML file.
Suppose I generate a file PersonEntity (from a People table) which has a Birthdate property (database field). You can imagine that having an Age property on the PersonEntity is useful, so I can do that by extending the Person class: I add another PersonEntity file and declare the class Partial, then add my method:
Code:
Public Partial Class PersonEntity
Public ReadOnly Property Age As Integer
Get
Return DateTime.Now.Years - Me.Birthdate.Years
End Get
End Property
End Class
This, as well as the fact that I generate my source code in the designer, now poses a major problem however:
I cannot create a class library (DLL) for this project!
A DLL contains 'fixed' classes that don't change; my project contains T4 files that generate source code on the fly. For every project that uses a database, it will contain different classes.
Furthermore, the extending Partial classes MUST be in the same project (a requirement for partial classes, unfortunately), so that's another reason I cannot compile it into a DLL and just reference it (there would be no way to add new (partial) classes to it).
Why is this a problem? Well... I cannot easily reuse this project in different solutions.
If I want to use this project for a solution with a database about my pets, and also for a solution about my work hours, I have to make a copy of the entire project and 'carry it around' to the new solution.
The most obvious thing one would do in such a scenario, usually, is just keep a single database generating project and use 'Add Existing Project' to the two solutions. The two solutions would then use the same project, and when I make a change from one solution, it also applies to the other.
This is usually desired (I am still making changes to the generating code constantly, it's not finished at all and probably never will be), but in my case I do not want this at all. In one solution the XML file defines completely different tables than in the other solution, and completely different classes will be generated. Furthermore, extending partial classes in one solution won't need to be in the other solution.
So, as far as I can see I must make a copy of the database generating project every single time I start a new solution. This now poses a new problem, namely the fact that I'm still changing it. Each time I discover an error or mistake or an improvement to make, I apply a fix to the project in the solution I happen to be working on at that time. However, the (possibly) 38 other copies of this project won't get that fix, and over time I will (and have...) end up with 38 different copies of the database generating project, some very old, some up to date, some containing fixes that others do not, etc... It's turning into a real mess!
Does anyone know what I can do about this?
One possible solution, that still has the last problem kind of, is to create a new Project Template for it. I already tried that, I can add the XML file, the T4 generating files and all, and when I create a new 'Database Generator Project' (which is what I named it), I get a fresh copy of my database generating code project.
All I need to do now is 'compile' any changes I make back into the Project Template, so that the Project Template always has the most up to date version. But that is basically the same problem as I had before, and it's not really feasible to keep updating the Project Template because I have to 'clean' the project first then (remove the extending partial classes, clean out the XML file, etc) and then my code is lost...
So I'm still really looking for a solution to this problem.
I know, very long post, but I hope some of you at least get the gist of what I'm trying to say... Thanks!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|