Wednesday, January 18, 2012

Using a Global AssemblyInfo File

I’ve been meaning to post this for a long time, but for some reason it always seems to fall off the radar. Not anymore…

For all but the most trivial applications, you will most likely separate your solution into multiple projects that each have their own responsibility. This allows for better code re-use and better separation of concerns.

An issue that you might end up facing is that each project contains it’s own AssemblyInfo.cs file and several of the assembly directives in these have values that are repeated. For instance:

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyCompany( "KyKoSoft" )]
[assembly: AssemblyProduct( "KyKoSoft Application Foo" )]
[assembly: AssemblyCopyright( "COPYRIGHT © 2012 KyKoSoft.  All Rights Reserved." )]
[assembly: AssemblyTrademark( "" )]
[assembly: AssemblyCulture( "en-US" )]

The values for AssemblyCompany, AssemblyProduct, AssemblyCopyright, etc. will most likely have the exact same values for each project in the solution. Would you want to manually update the AssemblyCopyright setting in each project on January 1st every year? What if your company is bought out or your product gets renamed?


In order to keep things DRY (Don’t Repeat Yourself) and make updates easier, a real simple solution is to extract the repeated/common values into a new file (say GlobalAssemblyInfo.cs) located at the root of your solution. Then, right-click each project and select Add | Existing Item… and navigate to the root of your solution and select the GlobalAssemblyInfo.cs file.


But wait! Instead of just clicking the Add button (or double clicking on the file), use the little down arrow on the Add button and select the Add As Link option. This will ‘link’ the file into the project instead of copying it into the project directory (remember, we want to maintain this information only once).


If you tried to compile the project at this point, you will get a compilation error stating that there were duplicate Assembly directives defined. This is because your original AssemblyInfo.cs file (located in the Properties folder) still has these directives in it. Simply open it up and remove the duplicated (global) entries and recompile.


Presto! No more duplication!

3 comments:

Unknown said...

This post is invaluable, thank you.

Kumar Sadhu said...

Hi,

Thank you for your post. If a new project is created, how do we force the developers or the VS to have the share assembly info file as a link by default?

Thanks
Kumar

sasitamil said...

I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing..
Believe me I did wrote an post about tutorials for beginners with reference of your blog. 




Selenium training in bangalore
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training