Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

Implementing Model-View-Presenter in ASP.NET

, 17 Nov 2007 CPOL
Three implementations of Model-View-Presenter in ASP.NET 2.0.
MVPSampleApp.zip
MVP.SampleApp
Lib
Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Data.dll
MySql.Data.dll
nunit.framework.dll
Rhino.Mocks.dll
Model
Data
Interfaces
Properties
Presentation
Presentation.Tests
Properties
Interfaces
Properties
SubSonic
ActiveRecord
Builder
CodeGeneration
Templates
CodeLanguage
Configuration
Controls
Calendar
lang
skin
active-bg.gif
calendar.gif
dark-bg.gif
hover-bg.gif
menuarrow.gif
normal-bg.gif
rowhover-bg.gif
status-bg.gif
title-bg.gif
today-bg.gif
Resources
DataProviders
Properties
Sql Tools
SubSonic.snk
Sugar
WebApp
App_Data
Views
SQL2000SampleDb.zip
SQL2000SampleDb.msi
///////////////////////////////////////////////////////
// Code author: Martin Lapierre, http://devinstinct.com
///////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace SubSonic
{
    /// <summary>
    /// Provides access to configuration files for ASP.NET, EXEs and DLLs.
    /// </summary>
    public static class ConfigurationProvider
    {
        #region Fields

        /// <summary>
        /// The cache of configuration resolvers as a pair of assembly-full-name/resolver.
        /// </summary>
        private static Dictionary<string, ConfigurationResolver> _resolvers = new Dictionary<string, ConfigurationResolver>();

        #endregion Fields


        #region Properties

        /// <summary>
        /// Gets the configuration resolver instance for the calling method.
        /// </summary>
        public static ConfigurationResolver CurrentInstance
        {
            [MethodImpl(MethodImplOptions.NoInlining)] // Make sure the property is not inlined by the compiler.
            get
            {
                // The call to Assembly.GetCallingAssembly() must be in this property.
                return GetResolver(Assembly.GetCallingAssembly());
            }
        }

        /// <summary>
        /// Gets the ConnectionStringsSection data for the current application's default configuration.
        /// </summary>
        /// <remarks>
        /// The method uses the configuration file overrides, if set.
        /// Otherwise, it tries to use the assembly configuration file and
        /// falls back to app.config or web.config if the configuration information is not found.
        /// </remarks>
        public static ConnectionStringSettingsCollection ConnectionStrings
        {
            [MethodImpl(MethodImplOptions.NoInlining)] // Make sure the property is not inlined by the compiler.
            get
            {
                // The call to Assembly.GetCallingAssembly() must be in this property.
                return GetResolver(Assembly.GetCallingAssembly()).ConnectionStrings;
            }
        }

        /// <summary>
        /// Gets the AppSettingsSection data for the current application's default configuration. 
        /// </summary>
        /// <remarks>
        /// The method uses the configuration file overrides, if set.
        /// Otherwise, it tries to use the assembly configuration file and
        /// falls back to app.config or web.config if the configuration information is not found.
        /// </remarks>
        public static NameValueCollection AppSettings
        {
            [MethodImpl(MethodImplOptions.NoInlining)] // Make sure the property is not inlined by the compiler.
            get
            {
                // The call to Assembly.GetCallingAssembly() must be in this property.
                return GetResolver(Assembly.GetCallingAssembly()).AppSettings;
            }
        }

        #endregion Properties


        #region Methods

        /// <summary>
        /// Retrieves a specified configuration section for the current application's default configuration. 
        /// </summary>
        /// <remarks>
        /// The method uses the configuration file overrides, if set.
        /// Otherwise, it tries to use the assembly configuration file and
        /// falls back to app.config or web.config if the configuration information is not found.
        /// </remarks>
        /// <typeparam name="TSection">The type of the section to retrieve.</typeparam>
        /// <param name="sectionName">The configuration section path and name.</param>
        /// <returns>
        /// The specified ConfigurationSection object. 
        /// Throws a ConfigurationErrorsException if the section does not exist.
        /// </returns>
        [MethodImpl(MethodImplOptions.NoInlining)] // Make sure the method is not inlined by the compiler.
        public static TSection GetSection<TSection>(string sectionName)
            where TSection : System.Configuration.ConfigurationSection
        {
            // The call to Assembly.GetCallingAssembly() must be in this property.
            return GetResolver(Assembly.GetCallingAssembly()).GetSection<TSection>(sectionName);
        }

        /// <summary>
        /// Finds a project configuration files from a specified path.
        /// </summary>
        /// <remarks>
        /// The method starts by searching the given path, then goes up the directory hierarchy until 
        /// it finds the configuration files. It stops looking if a project, solution or the root
        /// directory is reached.
        /// </remarks>
        /// <param name="path">
        /// A file path or a directory path. 
        /// A directory path must end with Path.DirectorySeparatorChar.
        /// </param>
        /// <returns>The configuration files found.</returns>
        [MethodImpl(MethodImplOptions.NoInlining)] // Make sure the method is not inlined by the compiler.
        public static string[] FindProjectConfigFiles(string path)
        {
            // The call to Assembly.GetCallingAssembly() must be in this property.
            return GetResolver(Assembly.GetCallingAssembly()).FindProjectConfigFiles(path);
        }

        /// <summary>
        /// Gets the configuration resolver from the cache for the given assembly.
        /// </summary>
        /// <param name="assembly">The assembly to get the configuration resolver for.</param>
        /// <returns>The configuration resolver for the given assembly.</returns>
        private static ConfigurationResolver GetResolver(Assembly assembly)
        {
            if (!_resolvers.ContainsKey(assembly.FullName))
                lock (typeof(ConfigurationResolver))
                {
                    if (!_resolvers.ContainsKey(assembly.FullName))
                        _resolvers.Add(assembly.FullName, new ConfigurationResolver(assembly));
                }
            return _resolvers[assembly.FullName];
        }

        #endregion Methods
    }
}

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)

Share

About the Author

Alex Mueller
Web Developer
United States United States
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411022.1 | Last Updated 17 Nov 2007
Article Copyright 2007 by Alex Mueller
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid