Exemple #1
0
        public async Task ClassName_WithGenerics()
        {
            var options = new JsonStoreOptions();

            _path = FilePathEvaluator.GetFilePath(nameof(Person));
            var store = new JsonRepository <Person, int>(options);

            await store.AddAsync(_person);

            await store.SaveChangesAsync();

            Assert.True(File.Exists(_path));
        }
Exemple #2
0
        private static int Main(string[] args)
        {
            if (args.Length is < 1 or > 4)
            {
                PrintMessage("Random JSON Creator", ConsoleColor.DarkGray);
                PrintInstructions();
            }

            int filesCount;
            int itemsPerFile;

            try
            {
                filesCount = Convert.ToInt32(args[0]);
                if (args.Length == 1 || !int.TryParse(args[1], out itemsPerFile))
                {
                    itemsPerFile = 200;
                }
            }
            catch (FormatException)
            {
                PrintMessage("Invalid parameters!", ConsoleColor.Red);
                PrintInstructions();
                return(1);
            }

            var verbose             = args.Contains("--verbose");
            var deletePreviousFiles = args.Contains("--deletePrevious");

            var fullFilePath = Path.Join(Environment.CurrentDirectory, FilePath);

            if (deletePreviousFiles && Directory.GetFiles(fullFilePath).Length > 0)
            {
                if (verbose)
                {
                    PrintMessage($"Deleting previous files on {fullFilePath}");
                }
                foreach (var file in Directory.GetFiles(fullFilePath))
                {
                    File.Delete(file);
                }
            }

            if (verbose)
            {
                PrintMessage($"Creating {filesCount} files with {itemsPerFile} items...", ConsoleColor.Green);
            }
            for (var fileIndex = 0; fileIndex < filesCount; fileIndex++)
            {
                if (verbose)
                {
                    PrintMessage($"Generating data for file {fileIndex}...");
                }
                var items = new JsonRepository <FileItem, int>(new JsonStoreOptions
                {
                    NamingStrategy           = new StaticNamingStrategy(GetRandomString()),
                    Location                 = fullFilePath,
                    ThrowOnSavingChangedFile = false
                });

                for (var itemIndex = 0; itemIndex < itemsPerFile; itemIndex++)
                {
                    items.AddAsync(new FileItem {
                        Id = itemIndex, Data = GetRandomString()
                    }).Wait();
                }

                if (verbose)
                {
                    PrintMessage($"Data generated. Saving file {fileIndex}...");
                }
                items.SaveChangesAsync().Wait();

                if (verbose)
                {
                    PrintMessage($"File {fileIndex} saved.");
                }
            }

            if (verbose)
            {
                PrintMessage($"{filesCount} files created on {fullFilePath}.", ConsoleColor.Green);
            }
            return(0);
        }