private static IMongoCollection <Employee> CreateMongoCollection() { var mongoClient = MongoClientFactory.Create(); var mongoDatabase = mongoClient.GetDatabase("SgDemo"); return(mongoDatabase.GetCollection <Employee>("employee")); }
protected override void Load(ContainerBuilder builder) { builder .RegisterType <MongoConfig>() .As <IMongoConfig>() .SingleInstance(); builder .Register(c => MongoClientFactory.Create(c.Resolve <IMongoConfig>())) .As <MongoClient>() .SingleInstance(); }
static void Main(string[] args) { var server = ConfigurationManager.AppSettings.Get("MongoDbServer"); var port = Convert.ToInt32(ConfigurationManager.AppSettings.Get("MongoDbPort")); var mongoDbConnectionSettings = new MongoDbConnectionSettings(server, port); var client = MongoClientFactory.Create(mongoDbConnectionSettings); var database = client.GetDatabase("book"); var collection = database.GetCollection <BsonDocument>("towns"); var document = collection.Find(new BsonDocument()).FirstOrDefault(); Console.ReadKey(); }
public static void Init(IServiceCollection services, IConfiguration configuration) { services.Configure <MongoDbOptions>(configuration.GetSection(nameof(MongoDbOptions))); services.AddSingleton(serviceProvider => { var mongoDbOptions = serviceProvider.GetRequiredService <IOptions <MongoDbOptions> >(); var logger = serviceProvider.GetRequiredService <ILogger <MongoClient> >(); var mongoClient = MongoClientFactory.Create(mongoDbOptions.Value, logger); return(mongoClient); }); services.AddSingleton(typeof(IGenericRepository <>), typeof(GenericRepository <>)); }
public void Create_WhenMongoClientCreatedShouldReturnConnectionStringFromConfigurationRepository() { var mongoClient = mongoClientFactory.Create(configuration); mongoClient.ShouldBeType <MongoClient>(); }