MongoServices 0.2.0
A Service/Repository Framework for mongoDB
|
00001 using System; 00002 using System.Collections.Generic; 00003 using System.Linq; 00004 using System.Text; 00005 using MongoDB.Driver; 00006 using System.Configuration; 00007 00008 namespace MongoServices 00009 { 00014 public abstract class MongoServicesRepository<T> : IMongoServicesRepository<T> 00015 where T : MongoServicesModel 00016 { 00017 #region Private Fields 00018 private MongoCollection<T> _collection; 00019 #endregion 00020 00021 #region Protected Properties 00022 00023 00024 00025 protected virtual string CollectionName 00026 { 00027 get 00028 { 00029 var attr = typeof(T).GetCustomAttribute<MongoCollectionNameAttribute>(true); 00030 00031 if (attr == null) 00032 throw new MongoCollectionNameMissingException(typeof(T)); 00033 00034 return attr.CollectionName; 00035 } 00036 } 00037 00041 protected MongoCollection<T> Collection 00042 { 00043 get 00044 { 00045 if (_collection == null) 00046 { 00047 _collection = Database.GetCollection<T>(CollectionName); 00048 } 00049 00050 return _collection; 00051 } 00052 } 00053 #endregion 00054 00055 #region Internal Properties 00056 internal static GlobalConfigContainer GlobalConfig 00057 { 00058 get 00059 { 00060 return GlobalConfigContainer.Instance; 00061 } 00062 } 00063 00064 internal static RepositoryConfigContainer<MongoServicesRepository<T>, T> RepositoryConfig 00065 { 00066 get 00067 { 00068 return RepositoryConfigContainer<MongoServicesRepository<T>, T>.Instance; 00069 } 00070 } 00071 #endregion 00072 00073 #region Private Properties 00074 00075 00076 00077 private MongoDatabase Database 00078 { 00079 get 00080 { 00081 var mongoUrlProvider = RepositoryConfig.MongoUrlProvider == null ? GlobalConfig.MongoUrlProvider : RepositoryConfig.MongoUrlProvider; 00082 00083 return MongoDatabase.Create(mongoUrlProvider()); 00084 } 00085 } 00086 #endregion 00087 00088 #region Static Methods 00089 00090 00091 00092 00093 public static void SetMongoUrlProvider(Func<MongoUrl> provider) 00094 { 00095 RepositoryConfig.MongoUrlProvider = provider; 00096 } 00097 #endregion 00098 } 00099 }