|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionIn the .NET environment there are few things left that are not strongly typed. The application settings, based on the .config files are one of them. When you access the settings through During the short analysis part of this project, I determined a few other requirements /nice features:
BackgroundI had this idea while using strongly typed Apparently, Microsoft changed its mind about letting VS.NET users write their own custom tools. The interfaces and base classes that were public in VS.NET 2002 disappeared in VS.NET 2003. Since some people already had code that used these interfaces, they released the source code on GotDotNet. If you ever want to write a custom tool, get this file. Another interesting tidbit about this project is the integration of new file types in VS.NET's "Add new item..." dialog. I wanted to be able to add a special file to my project, define the values and types I wanted to access, and have the custom tool do the rest. Fortunately Chris Sells has another article on how to add your own file types to VS.NET. After a bit of prototyping, it became clear that I would need to generate quite a bit of code. CodeDom could have been a good solution, but having used it a bit, I knew it would be a real pain to code that part. So I decided to use CodeSmith instead. It is a code generator that relies on a ASP.NET-like syntax and you can use its engine in your code. You will need to download and install it to get this tool to work. UsageI decided to forget about .config files and use a simple XML file so as not to interfere with the existing The tool works as follows:
The codeThere is not much code involved in making such a custom tool. You simply write a class that inherits from the Template codeThe trick here is to clearly separate in your mind the generator
code from the generated code. Since they are mixed in a single
ASP.NET-like file it is easy to lose track of it. As an example, here
is the part of the template that generates the strongly typed
properties for each entry in the XML file. The parts in <% foreach(XmlNode n in doc.SelectNodes("/Configuration/Entry"))
{%>
private <%=n.Attributes["Type"].Value%> m_<%=n.Attributes["Name"].Value%>;
<% if(n.Attributes["Description"] != null)
{
%>
/// <summary>
/// <%=n.Attributes["Description"].Value%>
/// </summary>
[Description("<%=n.Attributes["Description"].Value%>")]
<%
}%>
[Category("Values")]
public <%=n.Attributes["Type"].Value%> <%=n.Attributes["Name"].Value%>
{
get { return m_<%=n.Attributes["Name"].Value%>; }
set { m_<%=n.Attributes["Name"].Value%> = value; }
}
<%
}
%>
You can analyze the rest of the template and particularly the Generated codeLike private static Config1 m_settings;
public static Config1 Settings
{
get
{
if(m_settings == null)
m_settings = new Config1();
return m_settings;
}
}
//[...]
private Config1()
{
/// [...]
}
In order to monitor changes made to the configuration file externally, a The The class also inherits from Points of interestThe most difficult aspect of this project was certainly the installation procedure required to integrate it to Visual Studio correctly. There are files to put at the right place, registry keys to read to determine where to put the files, components to register etc. I used NSIS (Winamp's installer) for that, because it is powerful but it is also fairly difficult to use. The install script and files required to generate the installer is included in the source package, in case you want to have a look at it. UpdatesLatest versions will be posted on my weblog and on Code Project. Feel free to post comments or suggestions. DownloadsNOTE : The source code package contains only the source code which in itself is not functional. You need to run the installer to get the tool to work. History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||