Advertisement
Guest User

Untitled

a guest
May 26th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class MongoDBManager
  2.     {
  3.         Mongo driver = new Mongo();
  4.  
  5.         MongoDB.Driver.Database currentDatabase;
  6.  
  7.         public MongoDBManager(string database)
  8.         {
  9.             driver = new Mongo();
  10.             driver.Connect();
  11.             currentDatabase = driver[database];
  12.         }
  13.  
  14.         public T FindOne<T>(T specs) where T : IMongoPersistableObject
  15.         {
  16.             IMongoCollection c = GetCollection<T>();
  17.             Document spec = ConvertSpecsToDocument(specs);
  18.             Document result = c.FindOne(spec);
  19.             if (result != null)
  20.                 return ConvertFromDocument<T>(result);
  21.             return default(T);
  22.         }
  23.  
  24.         public IEnumerable<T> Find<T>(T specs) where T : IMongoPersistableObject
  25.         {
  26.             IMongoCollection c = GetCollection<T>();
  27.             Document specDocument = ConvertSpecsToDocument(specs);
  28.             ICursor results = c.Find(specDocument);
  29.             foreach (Document document in results.Documents)
  30.             {
  31.                 yield return ConvertFromDocument<T>(document);
  32.             }
  33.         }
  34.  
  35.         private T ConvertFromDocument<T>(Document result) where T : IMongoPersistableObject
  36.         {
  37.             T o = Activator.CreateInstance<T>();
  38.             foreach (var field in typeof(T).GetFields())
  39.             {
  40.                 object val;
  41.                 if ((val = result[field.Name]) != null)
  42.                     field.SetValue(o, val);
  43.             }
  44.             IMongoPersistableObject mpo = (IMongoPersistableObject)o;
  45.             mpo.DatabaseId = (Oid)result["_id"];
  46.             return o;
  47.         }
  48.  
  49.  
  50.         private Document ConvertToDocument<T>(T o)
  51.         {
  52.             Document doc = new Document();
  53.             foreach (var field in typeof(T).GetFields())
  54.             {
  55.                 doc.Add(field.Name, field.GetValue(o));
  56.             }
  57.             return doc;
  58.         }
  59.  
  60.         private Document ConvertSpecsToDocument<T>(T specs)
  61.         {
  62.             Document doc = new Document();
  63.             foreach (var field in typeof(T).GetFields())
  64.             {
  65.                 object value = field.GetValue(specs);
  66.                 if (value != null)
  67.                     doc.Add(field.Name, value.ToString());
  68.             }
  69.             return doc;
  70.         }
  71.  
  72.         private IMongoCollection GetCollection<T>()
  73.         {
  74.             return currentDatabase.GetCollection(typeof(T).FullName);
  75.         }
  76.  
  77.         public T Persist<T>(T o) where T : IMongoPersistableObject
  78.         {
  79.             IMongoPersistableObject mpo = (IMongoPersistableObject)o;
  80.             if (mpo.DatabaseId == null)
  81.             {
  82.                 return Insert<T>(o);
  83.             }
  84.             else
  85.             {
  86.                 return Update<T>(o);
  87.             }
  88.         }
  89.  
  90.         private T Insert<T>(T o) where T : IMongoPersistableObject
  91.         {
  92.             IMongoCollection table = GetCollection<T>();
  93.             table.Insert(ConvertToDocument(o));
  94.             return o;
  95.         }
  96.  
  97.         private T Update<T>(T o) where T : IMongoPersistableObject
  98.         {
  99.             IMongoCollection table = GetCollection<T>();
  100.             Document d = ConvertToDocument<T>(o);
  101.             IMongoPersistableObject mpo = (IMongoPersistableObject)o;
  102.             d["_id"] = mpo.DatabaseId;
  103.             table.Update(d);
  104.             return o;
  105.         }
  106.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement