Click here to Skip to main content
15,891,184 members
Articles / Web Development / ASP.NET

Implementing Model-View-Presenter in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.80/5 (27 votes)
17 Nov 2007CPOL12 min read 129.6K   2.7K   120  
Three implementations of Model-View-Presenter in ASP.NET 2.0.
/***************************************************************************
Note to Dev Team, Contributors, all people of the Earth...
 * PLEASE DO NOT CHANGE THIS FILE. ever.
 * It's just the way we want it.
 * If there is an issue please contact me (Rob) and let me know before any checkins
 * 
 * Love and Kisses
 * Rob

****************************************************************************/
using System;
using System.CodeDom;
using System.Text;
using System.Web.Compilation;
using SubSonic.Utilities;
using SubSonic.CodeGenerator;

namespace SubSonic
{
	/// <summary>
	/// The Magic...
	/// </summary>
    public class BuildProvider : System.Web.Compilation.BuildProvider
	{
        /// <summary>
        /// This is the main method used by the build provider
        /// </summary>
        /// <param name="assemblyBuilder"></param>
		public override void GenerateCode(AssemblyBuilder assemblyBuilder)
		{
			// retrieve input file
			Utility.WriteTrace("Invoking BuildProvider");

			//using (Stream inFile = VirtualPathProvider.OpenFile(VirtualPath))
			//{
                //StreamReader rdr = new StreamReader(inFile);
                //string tableList = rdr.ReadToEnd();
                //rdr.Close();

				ICodeLanguage language;
				try
				{
					language = CodeLanguageFactory.GetByCodeProviderName(GetDefaultCompilerType().CodeDomProviderType.Name);
				}
				catch
				{
					language = new CSharpCodeLanguage();
				}
        		Utility.WriteTrace("Setting language to " + language.Identifier);

				BuildCode(assemblyBuilder, language);
			//}
		}

		private void BuildCode(AssemblyBuilder ab, ICodeLanguage language)
		{
			//make sure the providers are loaded
			Utility.WriteTrace("Initializing Providers....");
			DataService.LoadProviders();

			string usings = ParseUtility.GetUsings(language);
			StringBuilder code = new StringBuilder();
			code.Append(usings);

			//loop over the providers, and generate code for each
			foreach (DataProvider provider in DataService.Providers)
			{
				Utility.WriteTrace("Creating code for " + provider.Name);
				string[] tableList = DataService.GetTableNames(provider.Name);
				string[] viewList = DataService.GetViewNames(provider.Name);


				//generate the classes
				foreach (string tbl in tableList)
				{
					code.Append(CodeService.RunClass(tbl, provider.Name, language));
					Utility.WriteTrace("Generating Class " + tbl);
				}

				//generate the ODS Controllers
				foreach (string tbl in tableList)
				{
					code.Append(CodeService.RunODS(tbl, provider.Name, language));
					Utility.WriteTrace("Generating ODS Controller for:  " + tbl);
				}


				//generate the read-only
				Utility.WriteTrace("Generating View List");
				foreach (string view in viewList)
				{
					code.Append(CodeService.RunReadOnly(view, provider.Name, language));
					Utility.WriteTrace("Generating ReadOnly " + view);
				}

				//sps
				Utility.WriteTrace("Generating SPs");
				code.Append(CodeService.RunSPs(provider.Name, language));
			}

			//structs are built for all providers
			Utility.WriteTrace("Generating Structs");
			
            code.Append(CodeService.RunStructs(language));
			ab.AddCodeCompileUnit(this, new CodeSnippetCompileUnit(code.ToString()));
		}
	}
}

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 Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions