Example #1
0
        public void TestCamelCaseElementNameConvention()
        {
            var convention = new CamelCaseElementNameConvention();

            Assert.AreEqual("firstName", convention.GetElementName(typeof(TestClass).GetProperty("FirstName")));
            Assert.AreEqual("age", convention.GetElementName(typeof(TestClass).GetProperty("Age")));
            Assert.AreEqual("_DumbName", convention.GetElementName(typeof(TestClass).GetProperty("_DumbName")));
            Assert.AreEqual("lowerCase", convention.GetElementName(typeof(TestClass).GetProperty("lowerCase")));
        }
Example #2
0
        public static IServiceCollection AddMongoContext <TContext>(
            this IServiceCollection services,
            Action <MongoConfigurationBuilder> builder)
            where TContext : MongoDbContext
        {
            var configuration = new MongoConfigurationBuilder();

            builder(configuration);

            if (configuration.DatabaseName == null)
            {
                throw new ArgumentNullException("Не задано имя базы данных");
            }

            var clientSettings = string.IsNullOrEmpty(configuration.ConnectionString)
                ? new MongoClientSettings()
                : MongoClientSettings.FromConnectionString(configuration.ConnectionString);

            configuration.ConfigureClientSettings?.Invoke(clientSettings);

            if (clientSettings.Server == null)
            {
                throw new ArgumentNullException("Не заданы настройки подключения к серверу БД");
            }

            var databaseSettings = new MongoDatabaseSettings();

            configuration.ConfigureDatabaseSettings?.Invoke(databaseSettings);

            if (configuration.CamelCase)
            {
                var conventionPack      = new ConventionPack();
                var camelCaseConvention = new CamelCaseElementNameConvention();
                conventionPack.Add(camelCaseConvention);
                ConventionRegistry.Register("camelCase", conventionPack, t => true);
            }

            services.AddScoped(provider =>
            {
                var client   = new MongoClient(clientSettings);
                var database = client.GetDatabase(configuration.DatabaseName, databaseSettings);
                var context  = (TContext)Activator.CreateInstance(typeof(TContext), database);

                if (configuration.MigrationsAssembly != null)
                {
                    context.MigrationsAssembly = configuration.MigrationsAssembly;
                }

                return(context);
            });

            return(services);
        }
        public void TestCamelCaseElementNameConvention()
        {
            var convention = new CamelCaseElementNameConvention();
            var classMap   = new BsonClassMap <TestClass>();

            convention.Apply(classMap.MapMember(x => x.FirstName));
            convention.Apply(classMap.MapMember(x => x.Age));
            convention.Apply(classMap.MapMember(x => x._DumbName));
            convention.Apply(classMap.MapMember(x => x.lowerCase));
            Assert.Equal("firstName", classMap.GetMemberMap(x => x.FirstName).ElementName);
            Assert.Equal("age", classMap.GetMemberMap(x => x.Age).ElementName);
            Assert.Equal("_DumbName", classMap.GetMemberMap(x => x._DumbName).ElementName);
            Assert.Equal("lowerCase", classMap.GetMemberMap(x => x.lowerCase).ElementName);
        }
Example #4
0
        public void GivenACamelCaseConventionPack_WhenAppliedToAClassInheritingFromIndexIdentityBase_ThenPropertyNamesComeOutAsCamelCase()
        {
            var convention = new CamelCaseElementNameConvention();

            var classMap = new BsonClassMap <TestClass>();

            convention.Apply(classMap.MapMember(x => x.FirstNames));
            convention.Apply(classMap.MapMember(x => x.LastName));
            convention.Apply(classMap.MapMember(x => x.Title));
            convention.Apply(classMap.MapMember(x => x.Salutation));

            Assert.Equal("firstNames", classMap.GetMemberMap(x => x.FirstNames).ElementName);
            Assert.Equal("lastName", classMap.GetMemberMap(x => x.LastName).ElementName);
            Assert.Equal("title", classMap.GetMemberMap(x => x.Title).ElementName);
            Assert.Equal("salutation", classMap.GetMemberMap(x => x.Salutation).ElementName);
        }
Example #5
0
        public void TestCamelCaseElementNameConvention()
        {
            var convention = new CamelCaseElementNameConvention();
            var classMap   = new BsonClassMap <TestClass>();
            var firstName  = classMap.MapMember(x => x.FirstName);
            var age        = classMap.MapMember(x => x.Age);
            var _dumbName  = classMap.MapMember(x => x._DumbName);
            var lowerCase  = classMap.MapMember(x => x.lowerCase);

            convention.Apply(firstName);
            convention.Apply(age);
            convention.Apply(_dumbName);
            convention.Apply(lowerCase);

            Assert.AreEqual("firstName", firstName.ElementName);
            Assert.AreEqual("age", age.ElementName);
            Assert.AreEqual("_DumbName", _dumbName.ElementName);
            Assert.AreEqual("lowerCase", lowerCase.ElementName);
        }