Exemple #1
0
        public static IdentityBuilder AddMongoDbStores <TUser, TRole, TKey>(this IdentityBuilder builder, Action <MongoIdentityOptions> setupDatabaseAction,
                                                                            IdentityErrorDescriber identityErrorDescriber = null)
            where TKey : IEquatable <TKey>
            where TUser : MongoUser <TKey>
            where TRole : MongoRole <TKey>
        {
            var dbOptions = new MongoIdentityOptions();

            setupDatabaseAction(dbOptions);

            var migrationCollection     = MongoUtil.FromConnectionString <MigrationHistory>(dbOptions, dbOptions.MigrationCollection);
            var migrationUserCollection = MongoUtil.FromConnectionString <MigrationMongoUser <TKey> >(dbOptions, dbOptions.UsersCollection);
            var userCollection          = MongoUtil.FromConnectionString <TUser>(dbOptions, dbOptions.UsersCollection);
            var roleCollection          = MongoUtil.FromConnectionString <TRole>(dbOptions, dbOptions.RolesCollection);

            Migrator.Apply <MigrationMongoUser <TKey>, TRole, TKey>(migrationCollection, migrationUserCollection, roleCollection);

            builder.Services.AddSingleton(x => userCollection);
            builder.Services.AddSingleton(x => roleCollection);

            // register custom ObjectId TypeConverter
            if (typeof(TKey) == typeof(ObjectId))
            {
                TypeConverterResolver.RegisterTypeConverter <ObjectId, ObjectIdConverter>();
            }

            // Identity Services
            builder.Services.AddTransient <IRoleStore <TRole> >(x => new RoleStore <TRole, TKey>(roleCollection, identityErrorDescriber));
            builder.Services.AddTransient <IUserStore <TUser> >(x => new UserStore <TUser, TRole, TKey>(userCollection, roleCollection, identityErrorDescriber));

            return(builder);
        }
Exemple #2
0
        public void Dir(
            [Option('i', Description = "input folder path to parse mysql ddl query")] string input,
            [Option('o', Description = "output directory path of generated C# class files")] string output,
            [Option('n', Description = "namespace to write")] string @namespace,
            [Option('c', Description = "converter name to use")] string converter = defaultConverter,
            [Option(Description = "true to ignore eol")] bool ignoreeol           = true,
            [Option(Description = "true to add bom")] bool addbom = false,
            [Option(Description = "true to dry-run")] bool dry    = false,
            [Option(Description = "executionid to detect execution")] string executionid = nameof(Dir))
        {
            PrintDryMessage(dry);
            Console.WriteLine($"dir executed. Output Directory: {output}");
            var encoding          = new UTF8Encoding(false);
            var tables            = Parser.FromFolder(input, encoding).ToArray();
            var resolvedConverter = TypeConverterResolver.Resolve(converter);
            var generator         = new Generator(resolvedConverter, addbom, ignoreeol);

            foreach (var table in tables)
            {
                var className = Generator.GetClassName(table.Name);
                var generated = generator.Generate(@namespace, className, table, resolvedConverter);
                generator.Save(className, generated, output, dry);

                QueryToCsharpContext.Current.AddLog(executionid, generated);
            }
        }
Exemple #3
0
        internal IList <object> ConvertToRow(TModel model, PropertyMap[] propertyMaps, int columnSize)
        {
            var row = new object[columnSize];

            foreach (var map in propertyMaps)
            {
                var converter = TypeConverterResolver.GetTypeConverter(map.Property);
                row[map.ColumnIndex] = converter.ConvertToField(map.GetValue(model));
            }

            return(row);
        }
Exemple #4
0
        public void ParseFromFile(
            [Option("-i", "file path to parse mysql query")] string input,
            [Option("-o", "directory path to output C# class file")] string output,
            [Option("-n", "namespace to write")] string @namespace,
            [Option("-c", "add bom or not")] string converter  = defaultConverter,
            [Option("--addbom", "add bom or not")] bool addbom = false)
        {
            var definition        = Parser.FromFile(input, false);
            var resolvedConverter = TypeConverterResolver.Resolve(converter);

            Context.Logger.LogInformation($"Output Directory: {output}");
            new Generator(addbom, resolvedConverter).Save(@namespace, definition, output);
        }
Exemple #5
0
        public void ParseString(
            [Option("-i", "mysql query to parse")] string input,
            [Option("-o", "directory path to output C# class file")] string output,
            [Option("-n", "namespace to write")] string @namespace,
            [Option("-c", "add bom or not")] string converter  = defaultConverter,
            [Option("--addbom", "add bom or not")] bool addbom = false)
        {
            var     listener = new CreateTableStatementDetectListener();
            IParser parser   = new Parser();

            parser.Parse(input, listener);
            var definition        = listener.TableDefinition;
            var resolvedConverter = TypeConverterResolver.Resolve(converter);

            Context.Logger.LogInformation($"Output Directory: {output}");
            new Generator(addbom, resolvedConverter).Save(@namespace, definition, output);
        }
Exemple #6
0
        internal TModel ConvertFromRow(IList <object> row, int rowIndex, PropertyMap[] propertyMaps)
        {
            var model = new TModel();

            foreach (var map in propertyMaps)
            {
                var converter = TypeConverterResolver.GetTypeConverter(map.Property);
                try
                {
                    map.SetValue(model, converter.ConvertFromField(row[map.ColumnIndex]));
                }
                catch (Exception ex)
                {
                    _errors.Add(new ErrorDetail
                    {
                        Type        = "ConvertFailed",
                        Message     = $"Field value({row[map.ColumnIndex]}) cannot be converted({ex.Message}), so property({map.Property.Name}) set is failed.",
                        RowIndex    = rowIndex,
                        ColumnIndex = map.ColumnIndex,
                        MemberNames = new[] { map.Property.Name }
                    });
                    if (_errors.Count >= ErrorLimit)
                    {
                        throw new TableIOException(_errors);
                    }
                }
            }

            var errors = ModelValidator.Validate(model);

            if (errors.Any())
            {
                foreach (var error in errors)
                {
                    error.RowIndex = rowIndex;
                }
                _errors.AddRange(errors);
                if (_errors.Count >= ErrorLimit)
                {
                    throw new TableIOException(_errors);
                }
            }

            return(model);
        }
        public static IdentityBuilder AddIdentityMongoDbProvider <TUser, TRole, TKey>(this IServiceCollection services,
                                                                                      Action <IdentityOptions> setupIdentityAction, Action <MongoIdentityOptions> setupDatabaseAction, IdentityErrorDescriber identityErrorDescriber = null)
            where TKey : IEquatable <TKey>
            where TUser : MongoUser <TKey>
            where TRole : MongoRole <TKey>
        {
            var dbOptions = new MongoIdentityOptions();

            setupDatabaseAction(dbOptions);

            var migrationCollection     = MongoUtil.FromConnectionString <MigrationHistory>(dbOptions, dbOptions.MigrationCollection);
            var migrationUserCollection = MongoUtil.FromConnectionString <MigrationMongoUser <TKey> >(dbOptions, dbOptions.UsersCollection);
            var userCollection          = MongoUtil.FromConnectionString <TUser>(dbOptions, dbOptions.UsersCollection);
            var roleCollection          = MongoUtil.FromConnectionString <TRole>(dbOptions, dbOptions.RolesCollection);

            // apply migrations before identity services resolved
            Migrator.Apply <MigrationMongoUser <TKey>, TRole, TKey>(migrationCollection, migrationUserCollection, roleCollection);

            var builder = services.AddIdentity <TUser, TRole>(setupIdentityAction ?? (x => { }));

            builder.AddRoleStore <RoleStore <TRole, TKey> >()
            .AddUserStore <UserStore <TUser, TRole, TKey> >()
            .AddUserManager <UserManager <TUser> >()
            .AddRoleManager <RoleManager <TRole> >()
            .AddDefaultTokenProviders();

            services.AddSingleton(x => userCollection);
            services.AddSingleton(x => roleCollection);

            // register custom ObjectId TypeConverter
            if (typeof(TKey) == typeof(ObjectId))
            {
                TypeConverterResolver.RegisterTypeConverter <ObjectId, ObjectIdConverter>();
            }

            // Identity Services
            services.AddTransient <IRoleStore <TRole> >(x => new RoleStore <TRole, TKey>(roleCollection, identityErrorDescriber));
            services.AddTransient <IUserStore <TUser> >(x => new UserStore <TUser, TRole, TKey>(userCollection, roleCollection, identityErrorDescriber));

            return(builder);
        }