2013年9月29日 星期日

MVVM 概觀

參考:
1.[Architecture] MVP, MVC, MVVM, 傻傻分不清楚~
MVVM 的架構一樣是 M, V 分離,但中間是以 VM (ViewModel) 來串接,這個 ViewModel 比較像是 View 的一個代理程式,它負責直接對 Model 做溝通,而 View 可以透過一些機制 (ex: Events, Two-way Databindings, ...) 來和 ViewModel 溝通以取得資料或將資料拋給 Model 做存取等工作,ViewModel 也可以作為和外部系統的代理程式,例如 Web Service 或是 REST Service 或是 Enterprise Services 等等,不過它和 MVC 不同的地方,就是 ViewModel 和 View 的黏合度比較高,因為 View 必須要透過 ViewModel 才可以取得 Model,而 ViewModel 又必須要處理來自 View 的通知訊息,所以雖然職責一樣分明,但是卻不像 MVC 那樣可以擴展到整個系統元件都能用。

ViewModel 最好是可以進行單元測試,這樣會更佳。

以下有幾個圖可以解釋MVVM架構:












2013年5月30日 星期四

C# HttpListener 範例

建立Console專案.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpListener listener = new HttpListener();
            try
            {
                listener.Prefixes.Add("http://*:8055/"); //要監聽的URL範圍
                listener.Start(); //開始監聽端口,接收客户端請求
                Console.WriteLine("Listening");
                while (true)
                {
                    //獲取一個客户端請求為止
                    HttpListenerContext context = listener.GetContext();
                    //將其處理過程放入線程池(Pool)
                    System.Threading.ThreadPool.QueueUserWorkItem(ProcessHttpClient, context);
                }
            }
            catch (Exception e)
            {

                Console.WriteLine(e.Message);
            }
            finally
            {
                listener.Stop(); //關閉HttpListener
            }
        }

        //客户請求處理
        static void ProcessHttpClient(object obj)
        {
            HttpListenerContext context = obj as HttpListenerContext;
            HttpListenerRequest request = context.Request;
            HttpListenerResponse response = context.Response;
            //string s = request.QueryString["command"];
            //do something as you want
            string responseString = string.Format(" {0}", DateTime.Now);
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
            response.ContentLength64 = buffer.Length;
            System.IO.Stream output = response.OutputStream;
            output.Write(buffer, 0, buffer.Length);

            //關閉輸出流,釋放相應資源。
            output.Close();
        }
    }
}




2013年5月15日 星期三

Ext.Net 學習第一步


資料來源:http://extnet.tumblr.com/post/30985419252/ext-net-ext-net



安裝步驟 & 開始使用 如下:
1.安裝Visual Studio 2005,2008 or 2012 或
   Visual Web Developer Express 2005,2008 or 2010
如果未安裝的,可以裝Visual Web Developer Express 2010,是免費的,且使用上跟Visual Studio沒什麼差異。。
2.安裝.Net Framework 3.5以上版本
3.下載元件檔,可以至http://www.ext.net/download/下載你所要的版本
↓將下載回來的檔案解開來,我習慣開個asp.net 的資料夾放這些元件,待回兒會使用到
4.開啟專案如附圖
檔案→新網站→選擇ASP.NET空網站(空網站比較乾淨,不會有一些雜七雜八的…)
選擇的語言Visual C#(因為Ext.Net都是用C#寫的,所以選C#會比較好,Ext.Net的範例也都是C#,比較沒有轉換的問題)
當然,您要用vb也是可以使用的,但網路上的支援大部份都是C#…
↓加一個頁面進來
↓語言一樣建議是C#,選擇Web Form
這裡我不勾選將程式碼置於各別檔案中,為什麼?
主要是日後要在Ext.Net的討論區發問會比較方便,檔案結構也會比較簡潔….(不會拆成兩個檔.aspx及.cs)
5.將Ext.Net元件安裝到工具箱(只需安裝一次,日後新增的專案就不需要做這個動作了)
↑標籤命名為Ext.Net
↓接下來在Ext.Net按右鍵→選擇項目→.Net Framework元件→點瀏覽→
 選擇剛才解壓縮後放至本機的Ext.Net.dll檔→開啟舊檔
↓預設會勾選Ext.Net的全部元件,直接按”確定”,就ok了
↓元件已經加進來了
6.開始我們第一個Ext.Net的頁面吧
要使用Ext.Net,第一件事,就是加上ResourceManager,不然會有err
↓將元件ResourceManager拉至你的頁面
當你拉好後,你會發現你的方案總管就會自動出現bin目錄,跟一些dll檔
↓接下來測試拉個Window並Run看看吧~
第一次會出現是不是要出現除錯訊息,因為還沒有要放到server上,就按確定就可以了
↓Run的時侯會出現Web.config要設定的訊息
↓我們把這一段設定加上,再重新Run一次看看吧
↓很感動的,我們完成第一隻Ext.Net的程式了
是不是很沒Feel?
你們可以到http://examples.ext.net/找Code,貼到剛才的.aspx上玩看看喔
The End…

2013年3月25日 星期一

[C#] 刪除1天前某個目錄的檔案

            //刪除1天前某個目錄的檔案,減少temp檔佔用空間
            try
            {
                string TmpRootPath= @"C:\temp";
                DirectoryInfo TempInfo = new DirectoryInfo(TmpRootPath);
                FileInfo[] TempArray = TempInfo.GetFiles();
                if (TempArray.Length > 0)
                {
                    for (int i = 0; i < TempArray.Length; i++)
                    {
                        string TempFileName = TempArray[i].Name;
                        DateTime dtTempFileEditTime = TempArray[i].LastWriteTime;
                        TimeSpan ts = DateTime.Now - dtTempFileEditTime;
                        int differenceInDays = ts.Days;
                        if (differenceInDays > 1) File.Delete(TmpRootPath+ TempFileName);
                    }
                }