static void Main(string[] args)
        {
            var context = new MongoDbContextBuilder()
                          .SetOptions(opt =>
            {
                opt.ConnectionString = "<your connection string>";
                opt.DatabaseName     = "<your database>";
                opt.MaxLifeTime      = 1;
                opt.MaxIdleTime      = 1;
            })
                          .Build();


            IMongoCollection <TestDatabaseModel> modelCollection = context.GetCollection <TestDatabaseModel>("testModels");

            var filter = Builders <TestDatabaseModel> .Filter.Empty;
            var models = modelCollection
                         .Find(filter)
                         .ToList();

            foreach (var m in models)
            {
                Console.WriteLine(JsonConvert.SerializeObject(m));
            }

            var newModel = new TestDatabaseModel()
            {
                Name = "TestModel"
            };

            modelCollection.InsertOne(newModel);
        }
Beispiel #2
0
        public void CanBuildMongoDbContext()
        {
            const string connectionString = "mongodb://localhost:27017/testDb";

            var result = MongoDbContextBuilder.Build(connectionString);

            Assert.IsType <MongoDbContext>(result);
        }
        private static MongoDbContextBuilder <MongoDbContext> AddMongoDbContext(
            this IServiceCollection services,
            Action <MongoDbContextConfiguration> contextConfiguration)
        {
            services.Configure(contextConfiguration);
            var builder = new MongoDbContextBuilder <MongoDbContext>(services);

            return(builder.Build());
        }
Beispiel #4
0
        public static IServiceCollection AddMongoDbContext <TContext>(this IServiceCollection services, Action <IMongoDbContextBuilder> builderAction)
            where TContext : MongoDbContext
        {
            if (builderAction == null)
            {
                throw new ArgumentNullException(nameof(builderAction));
            }

            var builder = new MongoDbContextBuilder <TContext>();

            builderAction.Invoke(builder);

            services.AddSingleton(builder.Build);

            return(services);
        }
    public static IServiceCollection AddMongoDbCore <TService, TContext> (this IServiceCollection services, Action <IMongoDbContextBuilder> optionsAction)
        where TService : class
        where TContext : MongoDbContext
    {
        if (optionsAction == null)
        {
            throw new ArgumentNullException(nameof(optionsAction));
        }

        var optionsBuilder = new MongoDbContextBuilder <TService, TContext> ();

        optionsAction.Invoke(optionsBuilder);

        services.AddSingleton <TService> (optionsBuilder.Build);

        return(services);
    }
        Task IAsyncLifetime.InitializeAsync()
        {
            builder = new MongoDbContextBuilder <TestDbContext>
            {
                DatabaseName = "Test"
            };

            var services = new ServiceCollection();

            services
            .AddSingleton <TestService>()
            .AddFakeMongoDbClientFactory();
            var provider = services.BuildServiceProvider();

            dbContext = builder.Build(provider);

            return(Task.CompletedTask);
        }
Beispiel #7
0
        /// <summary>
        /// Executes the migration
        /// </summary>
        /// <param name="options">Migration options</param>
        /// <exception cref="NotSupportedException">Unsupported Migration Options</exception>
        public static MigrationResult Run(MigrationOptions options)
        {
            IMigrationOperation migrationOperation;
            IMongoDbContext     dbContext = null;
            string result;
            var    isSuccessful = true;

            try
            {
                if (options.Uri != null)
                {
                    dbContext = MongoDbContextBuilder.Build(options.Uri.ConnectionString);
                }
                else
                {
                    if (options.Operation != MigrationOperation.Create &&
                        options.Operation != MigrationOperation.None)
                    {
                        return(new MigrationResult
                        {
                            Operation = options.Operation,
                            Result = "A vaild MongoDB URI is required. Run --help for more information.",
                            IsSuccessful = false
                        });
                    }
                }

                switch (options.Operation)
                {
                case MigrationOperation.Up:
                    migrationOperation = new UpMigrationOperation(dbContext, options.ProjectFile);
                    break;

                case MigrationOperation.Down:
                    migrationOperation = new DownMigrationOperation(dbContext, options.ProjectFile);
                    break;

                case MigrationOperation.Status:
                    migrationOperation = new StatusMigrationOperation(dbContext, options.ProjectFile);;
                    break;

                case MigrationOperation.Create:
                    migrationOperation = new CreateMigrationOperation(options.MigrationName);
                    break;

                case MigrationOperation.None:
                default:
                    return(new MigrationResult
                    {
                        Operation = options.Operation,
                        Result = $"{options.Operation} is not a supported operation.",
                        IsSuccessful = false
                    });
                }
                result = migrationOperation.Execute();
            }
            catch (MongoConfigurationException e)
            {
                result       = e.Message;
                isSuccessful = false;
            }

            return(new MigrationResult
            {
                Operation = options.Operation,
                Result = result,
                IsSuccessful = isSuccessful
            });
        }
Beispiel #8
0
 public void HandleNullOrEmptyConnectionString(string connectionString)
 {
     Assert.Throws <ArgumentNullException>(() => MongoDbContextBuilder.Build(connectionString));
 }