Exporting Visual Studio Solutions with SolutionFactory

来源:百度文库 编辑:神马文学网 时间:2024/04/27 22:04:14

b#

being agile in a traditional world
a blog by Ben Scheirman
  • Blog
  • About Me
  • Photos
  • Book
  • iPhone App
  • Contact

Exporting Visual Studio Solutions with SolutionFactory

Part of my job is to create reusable code, patterns, designs, solutions, etc for other developers to leverage.  Getting started using some of these things can be initially tough for someone who hasn't had a lot of experience with long-term, maintainable solution structures for projects that use:

  • an automated build
  • 3rd party libraries
  • machine-specific configuration
  • D.R.Y. for things like connection strings, etc.

Because of these things, I favor a folder structure similar to this one:

This keeps things tidy.  There is a home for everything.  Third party libraries get checked in to source control, as well as any light-weight developer tools that we use during the build.  Everything you need to build the software on a developer's machine should be here.  (Of course I assume that you already have the appropriate Visual Studio & database server on your local machine.)

In addition, many of our projects look something like this:

  • App.UI
  • App.Core
  • App.Persistence
  • App.Tests

Granted, this is very short-sighted and won't be the end-all-be-all solution structure for all projects.  But the goal here is to get you (a developer) started really quickly.  These projects can already contains some things that get from nothing to something tangible in short time.

My sample solution contains:

  • An MVC project with a standard look & feel
    • images
    • javascript files
  • A sample CRUD screen, showing a small example of the various players in displaying & updating data.
    • NHibernate
    • FluentNHibernate
    • NHibernate.Linq
    • an IOC container
    • an MVC Controller factory
  • A test project that shows both unit and integration style tests.  Some nice fluent testing extensions are left in to show how it works. (the target audience will probably only have a C# 2.0 background).

All of this is pre-wired up.  Dependencies are referenced in each project.  The libraries are located in a relative URL, meaning other people who check out this code can build immediately.  In addition, there is a sample NAnt script that shows how to build & test the software.

It's about convention over configuration.  For some applications, if they don't want to do it this way, fine.  It's not about enforcement, it's about gentle guidance & encouragement.

Packaging it all up

So I went on the path of trying to export this into a set of Visual Studio project templates.  I found the project templates to be less-than helpful for a few reasons.  First, it assumes that you keep your projects in the root of the project folder structure.  Second, there seemed to be no way to elegantly package up assemblies that the project depended on.  I looked at S#arp Architecture for some help, but quickly realized that there isn't an easy road to doing all of this.  In addition, as far as I could tell there is no way to automatically create solution templates.  You have to create project templates for each project and then manually stitch them together.

This violated my #1 rule with the templates.  In order for them to be successful, they must be really easy to change.  I figure that in 2 months time, something in the template will have changed, or someone will want to tweak the design, or upgrade to a new version of a dependant assembly.  If they have to go through all of those hoops to publish a new template, they just won't do it.  All of the effort on the initial template will be wasted.

Then I came across SolutionFactory, which is a project started by Eric Hexter up on Codeplex.  It consists of 2 major components:

  • A template wizard that handles all of the post-launch tasks, such as moving the projects to the right location, fixing up references, etc.
  • A Visual Studio add-in that creates a large solution template, containing multiple vstemplate files (project templates) and also respects my folder structure.  Nice!

Usage is pretty dead simple.  After installing, you get a new add-in.  You have to enable the add-in before using it.

 

Once you place the checkmark next to the Solution Factory add-in and click OK, you should see a new menu item in Visual Studio.

When you select "Export Template" from the Solution Factory menu, you are presented with this dialog:

This controls your exported VSI file.  One cool thing to note is that you can have a custom command run as part of the template invocation process.  This is especially helpful if you have a firstTimeConfig.bat file or similar.  Sometimes you just have to run the build once to make sure everything is in place.

During the export, any remnants of TFS source control will be stripped out in the target folder.  Any read-only files will be unlocked, any *.*scc files will be deleted, and *.csproj files will be modified to remove the source control bindings.  If this isn't done, you'll be prompted to remove source control bindings for each project every time you create a new project based on the template.  Yuck!

When the export is finished (it takes about a minute) you should see a message box like this:

Now if we look in our c:\temp folder, we should see some new files:

The VSI is a Visual Studio Installer file, and it simplifies putting the template zip file into Visual Studio.  All you really have to do is copy it to the appropriate Visual Studio 2008\Templates folder and run devenv.exe /installvstemplates to get it to rebuild the template cache.  When you run the VSI, you'll get a simple installer.  It will warn you that the template is not signed, just press OK.

There is one caveat though:  Our template relies on SolutionFactory.Wizard.dll to run some custom actions when the template is invoked.  The VSI file doesn't have permissions to copy files into C:\Program Files, so until a custom installer (MSI) is made, you have to copy this dll manually.

To do this, close down all instances of Visual Studio and copy SolutionFactory.Wizard.dll into c:\program files\Microsoft Visual Studio 9.0\Common 7\IDE\Private Assemblies.  (on Vista/7 this path may be different, so adjust accordingly).  This only has to be done once, and subsequent uses of the template will use this dll.

Now, fire up a fresh copy of Visual Studio, choose File->New Project, and see our new template!

When you click OK, Visual Studio will do a little dance, make sure you wait for it to finish!  When it's all done, you'll end up with this:

 

Every thing is completely packaged up, with multiple projects, 3rd party libraries, tools, and an automated build.  The best part is that we can keep the template up to date and re-publish easily.

#1
jdn
4.13.2009
1:36 PM

That's freakin' awesome. Thanks for the post, time to download.


#2
mknopf
4.13.2009
4:32 PM

Ben

As usual your article is great (I appreciate your consistent contribution to the community). I work at NASA (Kennedy Space Center, in Software Services) and we have been looking into this exact same thing lately. Your article has saved me from quite a bit of frustration, thanks a ton.

Michael Knopf


#3
Maggie
4.13.2009
7:45 PM

Great article - gave me some things to look at - I've not tried templates in VS before.


#4
Jones
4.24.2009
1:36 PM

Nice blog...


#5
Rick Strahl
4.27.2009
4:55 PM

Thanks Ben for pointing me at Solution Factory. This looks like an awesome tool to get around some of the issues I've been dealing with with manual template creation.


#6
Jack
4.29.2009
11:01 PM

No Replacement parameters? Sorry but not much point creating templates without them.


© 2004-2008 Ben Scheirman | Subscribe to this blog

Elegance is not optional - Richard O'Keefe

about


My name is Ben Scheirman. I'm a software developer from Houston, TX. Read more...

Ads by The Lounge

Working with 50 Million cells has never been faster! Check out the examples and read more.



recent tweets

  • "Drinking this Cold Stone shake is like slurping 68 strips of bacon" 2,010 calories! http://bit.ly/9Kq7wB
  • @JakeS so for the past year or so I've been using a mac, I haven't used any new vim features :/
  • @JakeS yeah apparently if you launch vim without the -N flag, you get vim in compatibility mode (none of the new stuff)

recent bookmarks

  • iPod, iPhone and iPad Firmware Download
  • Easy, Modular Code Sharing Across iPhone Apps: Static Libraries and Cross-Project References — ClintHarris.net
  • UIKeyboardTypeNumberPad and the missing "return" key
  • almost effortless » Automated Heroku Backups
  • iPhone Development: A Few More Notes on Creating Universal Apps