標籤: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筆記第四篇