Wednesday, January 05, 2005

Code Generation out of Visio...Sort of

Okay, so I have been planning to write this post for awhile now, and for some reason, I just haven't been able to sit down and do it. That being said, I have to warn you from the start, this is going to be fairly technical. With that being said, I've always liked the idea of code generations. It has always surprised me that code generators where not more integrated in to tools like Visio in the past. I know that next version if Visual Studio is going to have a strong tie into Visio, and that the current version if VS can reverse engineer your project in to Visio UML, but that just wasn't quite what I wanted. You see our current process is to use UML to in Visio to define the class structure of our projects. This is really nice because it helps you to think through your code before you start writing anything. You think through the data relationships, and how different parts of the project will interact with each other. Using this method, you create a model of the code, complete with classes, properties, variables and methods. The are all named and have their elationships created. Now here is where the disconnect was for me, after that is done, you have to write all the code that does that. So you have to retype every class name, property name, variable name and method name. Well, like any developer worth their salt, I am lazy in a good way. (In the way that makes you figure out how to not do repeatitive tasks over and over again, not the way where you sit on the sofa and order your children to do your choirs and drink too much beer kind of way.)

 So in looking for a solution to my dilema, I turned to google!. After some searching, I found nothing that worked like I imagined it should (darn imagination always getting in the way). But I did find somethings that worked in their own way, and I just kind of merged them. One of the first articles I came across with how to export UML to XMI, (Note that is XMI, not XML). While this was on the way to what I wanted it just handled a small part of it. So the article I rain into was on the Microsoft site. The article UML to XMI Export Functionality did get me on my way. I do have to warn you though, this one isn't for the faint of heart. You have to complie C++ code, and Add a Add-on in to Visio. Basically, if you don't think you are going to use this, don't download it.

Now I am thinking great, I have my UML exported to XMI, now I just need to convert the XMI into XML. As it turns out, converting XMI, in to XML isn't that bad. I found an article about converting XMI, in to HTML, and I figured, well if they can convert it to HTML, I can convert it to XML. Well the article was so helpful, I basically just rewrote the XSL, that they used to create the HTML, to create XML. One thing to note, the uml.dtd file that they supply in the download, is so valuable. You can't create the xml file without it! The article was very well written and laid out, I was able to create my xsl transform on the train ride in.

So now I have XML from my UML, and it only took two terrifying steps. (if you don't count, the compiling of code and installing stuff in the visio directory that really could of goofed it up, if I had done it wrong and make me have to unistall and reinstall Visio.) So now, I just need something to write the code right. Well that will be handled by Eric J. Smith's great product "CodeSmith". Honestly, if you develop applications in .NET, CodeSmith is a must have. Now with CodeSmith and this great article from The Code Project on Business Objects for CodeSmith. The nice thing about this article, is that the code examples are wonderful, and there for you to extend. Which we have, we can now pick between generating code for C# and VB. Of course there is so much that can be done with this framework. Now with everthing together we are able to generate code from Visio... Sort Of.

No comments: