Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Layers Pattern in Practice

, 23 Apr 2010
Layers Pattern via a WPF project.
AdditionalDependencies.zip
WPFVisifire.Charts.dll
AvalonControlsLibrary.dll
System.Windows.Controls.DataVisualization.Toolkit.dll
WPFToolkit.dll
BillsManager.zip
BillsManager
BillManagerUninstallAction
Properties
BillPayManager
Application.ico
BillPayManager.cd
BillPayManager.csproj.user
bin
Debug
AvalonControlsLibrary.dll
System.Windows.Controls.DataVisualization.Toolkit.dll
WPFToolkit.dll
WPFVisifire.Charts.dll
Charting
DataGrid
icon.ico
Images
about.png
archive.png
backArrow.png
bills.png
charts.png
fwdArrow.png
icon.ico
settings.png
Properties
Themes
BillPayManager.suo
BillsBusinessLogicLib
BillsBusinessLogicLib.csproj.user
BLLBills.cd
ciumac.sergiu.pfx
Properties
BillsDalLib
BillsDalClassDiagram.cd
BillsDalLib.csproj.user
ciumac.sergiu.pfx
DALSchema
Properties
BillsEntityLib
BillEntityClassDiagram.cd
BillsEntityLib.csproj.user
ciumac.sergiu.pfx
Properties
SetupBillPayManager
SetupBillPayManager.vdproj
TestBillPayManager
Properties
TestBillPayManager.csproj.user
TestBillPayManagerManual
AddBillManualTest.mht
DeleteBill.mht
Properties
Release.zip
AvalonControlsLibrary.dll
BillPayManager.exe
BillPayManager.vshost.exe
BillsBusinessLogicLib.dll
BillsDalLib.dll
BillsEntityLib.dll
System.Windows.Controls.DataVisualization.Toolkit.dll
WPFToolkit.dll
WPFVisifire.Charts.dll
en-US
BillPayManager.resources.dll
ro-RO
BillPayManager.resources.dll
ReleaseSetup.zip
SetupBillPayManager.msi
setup.exe
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)

Share

About the Author

Ciumac Sergiu
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.
Follow on   Twitter   LinkedIn

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 23 Apr 2010
Article Copyright 2010 by Ciumac Sergiu
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid