/// <summary> /// Creates a new instance or returns an existing instance of MongoDatabase. Only one instance /// is created for each combination of database settings. Automatically creates an instance /// of MongoServer if needed. /// </summary> /// <param name="url">Server and database settings in the form of a MongoUrl.</param> /// <returns> /// A new or existing instance of MongoDatabase. /// </returns> public static MongoDatabase Create(MongoUrl url) { var serverSettings = url.ToServerSettings(); var databaseName = url.DatabaseName; return(Create(serverSettings, databaseName)); }
public static IContainer Initialize() { ObjectFactory.Initialize(x => { x.Scan(scan => { scan.TheCallingAssembly(); scan.WithDefaultConventions(); }); x.For<IFilterProvider>().Use<SmFilterProvider>(); x.For<IFlexMembershipProvider>().HybridHttpOrThreadLocalScoped().Use<FlexMembershipProvider>(); x.For<IFlexRoleProvider>().HybridHttpOrThreadLocalScoped().Use<FlexRoleProvider>(); x.For<IFlexUserStore>().HybridHttpOrThreadLocalScoped().Use<FlexMembershipUserStore<User, Role>>(); x.For<IFlexRoleStore>().HybridHttpOrThreadLocalScoped().Use<FlexMembershipUserStore<User, Role>>(); x.SetAllProperties(p => p.OfType<IFlexRoleProvider>()); x.Forward<IFlexMembershipProvider, IFlexOAuthProvider>(); x.For<IApplicationEnvironment>().Singleton().Use<AspnetEnvironment>(); x.For<ISecurityEncoder>().Singleton().Use<DefaultSecurityEncoder>(); x.For<MongoDatabase>().Singleton().Use(c => { var connectionString = ConfigurationManager.ConnectionStrings["MongoConnectionString"].ConnectionString; if (String.IsNullOrEmpty(connectionString)) throw new ConfigurationErrorsException("MongoConnectionString was not found in the App/Web.config."); var url = new MongoUrl(connectionString); var server = MongoServer.Create(url.ToServerSettings()); var database = server.GetDatabase(url.DatabaseName); BsonClassMap.RegisterClassMap<User>(ctx => { ctx.SetIgnoreExtraElements(true); ctx.AutoMap(); ctx.SetIdMember(ctx.GetMemberMap(u => u.Id).SetRepresentation(BsonType.ObjectId)); }); BsonClassMap.RegisterClassMap<Role>(ctx => { ctx.SetIgnoreExtraElements(true); ctx.AutoMap(); ctx.SetIdMember(ctx.GetMemberMap(r => r.Id).SetRepresentation(BsonType.ObjectId)); }); var userCollection = database.GetCollection<User>("user"); var roleCollection = database.GetCollection<User>("role"); userCollection.EnsureIndex(IndexKeys<User>.Ascending(u => u.Username), IndexOptions.SetUnique(true)); userCollection.EnsureIndex(IndexKeys.Ascending("OAuthAccounts.Provider", "OAuthAccounts.ProviderUserId"), IndexOptions.SetUnique(true)); roleCollection.EnsureIndex(IndexKeys<Role>.Ascending(r => r.Name), IndexOptions.SetUnique(true)); roleCollection.EnsureIndex(IndexKeys<Role>.Ascending(r => r.Users)); return database; }); x.For<MongoCollection<User>>().Use(c => c.GetInstance<MongoDatabase>().GetCollection<User>("user")); x.For<MongoCollection<Role>>().Use(c => c.GetInstance<MongoDatabase>().GetCollection<Role>("role")); }); return ObjectFactory.Container; }
public void DeleteAllQuestions() { // Connecting the MongoDb and deleting its entire collections state var cnn = new MongoUrl(ConfigurationHelper.GetCurrentEnvDbConnString()); var server = MongoServer.Create(cnn.ToServerSettings()); MongoDatabase db = server.GetDatabase(cnn.DatabaseName); // deleting questions MongoCollection questionsCollection = db.GetCollection(typeof(Question).Name); questionsCollection.RemoveAll(); }
private static void CleanDbBeforeTests() { // Validate that we are not runing on the production DB Assert.AreNotEqual(ConfigurationHelper.GetCurrentEnvDbConnString(), "YOU WERE JUST TRYING TO DELETE THE PRODUCTION DB!!"); // Connecting the MongoDb and deleting its entire collections state var cnn = new MongoUrl(ConfigurationHelper.GetCurrentEnvDbConnString()); var server = MongoServer.Create(cnn.ToServerSettings()); MongoDatabase db = server.GetDatabase(cnn.DatabaseName); // deleting users MongoCollection userCollection = db.GetCollection(typeof(User).Name); var query = Query.NE("Name", "effie"); userCollection.Remove(query); // deleting questions MongoCollection questionsCollection = db.GetCollection(typeof(Question).Name); questionsCollection.RemoveAll(); }
/// <summary> /// Creates a new instance or returns an existing instance of MongoServer. Only one instance /// is created for each combination of server settings. /// </summary> /// <param name="url">Server settings in the form of a MongoUrl.</param> /// <returns> /// A new or existing instance of MongoServer. /// </returns> public static MongoServer Create( MongoUrl url ) { return(Create(url.ToServerSettings())); }
/// <summary> /// Creates and returns a MongoDatabase from the specified url. /// </summary> /// <param name="url">The url of the database</param> private void OpenStore(MongoUrl url) { var server = MongoServer.Create(url.ToServerSettings()); this.store = server.GetDatabase(url.DatabaseName); }