Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using Fishidy.Domain.Attributes;
- using Fishidy.Domain.Models.Base;
- using MongoDB.Bson.Serialization;
- using MongoDB.Bson.Serialization.Conventions;
- using MongoDB.Driver;
- namespace Fishidy.Domain.Infrastructure.Mongo
- {
- public static class FishidyMongo
- {
- #region Private Properties
- private static MongoDatabase Database
- {
- get
- {
- return MongoDatabase.Create(ConfigurationManager.ConnectionStrings["FishidyMongoDatabase"].ConnectionString);
- }
- }
- #endregion
- #region Public Methods
- public static MongoCollection<T> GetCollection<T>()
- where T : FishidyCollectionModel
- {
- var collectionAttribute = Attribute.GetCustomAttribute(typeof(T), typeof(MongoCollectionAttribute));
- return Database.GetCollection<T>(collectionAttribute == null ? typeof(T).Name : ((MongoCollectionAttribute) collectionAttribute).CollectionName);
- }
- public static MongoCollection GetCollection(Type modelType)
- {
- var collectionAttribute = Attribute.GetCustomAttribute(modelType, typeof(MongoCollectionAttribute));
- return Database.GetCollection(modelType, (collectionAttribute == null ? modelType.Name : ((MongoCollectionAttribute)collectionAttribute).CollectionName));
- }
- public static void Initialize()
- {
- SetConventionProfile();
- }
- #endregion
- #region Private Methods
- private static void SetConventionProfile()
- {
- var typesToProcess = new List<Type>();
- var convention = new ConventionProfile().SetMemberFinderConvention(new FishidyMongoMemberFinderConvention());
- foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
- {
- typesToProcess.AddRange(from t in assembly.GetTypes()
- where (t.IsSubclassOf(typeof(FishidyModel)) || t == typeof(FishidyModel))
- && t.IsClass
- select t);
- }
- typesToProcess.ForEach(type => BsonClassMap.RegisterConventions(convention, t => t == type));
- }
- #endregion
- }
- }
Add Comment
Please, Sign In to add comment