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

Populating a business logical layer from Stored Procedures

, 29 Oct 2013
A brief look at a technique mapping tables to a basic business layer.
doPersistence-noexe.zip
doPersistence
doPersistence.csproj.user
doPersistence.csproj.vspscc
obj
Debug
Release
Properties
doLogicCode
bin
Debug
doLogicCode.vshost.exe.manifest
doLogicCode.csproj.vspscc
obj
x86
Debug
doLogicCode.frmCodeGenerator.resources
doLogicCode.Properties.Resources.resources
Properties
Settings.settings
doPersistence.zip
Test
doLogic
bin
Debug
doLogic.dll
doLogic.pdb
doPersistence.dll
doPersistence.pdb
Release
doLogic.dll
doLogic.pdb
doPersistence.dll
doPersistence.pdb
obj
Debug
DesignTimeResolveAssemblyReferencesInput.cache
doLogic.dll
doLogic.pdb
ResolveAssemblyReference.cache
TempPE
Release
DesignTimeResolveAssemblyReferencesInput.cache
doLogic.dll
doLogic.pdb
ResolveAssemblyReference.cache
TempPE
Properties
doLogicCode
bin
Debug
doLogicCode.exe
doLogicCode.pdb
doLogicCode.vshost.exe
doLogicCode.vshost.exe.manifest
doPersistence.dll
doPersistence.pdb
obj
x86
Debug
DesignTimeResolveAssemblyReferencesInput.cache
doLogicCode.exe
doLogicCode.frmCodeGenerator.resources
doLogicCode.pdb
doLogicCode.Properties.Resources.resources
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
ResolveAssemblyReference.cache
TempPE
Properties.Resources.Designer.cs.dll
Properties
Settings.settings
doPersistence
bin
Debug
doPersistence.dll
doPersistence.pdb
Release
doPersistence.dll
doPersistence.pdb
doPersistence.csproj.vspscc
obj
Debug
DesignTimeResolveAssemblyReferencesInput.cache
doPersistence.dll
doPersistence.pdb
TempPE
Release
DesignTimeResolveAssemblyReferencesInput.cache
doPersistence.dll
doPersistence.pdb
TempPE
Properties
Test.suo
TestSite
Account
App_Data
bin
doLogic.dll
doLogic.pdb
doPersistence.dll
doPersistence.pdb
TestSite.dll
TestSite.pdb
Global.asax
obj
Debug
DesignTimeResolveAssemblyReferencesInput.cache
ResolveAssemblyReference.cache
TempPE
TestSite.dll
TestSite.pdb
Release
CSAutoParameterize
original
Account
transformed
Account
Database
DesignTimeResolveAssemblyReferencesInput.cache
Package
PackageTmp
Account
bin
doLogic.dll
doLogic.pdb
doPersistence.dll
doPersistence.pdb
TestSite.dll
TestSite.pdb
Global.asax
Scripts
Styles
ResolveAssemblyReference.cache
TempPE
TestSite.dll
TestSite.pdb
TransformWebConfig
original
transformed
Account
Properties
Scripts
Styles
TestSite.csproj.user
dopersistenceexample.zip
doLogic.dll
doPersistence.dll
doPersistence.dll
doLogic.dll
doPersistence.dll
TestSite.dll
Global.asax
TestSite.csproj.user
dopersistencepocodynamicsqlexample.zip
doLogic.dll
doLogic.pdb
doPersistence.dll
doPersistence.pdb
doLogic.dll
doLogic.pdb
doPersistence.dll
doPersistence.pdb
DesignTimeResolveAssemblyReferencesInput.cache
doLogic.dll
doLogic.pdb
ResolveAssemblyReference.cache
DesignTimeResolveAssemblyReferencesInput.cache
doLogic.dll
doLogic.pdb
ResolveAssemblyReference.cache
doPersistence.dll
doPersistence.pdb
doPersistence.dll
doPersistence.pdb
DesignTimeResolveAssemblyReferencesInput.cache
doPersistence.dll
doPersistence.pdb
DesignTimeResolveAssemblyReferencesInput.cache
doPersistence.dll
Test.suo
doLogic.dll
doLogic.pdb
doPersistence.dll
doPersistence.pdb
TestSite.dll
TestSite.pdb
Global.asax
DesignTimeResolveAssemblyReferencesInput.cache
ResolveAssemblyReference.cache
TestSite.dll
TestSite.pdb
DesignTimeResolveAssemblyReferencesInput.cache
ResolveAssemblyReference.cache
TestSite.dll
TestSite.pdb
TestSite.csproj.user
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

using System.Reflection;
using System.Configuration;

namespace doPersistence
{
    public static class Database
    {
        internal static string _ConnectionString = "";
        private static string _SpPrefix = "";
        public static Exception InitializationError = null;

        static Database()
        {
            try
			{
                _ConnectionString = ConfigurationManager.ConnectionStrings["doPersistence"].ConnectionString;
                _SpPrefix = ConfigurationManager.AppSettings["doPersistenceSpPrefix"];
            }
            catch (Exception e) { InitializationError = e; }
        }

        public static string SpPrefix { get { return _SpPrefix; } }
        public static bool AllowsDirectSql { get { return SqlServer._AllowsDirectSql; } }

        public static void SetConnectionString(string namedConnString)
        {
            //better get that named conn string right...
            _ConnectionString = ConfigurationManager.ConnectionStrings[namedConnString].ConnectionString;
        }

		public static void MapToTable(object o, DataTable table)
		{
			DataRow row = table.NewRow();
			foreach (DataColumn col in table.Columns)
			{
				object value = Class.GetPropertyValueByName(o, col.ColumnName);

				if (Class.IsNull(value))
					row[col.ColumnName] = DBNull.Value;
				else
					row[col.ColumnName] = value;
			}
			table.Rows.Add(row);
		}

		public static void MapToInstance(IDataReader reader, object o)
		{
			for (int i = 0; i < reader.FieldCount; i++)
			{
				if (reader.IsDBNull(i))
					Class.SetPropertyValueByName(o, reader.GetName(i), null);
				else
					Class.SetPropertyValueByName(o, reader.GetName(i), reader.GetValue(i));
			}
		}

        public static DataTable ExecuteDataTable(string spName, object properties)
        {
            return SqlServer.ExecuteDataTable(spName, properties, Database._ConnectionString);
        }

		public static void MapParameterValues(object o, DbCommand cmd)
		{
			foreach (DbParameter parm in cmd.Parameters)
			{
				string name = parm.ParameterName;
				if (name.StartsWith("@"))
					name = name.Substring(1);

				object value = Class.GetPropertyValueByName(o, name);

				if (Class.IsNull(value))
				{
					if (!parm.IsNullable)
						throw new ApplicationException(String.Format("Property value cannot be null on {0}: {1}.", o.GetType().Name, name));

					parm.Value = DBNull.Value;
				} else
					parm.Value = value;
			}
		}

		public static void Load(object o) { Load(o, ""); }
		public static void Load(object o, string action)
		{
			SqlServer.Load(o, action);
		}

		public static int LoadAll<T>(PersistentList<T> list) { return Database.LoadAll(list, ""); }
		public static int LoadAll<T>(PersistentList<T> list, string action)
		{
			return SqlServer.LoadAll(list, action, _ConnectionString);
		}

		public static int Store<T>(T o)
		{
            int _return = SqlServer.Store(o);
			foreach (PropertyInfo pi in o.GetType().GetProperties())
			{
				string name = "_" + pi.Name;
				if (typeof(PersistentList<T>).IsAssignableFrom(pi.PropertyType))
				{
					if (Class.GetFieldValueByName(o, name) != null)
						((PersistentList<T>)pi.GetValue(o, null)).Store();
				}
			}
            return _return;
		}

		public static bool Delete(object o)
		{
			return SqlServer.Delete(o);
		}

		public static DataTable GetTableStructure(string tableName)
		{
			return SqlServer.GetTableStructure(tableName, _ConnectionString);
		}

		public static void StoreBulk<T>(PersistentList<T> list)
		{
			DataTable table = Database.GetTableStructure(list.GetItemDataSource);
 			list.ToDataTable(table);
			SqlServer.StoreBulk(list.GetItemDataSource, table, _ConnectionString);
		}

		public static DbCommand GetInsertCommand(object o)
		{
			return SqlServer.GetInsertCommand(o, _ConnectionString);
		}

		public static DbCommand GetUpdateCommand(object o)
		{
			return SqlServer.GetUpdateCommand(o, _ConnectionString);
		}
	}
}

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

andrewbb@gmail.com
Architect
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 29 Oct 2013
Article Copyright 2012 by andrewbb@gmail.com
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid