Introduction
DataAccessLayer is a code purely in c#. Only the DLL is released, to be referenced in the BLL layer.
Versatility and simplicity of use
- Connect to the following databases
Oracle 10/11 - SQLServer 2008/2012/2014 - MySQL 5.X.X -
This allows you to manage the CRUD functions in the database.
-
Only two methods exposed by the DLL are invoked:- ActionOnDB for Insert, Delete, Update- SelectFromDB for Select
-
It is possible to choose the type of data returned: JSON or SERIALIZED data.
-
It is possible to execute a stored procedure or a query.
Usability
The DLL is used as the last layer of architecture, with 3 or more levels. Below is an example that is only a standard of implementation and development of BLL, but the developer is free to develop and implement the BLL layer as he sees fit.
In a base class (BaseManage) to inherit
Create a type variable DAL BaseMenageDAL objDAL. Set the constructor to be accepted as the type of connection and the connection string.
- public BaseManage(TypeConnection typeConnectionBLL, string ConnectionString) {
- try {
- strConnection = ConnectionString;
- this.typeConnectionBLL = typeConnectionBLL;
- objDAL = new BaseMenageDAL((BaseMenageDAL.TypeConnection) typeConnectionBLL, ConnectionString);
- } catch (Exception Error) {
- throw new Exception("Error on the procedure : " + System.Reflection.MethodInfo.GetCurrentMethod().ToString() + "-" + Error.Message);
- }
- }
Then, we create the 3 enums exposed in the DLL
- public enum TypeResult {
- JSON = BaseMenageDAL.TypeResult.JSON,
- SERIALIZED = BaseMenageDAL.TypeResult.SERIALIZED
- }
- public enum TypeConnection {
- SQLServer = BaseMenageDAL.TypeConnection.SQLServer,
- Oracle = BaseMenageDAL.TypeConnection.Oracle,
- MySQL = BaseMenageDAL.TypeConnection.MySQL
- }
- public enum ActionOnDB {
- storeInsert = BaseMenageDAL.Actions.storeInsert,
- storeUpdate = BaseMenageDAL.Actions.storeUpdate,
- storeDelete = BaseMenageDAL.Actions.storeDelete,
- storeSelect = BaseMenageDAL.Actions.storeSelect,
- queryInsert = BaseMenageDAL.Actions.queryInsert,
- queryUpdate = BaseMenageDAL.Actions.queryUpdate,
- queryDelete = BaseMenageDAL.Actions.queryDelete,
- querySelect = BaseMenageDAL.Actions.querySelect,
- }
To set the type of connection from the caller, create an enum type property TypeConnection.
- public TypeConnection typeConnectionBLL { get; set; }
- internal int Managing(Object obj, string Store) {
- try {
- int ret = 0;
- ret = objDAL.ActionOnDB(obj, Store.Trim());
- objDAL = null;
- return ret;
- } catch (Exception Error) {
- throw new Exception("Error on the procedure : " + System.Reflection.MethodInfo.GetCurrentMethod().ToString() + "-" + Error.Message);
- }
- }
- internal object Selecting(Object obj, string Store, BaseMenageDAL.TypeResult type) {
- try {
- var ret = objDAL.SelectFromDB(obj, Store.Trim(), type);
- objDAL = null;
- return ret;
- } catch (Exception Error) {
- throw new Exception("Error on the procedure : " + System.Reflection.MethodInfo.GetCurrentMethod().ToString() + "-" + Error.Message);
- }
- }
- ActionOnDB for Insert,Delete,Update
- electFromDB for Select
- public class ManagingUser: BaseManage {
- public ManagingUser(TypeConnection typeConnectionBLL, string ConnectionString): base(typeConnectionBLL, ConnectionString) {}
- public class Registry {
- public string PK_Registry {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public string Surname {
- get;
- set;
- }
- public string Address {
- get;
- set;
- }
- public int ? _Eta {
- get;
- set;
- }
- public int ? Eta {
- get {
- return this._Eta.HasValue ?
- this._Eta.Value :
- (int ? ) null;
- }
- set {
- this._Eta = value;
- }
- }
- private DateTime ? _Data = null;
- public DateTime ? Data {
- get {
- return this._Data.HasValue ?
- this._Data.Value :
- (DateTime ? ) null;
- }
- set {
- this._Data = value;
- }
- }
- }
- public int ManageUser(Object obj, ActionOnDB action) {
- int ret = 0;
- string Store = string.Empty;
- try {
- switch (typeConnectionBLL) {
- case TypeConnection.SQLServer:
- case TypeConnection.MySQL:
- {
- switch (action) {
- case ActionOnDB.storeInsert:
- {
- Store = "sp_RegistryInsert";
- }
- break;
- case ActionOnDB.storeUpdate:
- {
- Store = "sp_RegistryUpdate";
- }
- break;
- case ActionOnDB.storeDelete:
- {
- Store = "sp_RegistryDelete";
- }
- break;
- }
- }
- break;
- case TypeConnection.Oracle:
- {
- switch (action) {
- case ActionOnDB.queryInsert:
- {
- Store = "insert into Registry (Name,Surname,Address,Eta) values ";
- Store += "( '" + ((Registry) obj).Name + "' ,";
- Store += " '" + ((Registry) obj).Surname + "' ,";
- Store += " '" + ((Registry) obj).Address + "' ,";
- Store += " " + ((Registry) obj).Eta + " )";
- }
- break;
- case ActionOnDB.queryUpdate:
- {
- Store = " UPDATE Registry ";
- Store += " set Name= '" + ((Registry) obj).Name + "' ,";
- Store += " Surname='" + ((Registry) obj).Surname + "' ,";
- Store += " Address='" + ((Registry) obj).Address + "' ,";
- Store += " Eta=" + ((Registry) obj).Eta + " ";
- Store += " WHERE PK_Registry=" + ((Registry) obj).PK_Registry + " ";
- }
- break;
- case ActionOnDB.queryDelete:
- {
- Store = "DELETE FROM Registry ";
- Store += " WHERE PK_Registry=" + ((Registry) obj).PK_Registry + " ";
- }
- break;
- }
- obj = null;
- }
- break;
- }
- ret = Managing(obj, Store.Trim());
- return ret;
- } catch (Exception Error) {
- throw new Exception("Error on the procedure : " + System.Reflection.MethodInfo.GetCurrentMethod().ToString() + "-" + Error.Message);
- }
- }
- public object SelectUser(Object obj, ActionOnDB action, TypeResult type) {
- string Store = string.Empty;
- try {
- switch (typeConnectionBLL) {
- case TypeConnection.SQLServer:
- case TypeConnection.MySQL:
- {
- switch (action) {
- case ActionOnDB.storeSelect:
- {
- Store = "sp_RegistrySelect";
- }
- break;
- }
- }
- break;
- case TypeConnection.Oracle:
- {
- switch (action) {
- case ActionOnDB.querySelect:
- {
- Store = "SELECT * FROM Registry";obj = null;
- }
- break;
- }
- }
- break;
- }
- var ret = Selecting(obj, Store.Trim(), (BaseMenageDAL.TypeResult) type);
- return ret;
- } catch (Exception Error) {
- throw new Exception("Error on the procedure : " + System.Reflection.MethodInfo.GetCurrentMethod().ToString() + "-" + Error.Message);
- }
- }
- }
- }
As shown, only two methods are invoked to manage the selection of the class base.
The caller (WEB-API) refers to the BLL. Create the object of the ManagingUser class, setting the type connection and the connection string.
- ManagingUser objManagingUser = new ManagingUser((BaseManage.TypeConnection)objRegistry.settingconnection.typeConnectionBLL, objRegistry.settingconnection.strConnection);
In the case of Selection, you invoke the method SelectUser from the object ManagingUser setting the execution of a stored or query and the type of data returned.
In the case of Insert/Update/Delete, you invoke the method ManageUser from the object. ManagingUser affects the passing object and type of action to be performed.
- public class UserApiController: ApiController {
- [HttpPost]
- [ActionName("MenageUser")]
- public int MenageUser(Registry objRegistry) {
- int ret = 0;
- ManagingUser objManagingUser = new ManagingUser((BaseManage.TypeConnection) objRegistry.settingconnection.typeConnectionBLL, objRegistry.settingconnection.strConnection);
- ret = objManagingUser.ManageUser(objRegistry, (BaseManage.ActionOnDB) objRegistry.settingconnection.actionOnDB);
- objManagingUser = null;
- return ret;
- }
- [HttpPost]
- [ActionName("SelectUser")]
- public string SelectUser(SettingConnection obj) {
- ManagingUser objManagingUser = new ManagingUser((BaseManage.TypeConnection) obj.typeConnectionBLL, obj.strConnection);
- var res = objManagingUser.SelectUser(null, (BaseManage.ActionOnDB) obj.actionOnDB, (BaseManage.TypeResult) obj.typeResult);
- objManagingUser = null;
- return res.ToString();
- }
- }
API is recalled from FrontEnd.
In the case of selection, the settings for connection to the API are set using the properties of the SettingConnection object.
- public void LoadSQL() {
- try {
- SettingConnection objSettingConnection = new SettingConnection();
- objSettingConnection.strConnection = strSQL.Trim();
- objSettingConnection.typeConnectionBLL = SettingConnection.TypeConnection.SQLServer;
- objSettingConnection.actionOnDB = SettingConnection.ActionOnDB.storeSelect;
- objSettingConnection.typeResult = SettingConnection.TypeResult.SERIALIZED;
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////call api login
- dynamic data = Utility.CallAPI(uri, "UserApi/SelectUser", objSettingConnection);
- objSettingConnection = null;
- DataSet dataSet = JsonConvert.DeserializeObject < DataSet > (data.ToString());
- dgwSQL.DataSource = dataSet.Tables[0];
- Reload();
- objSettingConnection = null;
- } catch (Exception Error) {
- throw new Exception("Error on the procedure : " + System.Reflection.MethodInfo.GetCurrentMethod().ToString() + "-" + Error.Message);
- }
- }
In the case of Insert/Update/Delete you set the connection settings to the API and pass the object (Registry).
- private void btnSQL_Click(object sender, EventArgs e) {
- try {
- string API = string.Empty;
- SettingConnection objSettingConnection = new SettingConnection();
- objSettingConnection.strConnection = strSQL.Trim();
- objSettingConnection.typeConnectionBLL = SettingConnection.TypeConnection.SQLServer;
- Registry objRegistry = new Registry();
- API = "UserAPI/MenageUser";
- objRegistry.Name = txtname.Text;
- objRegistry.Surname = txtsurname.Text;
- objRegistry.Address = txtaddress.Text;
- objRegistry.Eta = Convert.ToInt16(txtEta.Text.Trim());
- objRegistry.Data = DateTime.Now;
- if (txtPKSQL.Text != "") {
- objRegistry.PK_Registry = txtPKSQL.Text;
- objSettingConnection.actionOnDB = SettingConnection.ActionOnDB.storeUpdate;
- } else {
- API = "UserAPI/MenageUser";
- objSettingConnection.actionOnDB = SettingConnection.ActionOnDB.storeInsert;
- }
- objRegistry.settingconnection = objSettingConnection;
- dynamic data = Utility.CallAPI(uri, API, objRegistry);
- LoadSQL();
- objSettingConnection = null;
- objRegistry = null;
- } catch (Exception ex) {
- string ms = ex.Message;
- }
- }
An example windows application is released, but nothing prevents you from using the DLL with Web App, WCF, WebAPI.
Download DLL, example Project, Script Create DB (SQL, MySQL):
http://samotech.altervista.org/utility.html
Summary
In this article, we learned about the DataAccessLayer API in C#.
Sourav Kumar DasPosted Nov 19, 2019, 10:48 PM
Nice useful article for basics.