Ejemplo n.º 1
0
        private static IMongoCollection <Employee> CreateMongoCollection()
        {
            var mongoClient   = MongoClientFactory.Create();
            var mongoDatabase = mongoClient.GetDatabase("SgDemo");

            return(mongoDatabase.GetCollection <Employee>("employee"));
        }
Ejemplo n.º 2
0
        protected override void Load(ContainerBuilder builder)
        {
            builder
            .RegisterType <MongoConfig>()
            .As <IMongoConfig>()
            .SingleInstance();

            builder
            .Register(c => MongoClientFactory.Create(c.Resolve <IMongoConfig>()))
            .As <MongoClient>()
            .SingleInstance();
        }
Ejemplo n.º 3
0
        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();
        }
Ejemplo n.º 4
0
        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 <>));
        }
Ejemplo n.º 5
0
        public void Create_WhenMongoClientCreatedShouldReturnConnectionStringFromConfigurationRepository()
        {
            var mongoClient = mongoClientFactory.Create(configuration);

            mongoClient.ShouldBeType <MongoClient>();
        }