Exemplo n.º 1
0
        public MongoInfrastructure(MongoContextOptions <TMongoContext> _mongoContextOptions, IMongoClientFactory _mongoClientFactory)
        {
            currentContextOptions = _mongoContextOptions;
            //获取mongo客户端信息
            var mongoClientInfo = _mongoClientFactory.GetMongoClient <TMongoContext>();

            currentMongoClient   = mongoClientInfo.Item1;
            currentMongoDatabase = Build(mongoClientInfo.Item2.DataBase);
        }
Exemplo n.º 2
0
        public DefaultMongoGridFSInfrastructure(MongoContextOptions <TMongoContext> _mongoContextOptions, IMongoClientFactory _mongoClientFactory, IServiceProvider serviceProvider)
        {
            currentContextOptions = _mongoContextOptions;
            //获取mongo客户端信息
            var mongoClientInfo = _mongoClientFactory.GetMongoClient <TMongoContext>();

            //获取客户端
            currentMongoClient = mongoClientInfo.Item1;
            //获取当前上下文信息
            currentMongoContext = serviceProvider.GetService(MergeNamedType.Get(typeof(TMongoContext).Name)) as TMongoContext;
            //构建GridFS对象
            gridFSBucket = Build(currentMongoClient.GetDatabase(mongoClientInfo.Item2.DataBase), currentMongoContext.BucketName);
        }
        public static void AddNetCleanMongoContextMemory<T>(this IServiceCollection services) where T : MongoContext
        {
            var options = new MongoContextOptions { UseInMemory = true };

            var context = (T)Activator.CreateInstance(typeof(T), new object[] { typeof(T).Name, options });

            services.AddSingleton<T>(context);

            services.AddScoped<MongoContext>(provider => provider.GetRequiredService<T>());

            services.AddScoped<IUnitOfWork>(provider => provider.GetRequiredService<T>() as IUnitOfWork);

            services.BuildServiceProvider().GetRequiredService<T>().CreateCollection();
        }
        protected MongoContext(string connectionString, MongoContextOptions options)
        {
            _connectionString = connectionString;
            _options          = options;

            if (_options.UseInMemory)
            {
                _runner = MongoDbRunner.Start();

                DatabaseName = connectionString;

                Database = new MongoClient(_runner.ConnectionString).GetDatabase(DatabaseName);
            }
            else
            {
                DatabaseName = new MongoUrl(_connectionString).DatabaseName;

                Database = new MongoClient(_connectionString).GetDatabase(DatabaseName);
            }
        }
        public static void AddNetCleanMongoContext<T>(
            this IServiceCollection services,
            string connectionString,
            MongoContextOptions options,
            MigrationOptions migration = MigrationOptions.EnsureCreated) where T : MongoContext
        {
            if (string.IsNullOrEmpty(connectionString))
            {
                throw new ArgumentException($"'{nameof(connectionString)}' is required.", nameof(connectionString));
            }

            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var context = (T)Activator.CreateInstance(typeof(T), new object[] { connectionString, options });

            services.AddSingleton<T>(context);

            services.AddScoped<MongoContext>(provider => provider.GetRequiredService<T>());

            services.AddScoped<IUnitOfWork>(provider => provider.GetRequiredService<T>() as IUnitOfWork);

            switch (migration)
            {
                case MigrationOptions.EnsureDeleteAndCreated:
                    {
                        services.BuildServiceProvider().GetRequiredService<T>().DropCollection();
                        services.BuildServiceProvider().GetRequiredService<T>().CreateCollection();
                    }
                    break;
                case MigrationOptions.EnsureCreated:
                    services.BuildServiceProvider().GetRequiredService<T>().CreateCollection();
                    break;
                default:
                    break;
            }
        }
Exemplo n.º 6
0
        public DefaultMongoRepository(IServiceProvider _serviceProvider, MongoContextOptions <TMongoContext> _mongoContextOptions)
        {
            serviceProvider = _serviceProvider;

            mongoContextOptions = _mongoContextOptions;
        }
Exemplo n.º 7
0
 public DefaultMongoGridFS(IMongoGridFSInfrastructure <TMongoContext> _mongoGridFS, MongoContextOptions <TMongoContext> _currentContextOptions)
 {
     mongoGridFS           = _mongoGridFS;
     currentContextOptions = _currentContextOptions;
 }