V. Data access at the dal Layer
Here we use the data persistence layer and pseudo SqlMapper object implemented in the previous article to implement data operations. Let's take a look at how the core Dao under Dal is implemented:
Remember how we implemented the dao class under IBatis.net? Yes, we can load dao configuration based on a base class BaseDAO and its constructor. But the implementation of Lou pig is not so complex and powerful. The implementation in this article is actually to get the key of the database connection object through BaseDAO and constructor, initialize a SqlMapper, then, use the SqlMapper object to perform basic CRUD and other data operations. So how can we use BaseDAO and constructor to initialize SqlMapper, just as in the Dal layer mentioned in the IBatis.net series earlier?
1. Under AdoNetDataaccess. Mapper, we define a public BaseDAO class.
Code
Namespace AdoNetDataAccess. Mapper
{
Public abstract class BaseDAO
{
# Region PRoperties
Public SqlMapper {get; set ;}
# Endregion
# Region Constructor
Private BaseDAO ()
{
}
/// <Summary>
/// SqlMapper attribute applies
/// </Summary>
/// <Param name = "mapperName"> </param>
Public BaseDAO (string mapperName)
{
This. SqlMapper = MapperUtill. GetMapper (mapperName );
}
# Endregion
}
}
2. initialize the handler class of SqlMapper
Code
Using System;
Using System. Collections. Generic;
Using System. Configuration;
Namespace AdoNetDataAccess. Mapper
{
Using AdoNetDataAccess. Core. Contract;
Using AdoNetDataAccess. Core. Implement;
Public sealed class MapperUtill
{
# Region fields
Public static string currentSqlKey = "sqlConn ";
Public static int timeout = 15;
Private static readonly object objSync = new object ();
Private static readonly IDictionary <string, SqlMapper> dictMappers = new Dictionary <string, SqlMapper> ();
# Endregion
# Region constructor and methods
Private MapperUtill ()
{
}
Static MapperUtill ()
{
Try
{
Specified Timeout = int. Parse (ConfigurationManager. deleettimeout ["db_timeOut"]);
}
Catch
{
Required timeout = 15;
}
// Instantiate SqlDbMapper
For (int I = 0; I <ConfigurationManager. ConnectionStrings. Count; I ++)
{
String key = ConfigurationManager. ConnectionStrings [I]. Name;
String value = ConfigurationManager. ConnectionStrings [I]. ConnectionString;
CreateMapper (key, value, interval timeout );
}
}
Public static SqlMapper GetSqlMapper (string key)
{
Return MapperUtill. GetMapper (key );
}
Public static SqlMapper GetCurrentSqlMapper ()
{
Return MapperUtill. GetMapper (currentSqlKey );
}
Public static void CreateMapper (string connKey, string sqlConStr, int connTimeOut)
{
IDbOperation operation = new SqlServer (sqlConStr, connTimeOut );
SqlMapper mapper = new SqlMapper (operation );
DictMappers. Add (connKey. ToUpper (). Trim (), mapper); // case insensitive
}
Public static SqlMapper GetMapper (string sqlConKey)
{
If (string. IsNullOrEmpty (sqlConKey ))
{
Throw new Exception ("the primary key of the database connection string is null! ");
}
SqlConKey = sqlConKey. ToUpper (); // case insensitive
SqlMapper mapper = null;
If (dictMappers. ContainsKey (sqlConKey ))
{
Mapper = dictMappers [sqlConKey];
}
Else
{
Throw new Exception (string. Format ("no database connection for {0}", sqlConKey ));
}
Return mapper;
}
/// <Summary>
/// Release all
/// </Summary>
Public void Release ()
{
Foreach (KeyValuePair <string, SqlMapper> kv in dictMappers)
{
SqlMapper mapper = kv. Value;
If (mapper = null)
{
Continue;
}
Mapper. CurrentDbOperation. CloseConnection ();
}
DictMappers. Clear ();
}
# Endregion
}
}
The main function of this connector class is to initialize the connectionStrings configuration node in the configuration file to obtain the connection string required by the SQL connection object.
3. PersonDao class
The following is the data operation for the specific Person table:
Code
Using System. Collections. Generic;
Using System. Data;
Namespace AdoNetDataAccess. Dal. Dao
{
Using AdoNetDataAccess. Dal. Model;
Using AdoNetDataAccess. Dal. Utility;
Using AdoNetDataAccess. Mapper;
Public class PersonDao: BaseDAO
{
Public PersonDao ()
: Base ("sqlConn") // sqlConn is a name of the <connectionStrings> Configuration node.
{
}
Public int Insert (string sqlInsert)
{
Int id = this. SqlMapper. Insert (sqlInsert );
// Object obj = this. SqlMapper. ExecuteScalar (sqlInsert, System. Data. CommandType. Text, null );
Return id;
}
Public bool BatchInsert (IList <Person> listModels)
{
Int batchSize = 50000;
Int copyTimeOut = 60;
DataTable dt = DataTableHelper. CreateTable <Person> (listModels );
Bool flag = this. SqlMapper. BatchInsert (typeof (Person). Name, batchSize, copyTimeOut, dt );
Return flag;
}
Public int Update (string sqlUpdate)
{
Int result = this. SqlMapper. Update (sqlUpdate );
Return result;
}
Public IList <Person> SelectPersons (string sqlSelect)
{
IList <Person> listPersons = this. SqlMapper. QueryForList <Person> (sqlSelect );
Return listPersons;
}
Public IDictionary <int, Person> SelectDictPersons (string sqlSelect)
{
IDictionary <int, Person> dictPersons = this. SqlMapper. QueryForDictionary <in