Click here to Skip to main content
15,893,594 members
Articles / Desktop Programming / WPF

Layers Pattern in Practice

Rate me:
Please Sign up or sign in to vote.
4.96/5 (59 votes)
23 Apr 2010CPOL25 min read 153K   8.1K   187  
Layers Pattern via a WPF project.
using System;
using System.Collections.Generic;
using System.Linq;
using BillsDalLib;
using BillsEntityLib;
using System.Diagnostics;

namespace BillsBusinessLogicLib
{
    /// <summary>
    /// BillManager class for business logic management
    /// </summary>
    public class BillsManager : IBillsManager
    {
        private static BillsManager _manager;
        private readonly IDalBillsManager _dalManager;
        private static object lockObject = new object();
        private static BooleanSwitch _bSwitch;
        #region Constructor
        private BillsManager()
        {
            _dalManager = XmlDalBillsManager.GetInstance(); //set underlying DAL object to XML Based data storage
            _bSwitch = new BooleanSwitch("BooleanSwitch", "Switch from config file"); //if no such switch in config file _bSwitch == false
            if(_bSwitch.Enabled)
                Trace.TraceInformation("Singleton Instance Created:" + this.GetType().ToString());
        }
        /// <summary>
        /// Private constructor [Singleton class]
        /// </summary>
        private BillsManager(params object [] settings) : this()
        {        
            _dalManager.Settings = settings;
        }
        #endregion
        #region Singleton
        /// <summary>
        /// Gets the singleton instance of BillsManager class
        /// </summary>
        /// <returns>BillsManager object</returns>
        public static BillsManager GetInstance()
        {
            if (_manager == null)   //lazy initialization
            {
                lock (lockObject)
                {
                    if (_manager == null)
                    {
                        _manager = new BillsManager();
                    }
                }
            }
            return _manager;
        }
        #endregion
        #region IBillsManager Members
        /// <summary>
        /// Reads recent data from the datasource
        /// </summary>
        /// <returns>List of recent bills</returns>
        public Bills Read(DateTime fromDate, DateTime toDate)
        {
            try
            {
                return _dalManager.Read(fromDate, toDate);
            }
            catch (DalBillManagerException e)
            {
                throw new BillManagerException(e.Message, e);
            }
        }

        /// <summary>
        /// Read an item from the datasource by its ID
        /// </summary>
        /// <param name="guid">Bill to read</param>
        /// <returns>Bill object</returns>
        public Bill ReadById(Guid guid)
        {
            if (Guid.Empty == guid)
                throw new ArgumentException("guid argument cannot be null");
            try
            {
                return _dalManager.ReadById(guid);
            }
            catch (DalBillManagerException e)
            {
                throw new BillManagerException(e.Message, e);
            }
        }

        /// <summary>
        /// Insert a bill into the datasource
        /// </summary>
        /// <param name="bill">Bill object to insert</param>
        public void Insert(Bills bill)
        {
            if (bill == null || bill.Items == null || bill.Items.Count() == 0)    //no need to call Insert operatio on DAL Layer
                return;
            try
            {
                _dalManager.Insert(bill);
            }
            catch (DalBillManagerException e)
            {
                throw new BillManagerException(e.Message, e);
            }
        }

        /// <summary>
        /// Delete a bill from datasource
        /// </summary>
        /// <param name="billId">Bill to delete</param>
        /// <returns>Number of deleted items</returns>
        public int Delete(Guid billId)
        {
            if (Guid.Empty == billId)
                throw new ArgumentException("billId argument cannot be null");
            try
            {
                return _dalManager.Delete(billId);
            }
            catch (DalBillManagerException e)
            {
                throw new BillManagerException(e.Message, e);
            }
        }

        /// <summary>
        /// Delete a list of bills from the datasource
        /// </summary>
        /// <param name="guids">Guid to delete</param>
        /// <returns>Number of delete items</returns>
        public int Delete(IEnumerable<Guid> guids)
        {
            if (guids == null || guids.Count() == 0) //no items to delete
                return 0;
            try
            {
                return _dalManager.Delete(guids);
            }
            catch (DalBillManagerException e)
            {
                throw new BillManagerException(e.Message, e);
            }
        }

        /// <summary>
        /// Update bill in the datasource
        /// </summary>
        /// <param name="billId">Bill to update</param>
        /// <param name="newBill">New bill parameters</param>
        /// <returns>Number of updated items</returns>
        public int Update(Guid billId, Bill newBill)
        {
            if (Guid.Empty == billId)
                throw new ArgumentException("billId argument cannot be null");
            try
            {
                return _dalManager.Update(billId, newBill);
            }
            catch (DalBillManagerException e)
            {
                throw new BillManagerException(e.Message, e);
            }
        }
        /// <summary>
        /// Settings for manager object
        /// </summary>
        public object [] Settings
        {
            get { return _dalManager.Settings; }
            set
            {
                try
                {
                    _dalManager.Settings = value;
                }
                catch (DalBillManagerException e)
                {
                    throw new BillManagerException(e.Message, e);
                }
            }
        }
        #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 Code Project Open License (CPOL)


Written By
Software Developer
Moldova (Republic of) Moldova (Republic of)
Interested in computer science, math, research, and everything that relates to innovation. Fan of agnostic programming, don't mind developing under any platform/framework if it explores interesting topics. In search of a better programming paradigm.

Comments and Discussions