c#開發Mongo筆記第四篇

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   color   ar   os   sp   

今天是個偉大的日子,不得不說小蘋果的歌詞真是深入人心啊。

不過今天偉大並不是因為我種下一顆種子,而是我從今天不再寫demo,而是進入項目的正式開發當中,畢竟項目時間有限(想必各位碼農也都深有體會吧),邊開發邊探索吧。

既然是剛剛開始,就先搭了一下系統架構,然後,做了一個登陸功能,和一個登陸日誌功能

public  class Log    {        public ObjectId Id { get; set; }        public string Abstract { get; set; }        public string Description { get; set; }        public DateTime CreateTime { get; set; }        public string IPAddress { get; set; }        public string OS { get; set; }        public string BrowserVersion { get; set; }        public string UserHostName { get; set; }        public string RequestUrl { get; set; }        public User User { get; set; }    }

經過考慮,我覺得把使用者物件當初冗餘存在日誌表中,當然了使用者表還是存在的,只不過是查詢日誌的時候避免聯查帶來的不便,儲存的結構式這樣的。

 public  class User    {        public ObjectId _id { get; set; }        public string UserName { get; set; }        public string Password { get; set; }        public Guid CustomID { get; set; }    }

 

其實這個id寫成_id而沒有遵循駝峰命名法也是很不規範的,之所以這麼寫,是因為查詢的時候要轉換成對象的話必須完全符合,不然的話會報錯,所以不得不寫了這麼一個奇怪的屬性。

要是大家有什麼號的建議還希望能夠多多指教。

然後就是使用者的登陸功能了,使用者我打算寫一個資料訪問層,目前先寫了兩個方法

 public class DALUser    {        public static User GetById(ObjectId id)        {            MongoDatabase db = MongoHelper.GetConnection();            MongoCollection collection = db.GetCollection<User>("User");            var list = collection.FindAllAs<User>();            return list.FirstOrDefault(u => u._id== id);        }        public static User GetByName(string name)        {            MongoDatabase db = MongoHelper.GetConnection();            MongoCollection collection = db.GetCollection<User>("User");            var list = collection.FindAllAs<User>();            return list.FirstOrDefault(u => u.UserName == name);        }    }

登陸的時候就直接先根據名稱查出這個使用者物件再比較密碼,其實和以前訪問sqlserver一樣了

 

日誌的儲存我用了一個通用的方法

   public static void Add(object o)        {            MongoDatabase db = GetConnection();            Type type = o.GetType();            MongoCollection collection = db.GetCollection<Type>(type.Name);            collection.Insert(o);        }

所以的Object Storage Service都調用這個函數,其實刪,改,查本來也想寫通用的函數的,但是沒想到好的寫法呢,就先分開寫,以後再探索吧。

總之,c#操作mongo之旅今天是正式開始了

c#開發Mongo筆記第四篇

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.