asp.net開發(fā)微信公眾平臺(2)多層架構(gòu)框架搭建和入口實(shí)現(xiàn)
Feb 23, 2017 pm 02:03 PM? ? ?上篇已經(jīng)設(shè)計出比較完善的數(shù)據(jù)庫了,這篇開始進(jìn)入代碼。 ?首先把上篇設(shè)計的數(shù)據(jù)庫腳本在數(shù)據(jù)庫中執(zhí)行下,生成數(shù)據(jù)庫,然后在VS中建立項(xiàng)目,為了方便理解和查看,我設(shè)計的都是很直白的類名和文件名,沒有命名空間前綴。
? ? ?采用接口方式,共8個項(xiàng)目:7個類庫和一個MVC項(xiàng)目, ?分別為: ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?顯示層——MVC項(xiàng)目
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?業(yè)務(wù)邏輯層——訪問接口IBLL、具體實(shí)現(xiàn)BLL
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?數(shù)據(jù)訪問層——訪問接口IDAL、具體實(shí)現(xiàn)DAL
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?數(shù)據(jù)(模型)——DataModel
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?通用方法——Common
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?倉儲——Factory
這里的倉儲并不為了生產(chǎn)業(yè)務(wù)邏輯層和數(shù)據(jù)訪問層的接口,而是為了存放EntityFramework上下文對象和一些緩存管理,業(yè)務(wù)邏輯層和數(shù)據(jù)訪問層的接口生產(chǎn)(實(shí)現(xiàn))工作我會交給Spring.NET注入實(shí)現(xiàn)。 框架搭建好之后如下:
?
? ? 框架搭建好了,接下去把數(shù)據(jù)庫添加進(jìn)來,在DAL中(注意是DAL不是datamodel)添加新項(xiàng),選擇數(shù)據(jù)--ADO.NET實(shí)體數(shù)據(jù)模型:
?
取個名字,就叫WeixinModel吧, 選擇從數(shù)據(jù)庫生成,配置一下數(shù)據(jù)庫連接到之前生成的數(shù)據(jù)庫,一路下一步,最后加載到edmx, 在edmx上右鍵--添加代碼生成項(xiàng),選擇代碼:
選DbContext Generator, ?然后保存一下edmx, 之后把edmx和weixinmodel.tt復(fù)制到DataModel,刪除DAL中的edmx和weixinmodel.tt, 在datamodel中打開weixinmodel.tt保存一下即可, 另外需要在DAL中保留的WeiXinModel.Context.cs中聲明datamodel命名空間。 ?
? ? 框架和數(shù)據(jù)模型都有了,接下去在DAL、IDAL、BLL、IBLL中按照正確的引用層次添加引用,并寫幾個常用方法,就可以開始在顯示層中使用了,
這里舉例在DAL中寫添刪改查方法:
//添加 public T AddEntity<T>(DbContext db,T entity) where T : class { db.Entry<T>(entity).State = EntityState.Added; db.SaveChanges(); return entity; } //修改 public bool UpdateEntity<T>(DbContext db,T entity) where T : class { db.Set<T>().Attach(entity); db.Entry<T>(entity).State = EntityState.Modified; db.SaveChanges(); return true; } //刪除 public bool DeleteEntity<T>(DbContext db,T entity) where T : class { db.Set<T>().Attach(entity); db.Entry<T>(entity).State = EntityState.Deleted; db.SaveChanges(); return true; } // 返回一個對象 public T InfoEntities<T>(DbContext db, Expression<Func<T, bool>> whereLambda) where T : class { return db.Set<T>().Where<T>(whereLambda).FirstOrDefault(); }
對應(yīng)的把接口、業(yè)務(wù)邏輯層都寫上。
現(xiàn)在來到顯示層,默認(rèn)的MVC項(xiàng)目是返回VIEW, 這里我們不需要返回頁面, 把home中的index改成Void返回類型, 接下去就是接收微信發(fā)來的請求進(jìn)行判斷了,驗(yàn)證請求----接收POST數(shù)據(jù)---分析XML----解析成自己想要的數(shù)據(jù)
入口:首先驗(yàn)證消息來源是微信服務(wù)器,然后解析收到的xml,解析成功有數(shù)據(jù)則執(zhí)行LookMsgType方法來進(jìn)行處理
private IBLL.IDoWei BLLWei { set; get; } public DbContext dbHome { get; set; } private string token { get; set; } Dictionary<string, string> xmlModel = new Dictionary<string, string>(); public void Index() { dbHome=FContext.WeiXinDbContext(); //xml字符串 string xmlData = string.Empty; //請求類型 string method=Request.HttpMethod.ToLower(); string signature = Request.QueryString["signature"]; string timestamp = Request.QueryString["timestamp"]; string nonce = Request.QueryString["nonce"]; //驗(yàn)證接入和每次請求驗(yàn)證真實(shí)性 if (method == "get") { if (CheckSign(signature,timestamp,nonce)) { Often.ResponseToEnd(Request.QueryString["echostr"]); } else { Response.Status = "403"; Often.ResponseToEnd(""); } } //處理接收到的POST消息 else if (method == "post") { using (Stream stream = Request.InputStream) { Byte[] byteData = new Byte[stream.Length]; stream.Read(byteData, 0, (Int32)stream.Length); xmlData = Encoding.UTF8.GetString(byteData); } if (!string.IsNullOrEmpty(xmlData)) { try { xmlModel = ReadXml.GetXmlModel(xmlData); } catch { //未能正確處理 給微信服務(wù)器回復(fù)默認(rèn)值 Often.ResponseToEnd(""); } } if (xmlModel.Count > 0) { string msgType = ReadXml.ReadModel("MsgType", xmlModel); LookMsgType(msgType); } } else//除了post和get外 如head皆視為非法請求 { Response.Status = "403"; Often.ResponseToEnd(""); } dbHome.Dispose(); }
這里用到的驗(yàn)證方法:
/// <summary> /// 驗(yàn)證簽名 /// </summary> /// <param name="signature"></param> /// <param name="timestamp"></param> /// <param name="nonce"></param> /// <returns></returns> public bool CheckSign(string signature, string timestamp, string nonce) { List<string> list = new List<string>(); list.Add(token); list.Add(timestamp); list.Add(nonce); //默認(rèn)排序 list.Sort(); string tmpStr = string.Empty; list.All(l => { tmpStr += l; return true; }); tmpStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1"); //驗(yàn)證 if (tmpStr == signature) { return true; } return false; }
倉儲中的EF上下文:
public static DbContext WeiXinDbContext() { DbContext dbcontext =new WeiXinEntities(); //創(chuàng)建 dbcontext.Configuration.AutoDetectChangesEnabled = false;//自動檢測配置更改 dbcontext.Configuration.LazyLoadingEnabled = true;//延遲加載 dbcontext.Configuration.ValidateOnSaveEnabled = false;//自動跟蹤 return dbcontext; }
Common中的解析微信發(fā)來的XML方法
//把接收到的XML轉(zhuǎn)為字典 public static Dictionary<string, string> GetXmlModel(string xmlStr) { XmlDocument doc = new XmlDocument(); doc.LoadXml(xmlStr); Dictionary<string, string> mo = new Dictionary<string, string>(); var data = doc.DocumentElement.ChildNodes; for (int i = 0; i < data.Count; i++) { mo.Add(data.Item(i).LocalName, data.Item(i).InnerText); } return mo; } ////從字典中讀取指定的值 public static string ReadModel(string key, Dictionary<string, string> model) { string str = ""; model.TryGetValue(key, out str); if (str== null) str = ""; return str; }
好了,入口以及驗(yàn)證相關(guān)的都解決了,下一篇開始微信消息處理LookMsgType方法實(shí)現(xiàn)
更多asp.net開發(fā)微信公眾平臺(2)多層架構(gòu)框架搭建和入口實(shí)現(xiàn)相關(guān)文章請關(guān)注PHP中文網(wǎng)!

Alat AI Hot

Undress AI Tool
Gambar buka pakaian secara percuma

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Clothoff.io
Penyingkiran pakaian AI

Video Face Swap
Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Artikel Panas

Alat panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)