public MongoDbContext(IOptions <MongoDbConfiguration> configOptions, ILogger <MongoDbContext> logger) { var config = configOptions?.Value ?? throw new ArgumentNullException(nameof(configOptions)); _client = MongoDbFactory.GetClient(config); Database = _client.GetDatabase(config.Database); }
public void SetUp() { var settings = new ResumeDatabaseSettings { ConnectionString = "mongodb://*****:*****@localhost", DatabaseName = "resume", ResumeCollectionName = "profiles" }; var factory = new MongoDbFactory(); sut = new ProfileContext(factory, settings); }
static void Main(string[] args) { /* Aqui vamos instanciar a nossa sql factory */ DbFactory sqlFactory = new SqlServerFactory(); DbConnection sqlConnection = sqlFactory.CreateConnection(); sqlConnection.Open(); DbCommand sqlCommand = sqlFactory.CreateCommand(); sqlCommand.Execute(); /* Aqui vamos instanciar a nossa mongo factory */ DbFactory mongoFactory = new MongoDbFactory(); DbConnection mongoConnection = mongoFactory.CreateConnection(); mongoConnection.Open(); DbCommand mongoCommand = mongoFactory.CreateCommand(); mongoCommand.Execute(); Console.ReadLine(); }
private static ISiloHost ConfigureSilo() { var builder = new ConfigurationBuilder() .AddJsonFile($"appsettings.json", true, true) .AddJsonFile($"appsettings.{EnvironmentName}.json", true, true) .AddEnvironmentVariables(); Configuration = builder.Build(); var siloHostBuilder = new SiloHostBuilder(); BsonClassMap.RegisterClassMap <AccountEvent>(cm => { cm.AutoMap(); cm.SetIsRootClass(true); }); BsonClassMap.RegisterClassMap <AccountNameEvent>(); BsonClassMap.RegisterClassMap <DepositEvent>(); BsonClassMap.RegisterClassMap <NewStakeholderEvent>(); BsonClassMap.RegisterClassMap <NewTransactionEvent>(); BsonClassMap.RegisterClassMap <WithdrawEvent>(); IMongoDatabase database; siloHostBuilder.Configure <ClusterOptions>(options => { options.ClusterId = "dev"; options.ServiceId = "bancor-silohost"; }); switch (EnvironmentName) { case "Integration": siloHostBuilder .UseConsulClustering(options => { options.Address = new Uri("http://consul:8500"); }) .ConfigureEndpoints(siloPort: 11111, gatewayPort: 30000); database = new MongoDbFactory().Create(); break; case "Development": siloHostBuilder .UseLocalhostClustering() .Configure <EndpointOptions>(options => options.AdvertisedIPAddress = IPAddress.Loopback); database = new MongoDbFactory().Create("mongodb://localhost:27017"); break; default: throw new Exception($"Unknown environment '{EnvironmentName}'"); } siloHostBuilder.Configure <ClusterOptions>(options => { options.ClusterId = "dev"; options.ServiceId = "bancor"; }) .ConfigureServices(s => s.TryAddSingleton <IGrainStorage, MongoCustomerStorageProvider>()) .ConfigureServices(s => s.TryAddTransient <ICustomerRepository, CustomerRepository>()) .ConfigureServices(s => s.TryAddSingleton(database)) .ConfigureServices(s => s.TryAddTransient <IJournaldAccountRepository, JournalAccountRepository>()) .ConfigureServices(s => s.AddSingletonNamedService <IGrainStorage>("CustomerStorageProvider", (x, y) => new MongoCustomerStorageProvider(database, (IGrainFactory)x.GetService(typeof(IGrainFactory))))) .ConfigureLogging(logging => logging.AddConsole()) .AddMemoryGrainStorageAsDefault() .AddSimpleMessageStreamProvider("SMSProvider") .AddMemoryGrainStorage("PubSubStore") .AddCustomStorageBasedLogConsistencyProvider("CustomStorage") .UseTransactions(); var host = siloHostBuilder.Build(); return(host); }
private static async Task MainAsync() { try { //var config = new MongoDbConfiguration( // "MyBlogsDatabase", // DbLocation.Local, // "<connection string to an on premisse MongoDB instance>"); //var config = new MongoDbConfiguration( // "MyBlogsDatabase", // DbLocation.Azure, // "<Connection string from azure cosmos db>"); //Initialize the services Console.WriteLine($"Initializing services with {config.DatabaseLocation} configuration..."); var factory = new MongoDbFactory(config); var service = new MongoDbService(factory); Console.WriteLine("Services initialized."); //Ensure the database is created Console.WriteLine("Ensuring database is created..."); await factory.EnsureDatabaseIsCreatedAsync(); //Create some collections Console.WriteLine("Ensuring that database has some collections..."); await service.CreateCollectionAsync <Blog>(); //Set the Throughput based on the number of colections we have. //This is only applicable to Azure Cosmos Db instances Console.WriteLine("Scaling the throughput, based on the number of collections..."); await factory.ScaleDatabaseThroughputBasedOnCollectionCount(); Console.WriteLine("Inserting some data in the collections..."); var col = await service.GetCollectionAsync <Blog>(); await col.InsertOneAsync(new Blog { Title = "My First Blog", SubTitle = "How to insert data in MongoDb", Text = "InsertOneAsync" }); Console.WriteLine("Get data from the collections..."); var blogs = await(await col.FindAsync(_ => true)).ToListAsync(); Console.WriteLine(JsonConvert.SerializeObject(blogs, Newtonsoft.Json.Formatting.Indented)); Console.WriteLine("Delete data from the collections..."); foreach (var blog in blogs) { await col.DeleteOneAsync(b => b.Id == blog.Id); } Console.WriteLine("Done"); } catch (Exception ex) { if (Debugger.IsAttached) { Debugger.Break(); } Console.WriteLine(ex.Message); } }