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

Open Source Extensible Enterprise n-tier Application Framework with Multilayered Architecture

Rate me:
Please Sign up or sign in to vote.
4.67/5 (2 votes)
3 Dec 2010MIT3 min read 37K   770   41  
Xenta architecture overview
using System;
using System.Collections.Generic;
using System.Configuration;
using SiberTek.Xenta.Data.Config;
using SiberTek.Xenta.Data.Providers;
using SiberTek.Xenta.Exceptions;

namespace SiberTek.Xenta.Data
{
    /// <summary>
    /// Contains methods to create data provider objects or obtain 
    /// references to already created data provider objects
    /// </summary>
    public static class DataAccessManager
    {
        #region Fields
        private static readonly Object _mainLock = new Object();
        private static readonly Object _configurationLock = new Object();
        private static Dictionary<Type, IDataProvider> _providerInstanceCache = new Dictionary<Type, IDataProvider>();
        private static DataAccessConfiguration _configuration = null;
        #endregion

        #region Properties
        private static DataAccessConfiguration Configuration
        {
            get
            {
                lock(_configurationLock)
                {
                    if(_configuration == null)
                    {
                        _configuration = ConfigurationManager.GetSection("xentaDAL") as DataAccessConfiguration;
                    }
                }
                return _configuration;
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Creates an instance of the data provider designated by the specified generic type parameter and provider name
        /// </summary>
        /// <typeparam name="TProvider">The type of the provider</typeparam>
        /// <param name="provider">The name of the provider</param>
        /// <returns>A reference to the newly created data provider</returns>
        public static TProvider GetProviderInstance<TProvider>(string provider) where TProvider : IDataProvider
        {
            Type providerInterface = typeof(TProvider);

            if(!providerInterface.IsInterface)
            {
                throw new XentaException("T should be an data provider interface.");
            }
            if(String.IsNullOrEmpty(provider))
            {
                throw new XentaException("Provider name can't be null or empty.");
            }

            ProviderSettings providerSettings = Configuration.Providers[provider];

            if(providerSettings == null)
            {
                throw new XentaException("Cannot find provider configuration section.");
            }

            Type providerType = Type.GetType(providerSettings.Type, true);

            if(providerType.GetInterface(providerInterface.Name) == null)
            {
                throw new XentaException("Provider doesn't implement required interface.");
            }

            TProvider instance = default(TProvider);

            lock(_mainLock)
            {
                if(_providerInstanceCache.ContainsKey(providerType))
                {
                    instance = (TProvider)_providerInstanceCache[providerType];
                }
                else
                {
                    instance = (TProvider)Activator.CreateInstance(providerType);

                    if(instance == null)
                    {
                        throw new XentaException("Cannot create an instance of a provider type.");
                    }

                    instance.Initialize(provider, providerSettings.Parameters);
                    _providerInstanceCache.Add(providerType, instance);
                }
            }

            return instance;
        }
        #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 MIT License


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

Comments and Discussions