Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / XML

Strongly typed AppSettings with Genuilder.Extensibility

Rate me:
Please Sign up or sign in to vote.
4.71/5 (5 votes)
26 Jun 2010Ms-PL2 min read 21.3K   88   5  
Practical use case of Genuilder.Extensibility: Strongly typed AppSettings.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Genuilder.Extensibility;
using System.Configuration;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.Visitors;

namespace StronglyTypedAppSettingsPlugin2.Gen
{
	public class StronglyTypedAppSettingsPlugin : IPlugin
	{
		#region IPlugin Members

		public void Initialize(ICodeRepository repository)
		{
			repository.CodeItemCreated += new CodeItemCreatedHandler(repository_CodeItemCreated);
		}

		void repository_CodeItemCreated(ICodeRepository sender, CodeItem item)
		{
			if(item.Name.EndsWith(".config"))
			{
				var dependency = item.SourceOf("StronglyTypedSettings.cs");
				dependency.ShouldUpdateTarget += new CodeDependencyHandler(dependency_ShouldUpdateTarget);
			}
		}

		void dependency_ShouldUpdateTarget(CodeDependency sender, CodeItem target)
		{
			var configuration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
			{
				ExeConfigFilename = ((FileCodeItem)sender.Source).File.FullName
			}, ConfigurationUserLevel.None);


			var targetNRefactoryExtension = target.GetExtension<Genuilder.Extensibility.NRefactory.CompilationUnitExtension>();

			targetNRefactoryExtension.CompilationUnit.Children.Clear();
			var typeDeclaration = new TypeDeclaration(Modifiers.Public | Modifiers.Static, null)
			{
				Name = "AppConfigSettings"
			};
			targetNRefactoryExtension.CompilationUnit.Children.Add(typeDeclaration);



			foreach(KeyValueConfigurationElement setting in configuration.AppSettings.Settings)
			{

				var propertyDeclaration = new PropertyDeclaration(Modifiers.Public | Modifiers.Static, null, setting.Key, null)
				{
					TypeReference = new TypeReference(typeof(String).FullName)
				};
				var indexer = new IndexerExpression(
					new MemberReferenceExpression(
						new TypeReferenceExpression(
							new TypeReference(typeof(ConfigurationManager).FullName)), "AppSettings")
								, new List<Expression>()
						{
							new ICSharpCode.NRefactory.Ast.PrimitiveExpression(setting.Key)
						});

				propertyDeclaration.GetRegion = new PropertyGetRegion(new BlockStatement(), null);
				propertyDeclaration.GetRegion.Block.Children.Add(new ReturnStatement(indexer));
				typeDeclaration.Children.Add(propertyDeclaration);

			}
			targetNRefactoryExtension.Save();
			target.Logger.Send("Settings generated !!", Genuilder.Extensibility.Utilities.LogType.Warning);
		}

		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer Freelance
France France
I am currently the CTO of Metaco, we are leveraging the Bitcoin Blockchain for delivering financial services.

I also developed a tool to make IaaS on Azure more easy to use IaaS Management Studio.

If you want to contact me, go this way Smile | :)

Comments and Discussions