I have seen many data models in applications, saying that they all are data
models. But the best one that I have seen is the N-Tier data model it is fast, easy
to maintain and extremely feasible it also works very well with the current .Net
framework. The layer names are not 100% correct if you compare it to other models but the point stays the same, these name are mostly made up and works fine for me
Here is a graph that explains the data model. The Object Data Source connects and configures the data model with the page. for more information on Object Data Source
Connection Object
The Connection Object in this example the Data Layer Helper class is a central connection class that handles all connections to the data base. I only added two methods and the two will work for most cases. One for data retrieval and one for execution (insert, updates) a third can be added for SQL Scalar to retrieve a single value. also notice that i made use of Stored Procedures for faster data retrieval, it will also be a wise decision to add Transactions since all will be Stored Procedures. normally you will only create one Connection Object class and all other classes will work with this class to connect to the db.
using System; using System.Data; using System.Data.SqlClient; using System.Data.Sql; using System.Configuration; using System.Collections; ////// central point to execute comands to reduce code /// public class DataLayerHelper { ////// used for sql select comands, SqlDataReader used for faster retrieval /// /// Stored Procedure name/// List of Sql Parameters///public static SqlDataReader GetList(string cmdText, SqlParameter[] sqlParams) { using (SqlConnection conn = new SqlConnection( ConfigurationManager.ConnectionStrings["ConnectionString"]. ConnectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand(cmdText, conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddRange(sqlParams); SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); return rdr; } } /// /// used for sql execute comands /// /// /// ///public static int ExecuteNonQuery(string cmdText, ref SqlParameter[] sqlParams) { using (SqlConnection conn = new SqlConnection( ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand(cmdText, conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddRange(sqlParams); return cmd.ExecuteNonQuery(); } } }
Data Object
The Data Object Class is primarily used to get and set queries and parameter to send to Connection Object class for execution. normally you will have one Data Object class for a table or a set of tables. instead of using dataTable to load data in I make use of a object List this has many advantages and works great with Linq
using System; using System.Data; using System.Data.SqlClient; using System.Data.Sql; using System.Configuration; using System.Collections; using System.Collections.Generic; public class BookDB { public static List<Book> GetBookList(int BookId, String BookName, Boolean BookActive) { List<Book> itempList = null; Book item = null; SqlParameter[] sqlParams = new SqlParameter[] { new SqlParameter("@BookId", SqlDbType.Int), new SqlParameter("@BookName", SqlDbType.VarChar), new SqlParameter("@BookActive", SqlDbType.Bit) }; sqlParams[0].Value = BookId; sqlParams[1].Value = BookName; sqlParams[2].Value = BookActive; SqlDataReader rdr = DataLayerHelper.GetList("BookGet", sqlParams); if (rdr.HasRows) { itempList = new List<Book>(); while(rdr.Read()) { item = new Book(); item.pBookId = Convert.ToInt32(rdr["BookId"]); item.pBookName = Convert.ToString(rdr["BookName"]); item.pBookPrice = Convert.ToDecimal(rdr["BookPrice"]); item.pBookDesc = Convert.ToString(rdr["BookDesc"]); item.pBookActive = Convert.ToBoolean(rdr["BookActive"]); itempList.Add(item); } } return itempList; } public static int Insert(Book item) { SqlParameter[] sqlParams = new SqlParameter[] { new SqlParameter("@BookName", SqlDbType.VarChar), new SqlParameter("@BookPrice", SqlDbType.Money), new SqlParameter("@BookDesc", SqlDbType.VarChar), new SqlParameter("@BookImage", SqlDbType.Image) }; sqlParams[0].Value = item.pBookName; sqlParams[1].Value = item.pBookPrice; sqlParams[2].Value = item.pBookDesc; sqlParams[3].Value = item.pBookImage; return DataLayerHelper.ExecuteNonQuery("BookInsert", ref sqlParams); } }
Property Class
used by Data Object to populate the data lists
public class Book { private int BookId; private String BookName; private Decimal BookPrice; private String BookDesc; private Boolean BookActive; private System.Drawing.Image BookImage; public int pBookId { get { return BookId; } set { BookId = value; } } public String pBookName { get { return BookName; } set { BookName = value; } } public Decimal pBookPrice { get { return BookPrice; } set { BookPrice = value; } } public String pBookDesc { get { return BookDesc; } set { BookDesc = value; } } public Boolean pBookActive { get { return BookActive; } set { BookActive = value; } } public System.Drawing.Image pBookImage { get { return BookImage; } set { BookImage = value; } } }
Business Object
The last object connects the other objects with the front end and all the business logic is used in here. also notice that there is a [DataObject(true)] Declaration above the class declaration this is just to let the framework know that this is an Data Object class and will make it easy to work with the Object Data Source
using System; using System.ComponentModel; using System.Collections.Generic; using System.Collections; [DataObject(true)] public class BookManager { [DataObjectMethod(DataObjectMethodType.Select, true)] public List<Book< GetBookList() { int BookId = 0; String BookName = ""; Boolean BookActive = true; return BookDB.GetBookList(BookId, BookName, BookActive); } [DataObjectMethod(DataObjectMethodType.Select, true)] public List<Book< GetBookList(int BookId, string BookName, Boolean BookActive) { BookName = (BookName == null) ? "" : BookName; return BookDB.GetBookList(BookId, BookName, BookActive); } [DataObjectMethod(DataObjectMethodType.Insert, true)] public int Insert(Book item) { return BookDB.Insert(item); } [DataObjectMethod(DataObjectMethodType.Update, true)] public int Update(Book item) { return BookDB.Update(item); } }
On the next post i will explain how this works with the front end.
Source Code
No comments:
Post a Comment