国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁(yè) 微信小程式 微信開(kāi)發(fā) asp.net開(kāi)發(fā)微信公眾平臺(tái)(2)多層架構(gòu)框架建構(gòu)與入口實(shí)現(xiàn)

asp.net開(kāi)發(fā)微信公眾平臺(tái)(2)多層架構(gòu)框架建構(gòu)與入口實(shí)現(xiàn)

Feb 23, 2017 pm 02:03 PM
微信大眾平臺(tái)

? ? ?上篇已經(jīng)設(shè)計(jì)出比較完善的資料庫(kù)了,這篇開(kāi)始進(jìn)入程式碼。 ?先把上篇設(shè)計(jì)的資料庫(kù)腳本在資料庫(kù)中執(zhí)行下,產(chǎn)生資料庫(kù),然後在VS中建立項(xiàng)目,為了方便理解和查看,我設(shè)計(jì)的都是很直白的類(lèi)名和檔名,沒(méi)有命名空間前綴。

? ? ?採(cǎi)用介面方式,共8個(gè)項(xiàng)目:7個(gè)類(lèi)別庫(kù)與一個(gè)MVC項(xiàng)目, ?分別為: ?

? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?顯示器層-MVC項(xiàng)目

##? ? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?事業(yè)層方面-存取介面IBLL、特定實(shí)現(xiàn)BLL

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ?資料存取層-存取介面IDAL、特定實(shí)作DAL

##? ? ? ? ? ? ? ? ? ?與 ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 模型)-DataModel

? ? ? ? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?一般法-Common

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? ? ? ?倉(cāng)儲(chǔ)-Factory

##這裡的倉(cāng)儲(chǔ)並不是為了生產(chǎn)業(yè)務(wù)邏輯層和資料存取層的接口,而是為了存放EntityFramework上下文物件和一些快取管理,業(yè)務(wù)邏輯層和資料存取層的介面生產(chǎn)(實(shí)作)工作我會(huì)交給Spring.NET注入實(shí)作。 框架搭建好之後如下:

?asp.net開(kāi)發(fā)微信公眾平臺(tái)(2)多層架構(gòu)框架建構(gòu)與入口實(shí)現(xiàn)

? ? 框架搭建好了,接著把資料庫(kù)加入進(jìn)來(lái),在DAL中(注意是DAL不是datamodel)添加新項(xiàng),選擇資料--ADO.NET實(shí)體資料模型:

?asp.net開(kāi)發(fā)微信公眾平臺(tái)(2)多層架構(gòu)框架建構(gòu)與入口實(shí)現(xiàn)

取個(gè)名字,就叫WeixinModel吧, 選擇從資料庫(kù)生成,配置一下資料庫(kù)連接到之前產(chǎn)生的資料庫(kù),一路下一步,最後載入到edmx, 在edmx上右鍵--新增程式碼產(chǎn)生項(xiàng),選擇程式碼:

asp.net開(kāi)發(fā)微信公眾平臺(tái)(2)多層架構(gòu)框架建構(gòu)與入口實(shí)現(xiàn)選DbContext Generator, ?然後儲(chǔ)存一下edmx, 之後把edmx和weixinmodel.tt複製到DataModel,刪除DAL中的edmx和weixinmodel.tt, 在datamodel中打開(kāi)weixinmodel.tt保存一下即可, 另外需要在DAL中保留的WeiXinModel.Context.cs中聲明datamodel命名空間。 asp.net開(kāi)發(fā)微信公眾平臺(tái)(2)多層架構(gòu)框架建構(gòu)與入口實(shí)現(xiàn)

? ? 框架和資料模型都有了,接下去在DAL、IDAL、BLL、IBLL中按照正確的引用層次添加引用,並寫(xiě)幾個(gè)常用方法,就可以開(kāi)始在顯示層中使用了,

這裡舉例在DAL中寫(xiě)添刪改查方法:

//添加
        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;

        }




        // 返回一個(gè)對(duì)象
        public T InfoEntities<T>(DbContext db, Expression<Func<T, bool>> whereLambda) where T : class
        {

            return db.Set<T>().Where<T>(whereLambda).FirstOrDefault();

        }

對(duì)應(yīng)的把介面、業(yè)務(wù)邏輯層都寫(xiě)上。

現(xiàn)在來(lái)到顯示層,默認(rèn)的MVC項(xiàng)目是返回VIEW, 這里我們不需要返回頁(yè)面, 把home中的index改成Void返回類(lèi)型, 接下去就是接收微信發(fā)來(lái)的請(qǐng)求進(jìn)行判斷了,驗(yàn)證請(qǐng)求----接收POST數(shù)據(jù)---分析XML----解析成自己想要的數(shù)據(jù)

  入口:首先驗(yàn)證消息來(lái)源是微信服務(wù)器,然后解析收到的xml,解析成功有數(shù)據(jù)則執(zhí)行LookMsgType方法來(lái)進(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;
            //請(qǐng)求類(lèi)型
            string method=Request.HttpMethod.ToLower();
            string signature = Request.QueryString["signature"];
            string timestamp = Request.QueryString["timestamp"];
            string nonce = Request.QueryString["nonce"];
            //驗(yàn)證接入和每次請(qǐng)求驗(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皆視為非法請(qǐng)求
            {
                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;
        }

倉(cāng)儲(chǔ)中的EF上下文:

public static DbContext WeiXinDbContext()
        {
            DbContext dbcontext =new WeiXinEntities();  //創(chuàng)建
            dbcontext.Configuration.AutoDetectChangesEnabled = false;//自動(dòng)檢測(cè)配置更改
            dbcontext.Configuration.LazyLoadingEnabled = true;//延遲加載
            dbcontext.Configuration.ValidateOnSaveEnabled = false;//自動(dòng)跟蹤
            return dbcontext;
        }

Common中的解析微信發(fā)來(lái)的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)的都解決了,下一篇開(kāi)始微信消息處理LookMsgType方法實(shí)現(xiàn)

更多asp.net開(kāi)發(fā)微信公眾平臺(tái)(2)多層架構(gòu)框架建構(gòu)與入口實(shí)現(xiàn)相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)!


本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開(kāi)發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)程式碼編輯軟體(SublimeText3)

熱門(mén)話題

Laravel 教程
1601
29
PHP教程
1502
276