Click here to Skip to main content
15,885,546 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 36.9K   770   41  
Xenta architecture overview
using System;
using SiberTek.Xenta.Data;
using SiberTek.Xenta.Data.Providers;
using SiberTek.Xenta.Entities;
using SiberTek.Xenta.Entities.Collections;
using SiberTek.Xenta.Enums;
using SiberTek.Xenta.Events;
using SiberTek.Xenta.Exceptions;
using SiberTek.Xenta.Utils;

namespace SiberTek.Xenta.Managers
{
    /// <summary>
    /// Contains methods to manage currencies
    /// </summary>
    public class CurrencyManager : IManager
    {
        #region Fields
        private static CurrencyManager _instance = null;
        private ICurrencyDataProvider _dataProvider;
        private bool _initialized;
        #endregion

        #region Constructors
        private CurrencyManager()
        {
            _dataProvider = null;
            _initialized = false;
        }
        #endregion

        #region Events
        public event EntityEventHandler CurrencyCreated;
        public event EntityEventHandler CurrencyUpdated;
        public event EntityEventHandler CurrencyDeleted;
        #endregion

        #region Properties
        public static CurrencyManager Instance
        {
            get
            {
                if(_instance == null)
                {
                    _instance = new CurrencyManager();
                }
                return _instance;
            }
        }

        private ICurrencyDataProvider DataProvider
        {
            get
            {
                return _dataProvider;
            }
        }
        #endregion

        #region Methods
        public void Initialize()
        {
            if(!_initialized)
            {
                _dataProvider = DataAccessManager.GetProviderInstance<ICurrencyDataProvider>("CurrencyDataProvider");
                _initialized = true;
            }
        }

        public CurrencyInfo CreateCurrency(string name, string displayName, string priceFormat, bool isActive)
        {
            if(String.IsNullOrEmpty(name))
            {
                throw new XentaException("Currency name is not valid", (int)CoreError.CurrencyNameIsNotValid);
            }

            name = StringHelper.TrimToLength(name, 3);

            if(GetCurrencyByName(name) != null)
            {
                throw new XentaException("Currency already exists", (int)CoreError.DuplicateCurrency);
            }

            CurrencyInfo currency = null;
            int currencyID;
            displayName = StringHelper.TrimToLength(displayName, 50);
            priceFormat = StringHelper.TrimToLength(priceFormat, 20);

            if(DataProvider.InsertCurrency(name, displayName, priceFormat, isActive, DateTime.UtcNow, DateTime.UtcNow, out currencyID))
            {
                currency = GetCurrency(currencyID);
                FireCurrencyCreated(currency);
            }
            return currency;
        }

        public CurrencyInfo GetCurrency(int currencyID)
        {
            return DataMapper.Map(DataProvider.GetCurrency(currencyID));
        }

        public CurrencyInfo GetCurrencyByName(string name)
        {
            if(String.IsNullOrEmpty(name))
            {
                return null;       
            }

            name = StringHelper.TrimToLength(name, 3);

            return DataMapper.Map(DataProvider.GetCurrencyByName(name));
        }

        public CurrencyInfoCollection GetAllCurrencies()
        {
            return GetAllCurrencies(false);
        }

        public CurrencyInfoCollection GetAllCurrencies(bool showHidden)
        {
            return GetAllCurrencies(null, null, showHidden);
        }

        public CurrencyInfoCollection GetAllCurrencies(DateTime? createdOnStart, DateTime? createdOnEnd, bool showHidden)
        {
            return DataMapper.Map(DataProvider.GetAllCurrencies(createdOnStart, createdOnEnd, showHidden));
        }

        public CurrencyInfo GetDefaultCurrency()
        {
            return GetCurrency(SettingManager.Instance.GetParam<Int32>("Currency.Default"));
        }

        public CurrencyInfo UpdateCurrency(int currencyID, string name, string displayName, string priceFormat, bool isActive)
        {
            if(String.IsNullOrEmpty(name))
            {
                throw new XentaException("Currency name is not valid", (int)CoreError.CurrencyNameIsNotValid);
            }

            CurrencyInfo currency = GetCurrency(currencyID);

            if(currency == null)
            {
                throw new XentaException("Currency does not exist", (int)CoreError.CurrencyDoesNotExist);
            }

            name = StringHelper.TrimToLength(name, 3);

            if(!currency.Name.Equals(name) && GetCurrencyByName(name) != null)
            {
                throw new XentaException("Currency already exists", (int)CoreError.DuplicateCurrency);
            }

            displayName = StringHelper.TrimToLength(displayName, 50);
            priceFormat = StringHelper.TrimToLength(priceFormat, 20);

            if(DataProvider.UpdateCurrency(currencyID, name, displayName, priceFormat, isActive, currency.CreatedOn, DateTime.UtcNow))
            {
                currency = GetCurrency(currencyID);
                FireCurrencyUpdated(currency);
            }
            return currency;
        }

        public void DeleteCurrency(int currencyID)
        {
            if(currencyID == SettingManager.Instance.GetParam<Int32>("Currency.Default"))
            {
                throw new XentaException("Cannot delete default currency", (int)CoreError.CannotDeleteDefaultCurrency);
            }

            CurrencyInfo currency = GetCurrency(currencyID);

            if(currency != null)
            {
                FireCurrencyDeleted(currency);
                DataProvider.DeleteCurrency(currencyID);
            }
        }
        #endregion

        #region Utilties
        private void FireCurrencyCreated(CurrencyInfo currency)
        {
            if(CurrencyCreated != null)
            {
                CurrencyCreated(this, new EntityEventArgs(currency));
            }
        }

        private void FireCurrencyUpdated(CurrencyInfo currency)
        {
            if(CurrencyUpdated != null)
            {
                CurrencyUpdated(this, new EntityEventArgs(currency));
            }
        }

        private void FireCurrencyDeleted(CurrencyInfo currency)
        {
            if(CurrencyDeleted != null)
            {
                CurrencyDeleted(this, new EntityEventArgs(currency));
            }
        }
        #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