Tuesday, 14 May 2013

The Business Logic


Carrying on with the proof of concept I coded up some BL classes and a test harness for them. The Asset entity has a numeric Type property which is the ID of an AssetType entity. The BL needs to handle this relationship so that the user will work with friendly names / artefacts on the UI. The BL is therefore the translator between the DAL and the UI and so needs to change the Type property from a number to a friendly string. To this end I created a class that deals with a single Asset entity, converting it to a UI version.
using System;
using DAL;
using EntityClasses;

namespace BL
{
    public class UIAsset : Asset
    {
        // Inherits all the members of Asset and hides the base class Type (int) to expose the UI name (string)
        new public string Type { get; set; }
    }

    public class BLAsset
    {
        private Asset _asset { get; set; }
        private AssetType _assetType { get; set; }
        private UIAsset _uiAsset { get; set; }

        // Constructors
        public BLAsset()
        {
        }

        public BLAsset(Int32 id)
        {
            LoadAsset(id);
        }

        // Destructor
        ~BLAsset()
        {
        }

        // Build a UIAsset class from the Asset and AssetType entities
        public bool LoadAsset(Int32 id)
        {
            Boolean returnVal = false;

            try
            {
                DAL_Asset dal_Asset = new DAL_Asset();
                _asset = dal_Asset.Retrieve(id);
            }

            catch (Exception e)
            {
                _asset = null;
                throw e;
            }

            if (_asset.Type != 0)
            {
                try
                {
                    DAL_AssetType dal_AssetType = new DAL_AssetType();
                    _assetType = dal_AssetType.Retrieve(_asset.Type);
                }

                catch (Exception e)
                {
                    _asset = null;
                    throw e;
                }
            }
            else
            {
                _asset = null;
                throw new Exception(string.Format("Invalid Asset type associated with Asset ID {0}", id.ToString()));
            }

            _uiAsset = new UIAsset();
            _uiAsset.AssetTag = _asset.AssetTag;
            _uiAsset.Description = _asset.Description;
            _uiAsset.ID = _asset.ID;
           
            // Get the Asset Type friendly name
            _uiAsset.Type = _assetType.sAssetType;

            returnVal = true;
            return returnVal;
        }

        public UIAsset GetUIAsset()
        {
            return _uiAsset;
        }
    }
}

This is all well and good, but the UI will need to work with more than just one asset. For instance, at some point the UI will need to show a list of assets in the system so that a user can select one for editing, say. To accommodate this requirement I coded another class to handle multiple assets and to offer a list of Asset Types (on creating a new Asset entity the user will need to select an Asset Type, probably from a dropdown list).
using System;
using System.Collections.Generic;
using DAL;
using EntityClasses;

namespace BL
{
    public class BLAssets
    {
        private List<UIAsset> _assets { get; set; }
        private List<AssetType> _assetTypes { get; set; }

        // Constructor
        // Get a list of all the assets in the system
        public BLAssets()
        {
            try
            {
                // Get a list of the Asset IDs in the system
                DAL_Asset dal_Asset = new DAL_Asset();
                List<Int32> IDs = dal_Asset.RetrieveIDs();

                _assets = new List<UIAsset>();
                BLAsset _asset = new BLAsset();

                // Iterate through the list of IDs adding each one to the list
                foreach (Int32 id in IDs)
                {
                    UIAsset newAsset = new UIAsset();
                    if (_asset.LoadAsset(id))
                    {
                        newAsset = _asset.GetUIAsset();
                        _assets.Add(newAsset);
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            try
            {
                DAL_AssetType dal_AssetType = new DAL_AssetType();
                _assetTypes = dal_AssetType.RetrieveAll();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public List<UIAsset> GetAssets()
        {
            return _assets;
        }

        public List<AssetType> GetAssetTypes()
        {
            return _assetTypes;
        }
    }
}

To test this functionality a very simple Console application was needed.
using System;
using System.Collections.Generic;

namespace BL
{
    class Program
    {
        static void Main(string[] args)
        {
            ReadAssets();
            ReadAssetTypes();

            Console.WriteLine("Press Any Key to continue....");
            Console.ReadKey(true);
        }

        static void ReadAssets()
        {
            Console.WriteLine("ReadAssets() test");
            BLAssets x = new BLAssets();
            List<UIAsset> assets = x.GetAssets();
            if (assets.Count > 0)
            {
                foreach (UIAsset asset in assets)
                {
                    Console.WriteLine("Asset details:");
                    Console.WriteLine("ID: {0}, Tag: {1}, Type {2}", asset.ID, asset.AssetTag, asset.Type);
                }
            }
            Console.WriteLine("");
        }

        static void ReadAssetTypes()
        {
            Console.WriteLine("ReadAssetTypes() test");
            BLAssets x = new BLAssets();
            List<EntityClasses.AssetType> assetTypes = x.GetAssetTypes();
            if (assetTypes.Count > 0)
            {
                foreach (EntityClasses.AssetType assetType in assetTypes)
                {
                    Console.WriteLine("AssetType details:");
                    Console.WriteLine("ID: {0}, AssetType: {1}", assetType.ID, assetType.sAssetType);
                }
            }

            Console.WriteLine("");
        }
    }
}

So there it is. I needed to add the App.Config and ConnectionStrings.config files to the test harness (see previous post).
The last step in the P.O.C was to build a simple thick-client UI.

No comments:

Post a Comment