MODE 層 代碼
namespace MODEL{ public class Article { int _ID=0; int _CID=0;//類別ID string _Title="";//標(biāo)題 DateTime _AddDate=DateTime.Now;//添加日期 string _Content="";//內(nèi)容 int _Hits=0;//點(diǎn)擊次數(shù) int _Count=0;//評論次數(shù) public Article() { } //數(shù)據(jù)庫記錄實(shí)例化為 Article,這個地方 從數(shù)據(jù)庫 讀出一條記錄后 可以 直接實(shí)例化 MODEL,不需要 在 一個個去寫 public Article(DataRow dataRow) { DataColumnCollection DCC = dataRow.Table.Columns; if (DCC.Contains("id")) { _ID = DataHelper.Int(dataRow["id"]); } if (DCC.Contains("cid")) { _CID = DataHelper.Int(dataRow["cid"]); } if (DCC.Contains("hits")) { _Hits = DataHelper.Int(dataRow["hits"]); } if (DCC.Contains("count")) { _Count = DataHelper.Int(dataRow["count"]); } if (DCC.Contains("adddate")) { _AddDate = DataHelper.dateTime(dataRow["adddate"]); } if (DCC.Contains("title")) { _Title = DataHelper.String(dataRow["title"] ); } if (DCC.Contains("content")) { _Content =DataHelper.String( dataRow["content"] ); } } //外部接受過來的數(shù)據(jù)實(shí)例化類 使用 hashTable 構(gòu)造 類 public Article(Hashtable hashtable) { if (hashtable.Contains("id")) { _ID = DataHelper.Int(hashtable["id"]); } if (hashtable.Contains("cid")) { _CID = DataHelper.Int(hashtable["cid"]); } if (hashtable.Contains("hits")) { _Hits = DataHelper.Int(hashtable["hits"]); } if (hashtable.Contains("count")) { _Count = DataHelper.Int(hashtable["count"]); } if (hashtable.Contains("adddate")) { _AddDate = DataHelper.dateTime(hashtable["adddate"]); } if (hashtable.Contains("title")) { _Title = DataHelper.String(hashtable["title"] ); } if (hashtable.Contains("content")) { _Content = DataHelper.String(hashtable["content"] ); } } public Article(int id,int cid,string title,DateTime addDate,string content,int hits,int count) { _ID = id; _CID = cid; _Title = title; _AddDate = addDate; _Content = content; _Hits = hits; _Count = count; } public Article(int cid, string title, DateTime addDate, string content, int hits, int count) { _CID = cid; _Title = title; _AddDate = addDate; _Content = content; _Hits = hits; _Count = count; } public Article( int cid, string title, string content) { _CID = cid; _Title = title; _Content = content; } public Article(int id, int cid, string title, string content) { _ID = id; _CID = cid; _Title = title; _Content = content; } public int ID { get { return _ID; } set { _ID = value; } } public int CID { get { return _CID; } set { _CID = value; } } public string Title { get { return _Title; } set { _Title = value; } } public DateTime AddDate { get { return _AddDate; } set { _AddDate = value; } } public string Content { get { return _Content; } set { _Content = value; } } public int Hits { get { return _Hits; } set { _Hits = value; } } public int Count { get { return _Count; } set { _Count = value; } } }}
RequestData 類:代替 Request.Form; 上面用到的,作用是 把?Request.Form;里面的 name value 保存到一個 hashTable 里面
using System;using System.Collections;using System.Collections.Generic;using System.Collections.Specialized;using System.Web;/// <summary>///RequestData 的摘要說明/// </summary>public class RequestData{ public RequestData() { // //TODO: 在此處添加構(gòu)造函數(shù)邏輯 // } public static Hashtable Get() { Hashtable _Hashtable = new Hashtable(); NameValueCollection NVC = HttpContext.Current.Request.Form; for (int i = 0; i < NVC.Count; i++) { _Hashtable.Add(NVC.Keys[i].ToString().ToLower(), NVC[NVC.Keys[i]]); } return _Hashtable; }}
UI:
ArticleAdd.aspx
<html xmlns="http://www.w3.org/1999/xhtml"><head> <title></title></head><body><form action="?action=save" method="post"> 標(biāo)題:<input type="text" value="134" name="Title" /><br /> 內(nèi)容:<input type="text" value="134" name="Content" /><br /> 日期:<input type="text" value="1989 07 18" name="AddDate" /><br /> 類別ID:<input type="text" value="134" name="CID" /><br /> <input type="submit" value="submit" /></form></body></html>
ArticleAdd.aspx.cs
using System;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Collections;using MODEL;public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { Hashtable Has = RequestData.Get(); Article ArticleMOD=new Article(Has); //實(shí)例化 MODEL }}
7 回答

慕森卡
TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個贊
太麻煩了。建議使用下反射吧。
NameValueCollection本來就是KV6的格式的 為什么還要轉(zhuǎn)換成HashTable呢?
直接反射 保持鍵和Model中的屬性值相同的就可以了。

蝴蝶刀刀
TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個贊
這樣 耦合度高的話,會有哪些影響呢?
我能想到的是 當(dāng) 數(shù)據(jù)庫 加字段 刪除字段的時候 可能會 麻煩
還有就是 按照常規(guī)的寫法,數(shù)據(jù)庫 刪除字段 或者添加 字段 基本上 也要改個遍
但是這個 我寫了一個 代碼生成工具 ,根據(jù)數(shù)據(jù)庫 結(jié)構(gòu)生成 的 所以 也不是麻煩
我能想到的是 當(dāng) 數(shù)據(jù)庫 加字段 刪除字段的時候 可能會 麻煩
還有就是 按照常規(guī)的寫法,數(shù)據(jù)庫 刪除字段 或者添加 字段 基本上 也要改個遍
但是這個 我寫了一個 代碼生成工具 ,根據(jù)數(shù)據(jù)庫 結(jié)構(gòu)生成 的 所以 也不是麻煩

眼眸繁星
TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個贊
代碼生成工具固然方便。但是它是在項目開始時使用。到了項目迭代時:你不可能通過代碼生成工具來大批量生成Code。因?yàn)橛行I(yè)務(wù)邏輯是工具無法完成的
- 7 回答
- 0 關(guān)注
- 434 瀏覽
添加回答
舉報
0/150
提交
取消