private async Task EnsureIndicesCreatedImplAsync()
        {
            var indexNames = new
            {
                UniqueEmail = "identity_email_unique",
                Login       = "******"
            };

            var pack = ConventionRegistry.Lookup(typeof(CamelCaseElementNameConvention));

            var emailKeyBuilder = Builders <TUser> .IndexKeys.Ascending(user => user.Email.Value);

            var loginKeyBuilder = Builders <TUser> .IndexKeys.Ascending("logins.loginProvider").Ascending("logins.providerKey");

            var tasks = new[]
            {
                _usersCollection.Indexes.CreateOneAsync(emailKeyBuilder, new CreateIndexOptions {
                    Unique = true, Name = indexNames.UniqueEmail
                }),
                _usersCollection.Indexes.CreateOneAsync(loginKeyBuilder, new CreateIndexOptions {
                    Name = indexNames.Login
                })
            };

            await Task.WhenAll(tasks).ConfigureAwait(false);
        }
예제 #2
0
 private List <string> GetConventionsInRegistry()
 {
     return(ConventionRegistry.Lookup(typeof(object)).Conventions
            .OrderBy(x => x.Name)
            .Select(x => x.Name)
            .ToList());
 }
        public void RegisterConventionPack_RegisterEqualConventionPacksTwoTimes_OneConventionPackRegistered()
        {
            // Arrange
            var mongoDatabaseBuilder = new MongoDatabaseBuilder(_mongoOptions);

            mongoDatabaseBuilder.RegisterConventionPack(
                "duplicateTestConventionPack", new ConventionPack
            {
                new DuplicateTestConvention1(),
                new DuplicateTestConvention2()
            }, t => true);

            mongoDatabaseBuilder.RegisterConventionPack(
                "duplicateTestConventionPack", new ConventionPack
            {
                new DuplicateTestConvention2(),
                new DuplicateTestConvention1()
            }, t => true);

            // Act
            MongoDbContextData result = mongoDatabaseBuilder.Build();

            // Assert
            IEnumerable <IConvention> conventions = ConventionRegistry
                                                    .Lookup(typeof(string)).Conventions;
            int duplicateTestConventionCount1 = conventions
                                                .Count(convention => convention.Name == nameof(DuplicateTestConvention1));
            int duplicateTestConventionCount2 = conventions
                                                .Count(convention => convention.Name == nameof(DuplicateTestConvention2));

            Assert.Equal(1, duplicateTestConventionCount1);
            Assert.Equal(1, duplicateTestConventionCount2);
        }
        public void MongoDbConventionTest()
        {
            MongoDbConvention.SetCamelCaseElementNameConvention();
            var convention = ConventionRegistry.Lookup(typeof(Type)).Conventions.FirstOrDefault(c => c.Name == "CamelCaseElementName");

            convention.Should().NotBeNull();
        }
예제 #5
0
        public void IncludesNeededConventions()
        {
            var database   = Mock.Of <IMongoDatabase>();
            var eventStore = new MongodbEventStore(database);

            var conventionPack = ConventionRegistry.Lookup(typeof(Event));

            conventionPack.Conventions.Should().Contain(x => x.Name == "IgnoreExtraElements");
        }
        public void UseIgnoreIfNull()
        {
            builder.UseIgnoreIfNull(false);

            Assert.NotEmpty(builder.Conventions.OfType <IgnoreIfNullConvention>());

            var pack = ConventionRegistry.Lookup(typeof(ArticleDocument));

            Assert.NotEmpty(pack.Conventions.OfType <IgnoreIfNullConvention>());
        }
 /// <summary>
 /// Registers the convention pack to serialize most enumerations as strings but sometimes (like in dictionaries) you'll need to use the <see cref="EnumSerializer{TEnum}"/> directly.
 /// </summary>
 public static void RegisterEnumAsStringConventionIfNotRegistered()
 {
     if (ConventionRegistry.Lookup(typeof(ConventionPack)).Conventions.All(_ => _.GetType() != typeof(EnumRepresentationConvention)))
     {
         var enumAsStringPack = new ConventionPack {
             new EnumRepresentationConvention(BsonType.String)
         };
         ConventionRegistry.Register(EnumAsStringConventionName, enumAsStringPack, t => true);
     }
 }
        public void UseCamelCaseElementName()
        {
            builder.UseCamelCaseElementName();

            Assert.NotEmpty(builder.Conventions.OfType <CamelCaseElementNameConvention>());

            var pack = ConventionRegistry.Lookup(typeof(ArticleDocument));

            Assert.NotEmpty(pack.Conventions.OfType <CamelCaseElementNameConvention>());
        }
        public void CheckConventionOfPropertyEnumerable()
        {
            builder.UseIgnoreIfDefault(false);

            Assert.NotEmpty(builder.Conventions.OfType <IgnoreIfDefaultConvention>());

            var pack = ConventionRegistry.Lookup(typeof(Tag));

            Assert.NotEmpty(pack.Conventions.OfType <IgnoreIfDefaultConvention>());
        }
        public void CheckConventionOfPropertyArray()
        {
            builder.UseIgnoreIfDefault(false);

            Assert.NotEmpty(builder.Conventions.OfType <IgnoreIfDefaultConvention>());

            var pack = ConventionRegistry.Lookup(typeof(CommentDocument));

            Assert.NotEmpty(pack.Conventions.OfType <IgnoreIfDefaultConvention>());
        }
        public void CheckConventionOfKnownType()
        {
            builder.UseIgnoreIfDefault(false);

            Assert.NotEmpty(builder.Conventions.OfType <IgnoreIfDefaultConvention>());

            var pack = ConventionRegistry.Lookup(typeof(NewsDocument));

            Assert.NotEmpty(pack.Conventions.OfType <IgnoreIfDefaultConvention>());
        }
        public void RegisterImmutableConventionPack_RegisteredSuccessfully()
        {
            // Arrange
            var mongoDatabaseBuilder = new MongoDatabaseBuilder(_mongoOptions);

            mongoDatabaseBuilder.RegisterImmutableConventionPack();

            // Act
            mongoDatabaseBuilder.Build();

            // Assert
            IEnumerable <IConvention> conventions = ConventionRegistry.Lookup(typeof(string)).Conventions;

            Assert.NotNull(conventions.OfType <ImmutableConvention>().FirstOrDefault());
            Assert.NotNull(conventions.OfType <IgnoreExtraElementsConvention>().FirstOrDefault());
        }
        public void RegisterConventionPack_ConventionPackNotRegistered_ConventionPacksNotRegistered()
        {
            // Arrange
            var mongoDatabaseBuilder = new MongoDatabaseBuilder(_mongoOptions);

            // Act
            MongoDbContextData result = mongoDatabaseBuilder.Build();

            // Assert
            IEnumerable <IConvention> conventions = ConventionRegistry.Lookup(typeof(string)).Conventions;
            int enumRepConvention   = conventions.Count(convention => convention.Name == "EnumRepresentation");
            int camelCaseConvention = conventions.Count(convention => convention.Name == "CamelCaseElementName");

            Assert.Equal(0, enumRepConvention);
            Assert.Equal(0, camelCaseConvention);
        }
예제 #14
0
    public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
    {
        var bsonWriter = context.Writer;

        if (value != null && value.GetType().IsEnum)
        {
            var conventions = ConventionRegistry.Lookup(value.GetType());
            var enumRepresentationConvention = (EnumRepresentationConvention)conventions.Conventions.FirstOrDefault(convention => convention is EnumRepresentationConvention);
            if (enumRepresentationConvention != null)
            {
                switch (enumRepresentationConvention.Representation)
                {
                case BsonType.String:
                    value = value.ToString();
                    bsonWriter.WriteString(value.ToString());
                    return;
                }
            }
        }
        base.Serialize(context, args, value);
    }
        public void RegisterConventionPack_RegisterOneSpecificConventionPack_RegisteredSuccessfully()
        {
            // Arrange
            var mongoDatabaseBuilder = new MongoDatabaseBuilder(_mongoOptions);

            mongoDatabaseBuilder.RegisterConventionPack(
                "camelCase", new ConventionPack
            {
                new EnumRepresentationConvention(BsonType.String),
                new CamelCaseElementNameConvention()
            }, t => true);

            // Act
            MongoDbContextData result = mongoDatabaseBuilder.Build();

            // Assert
            IEnumerable <IConvention> conventions = ConventionRegistry.Lookup(typeof(string)).Conventions;

            Assert.NotNull(conventions.OfType <EnumRepresentationConvention>().FirstOrDefault(c => c.Representation == BsonType.String));
            Assert.NotNull(conventions.OfType <CamelCaseElementNameConvention>().FirstOrDefault());
        }
        public void AddMongoDbContext_WithConfiguration()
        {
            var configurationBuilder = new Microsoft.Extensions.Configuration.ConfigurationBuilder();

            configurationBuilder.Add(new Microsoft.Extensions.Configuration.Memory.MemoryConfigurationSource
            {
                InitialData = new Dictionary <string, string> {
                    { "ConnectionString", MongoDbDefaults.LocalConnectionString },
                    { "DatabaseName", "test" },
                    { "CamelCase", "true" },
                    { "IgnoreIfNull", "true" }
                }
            });

            var configuration = configurationBuilder.Build();

            var services = new ServiceCollection();

            services
            .AddSingleton <TestService>()
            .AddMongoDbContext <TestDbContext>(configuration)
            .AddFakeMongoDbClientFactory();

            using (var scope = services.BuildServiceProvider())
            {
                var dbContext = scope.GetService <TestDbContext>();

                var pack = ConventionRegistry.Lookup(typeof(Document));
                Assert.NotEmpty(pack.Conventions.OfType <CamelCaseElementNameConvention>());
                Assert.NotEmpty(pack.Conventions.OfType <IgnoreIfDefaultConvention>());
                Assert.NotEmpty(pack.Conventions.OfType <IgnoreIfDefaultConvention>());

                Assert.Equal("test", dbContext.Database.DatabaseNamespace.DatabaseName);

                dbContext.Dispose();
            }
        }