public T[] ReadFile <T>(string fileName) where T : class, new() { string filePath = GetFilePath(fileName); if (!File.Exists(filePath)) { File.Create(filePath).Dispose(); } IFlatFileEngine engine = GetFlatFileEngine <T>(); MethodInfo decorateMethod = engine.GetType().GetMethod("Read"); MethodInfo generic = decorateMethod.MakeGenericMethod(typeof(T)); try { using (var stream = new FileStream(filePath, FileMode.Open)) { var results = generic.Invoke(engine, new object[] { stream }) as IEnumerable <T>; return(results.ToArray()); } } catch (FormatException ex) { Console.WriteLine($"Error while loading file {filePath}, deleting file to ensure it doesn't happen again."); Console.WriteLine(ex); File.Delete(filePath); return(new T[0]); } }
public FixedLengthIntegrationTests() { _layout = new FixedLayout <TestObject>() .WithMember(o => o.Id, set => set.WithLength(5).WithLeftPadding('0')) .WithMember(o => o.Description, set => set.WithLength(25).WithRightPadding(' ')) .WithMember(o => o.NullableInt, set => set.WithLength(5).AllowNull("=Null").WithLeftPadding('0')); _engine = new FixedLengthFileEngine(Layout, new FixedLengthLineBuilderFactory(), new FixedLengthLineParserFactory()); }
public void Setup() { _csvConfig = new Configuration(); _csvConfig.RegisterClassMap <CsvHelperMappingForCustomObject>(); _fluentEngine = new DelimitedFileEngineFactory() .GetEngine(new FlatFileMappingForCustomObject()); var fixture = new Fixture(); _records = fixture.CreateMany <CustomObject>(N).ToArray(); }
public static string WriteToString <T>(this IFlatFileEngine engine, IEnumerable <T> source) where T : class, new() { using (var stream = new MemoryStream()) using (var reader = new StreamReader(stream)) { engine.Write(stream, source); stream.Position = 0; string fileContent = reader.ReadToEnd(); return(fileContent); } }
public void SaveFile <T>(string fileName, T[] objects) where T : class, new() { string filePath = GetFilePath(fileName); using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Write)) { IFlatFileEngine engine = GetFlatFileEngine <T>(); MethodInfo decorateMethod = engine.GetType().GetMethod("Write"); MethodInfo generic = decorateMethod.MakeGenericMethod(typeof(T)); generic.Invoke(engine, new object[] { stream, objects }); } }
public void Setup() { _helperEngine = new FileHelperEngine <FixedSampleRecord>(); var factory = new FixedLengthFileEngineFactory(); _fluentEngine = factory.GetEngine(new FixedSampleRecordLayout()); Configuration.Converters.UseOptimizedConverters(); var fixture = new Fixture(); _records = fixture.CreateMany <FixedSampleRecord>(N).ToArray(); }
public DelimitedIntegrationTests() { _layout = new DelimitedLayout <TestObject>() .WithDelimiter(";") .WithQuote("\"") .WithMember(o => o.Id) .WithMember(o => o.Description) .WithMember(o => o.NullableInt, set => set.AllowNull("=Null")); _engine = new DelimitedFileEngine( Layout, new DelimitedLineBuilderFactory(), new DelimitedLineParserFactory()); }
public void Setup() { _csvConfig = new Configuration(); _csvConfig.RegisterClassMap <CsvHelperMappingForCustomObject>(); _fluentEngine = new DelimitedFileEngineFactory() .GetEngine(new FlatFileMappingForCustomObject()); var records = new StringBuilder(N * 80); records.AppendLine("String Column,Int Column,Guid Column,Custom Type Column"); for (int i = 0; i < N; i++) { records.AppendLine($"\"{i + 1}\",{i + 1},{Guid.NewGuid():D},{i + 1}|{i + 2}|{i + 3}"); } _records = records.ToString(); }
public void Setup() { _helperEngine = new FileHelperEngine <FixedSampleRecord>(); _fluentEngine = new FixedLengthFileEngineFactory() .GetEngine(new FixedSampleRecordLayout()); Configuration.Converters.UseOptimizedConverters(); var records = new StringBuilder(N * 185); var random = new Random(); var fixture = new Fixture().Customize(new RandomFixedStringCustomization(80)); for (int i = 0; i < N; i++) { records.Append(20000000000L + i) .Append(fixture.Create <string>()) .Append(fixture.Create <string>()) .Append(random.Next(0, 999999).ToString().PadRight(6)) .AppendLine(); } _records = records.ToString(); }