Skip to content

Instantly share code, notes, and snippets.

@MarcosMeli
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcosMeli/a9db0b7f11de0d072609 to your computer and use it in GitHub Desktop.
Save MarcosMeli/a9db0b7f11de0d072609 to your computer and use it in GitHub Desktop.
FileHelpers 4.0 API brainstorming www.filehelpers.net
var engine = new FileHelperEngine<RecordClass>():
var context = engine.CreateContext(): // will return a ProcessContext<RecordClass>
// e will be of type FieldErrorEventHandler<T>
context.Callbacks.OnFieldError = e => {
// Fields of e
e.Field
e.Record
e.RecordLine
e.ContinueAnyway
e.FieldValue
}:
context.Callbacks.OnLineError ...
context.Callbacks.BeforeRead
context.Callbacks.Afterread
context.Callbacks....
engine.ReadFile("", context);
// Concurrent access to this object will allow check for progress inside the context
context.Progress.CurrentLine
context.Progress.TotalLines
context.StartTime
context.EndTime
// The idea is to remove RunTime Records and start using:
// dynamic & ExpandoObject
// Provide a way to use existing type that are not decorated with attributes
// For existing types (in this cas System.Windows.Forms.Form)
var descriptor = new DelimitedRecordDescriptor<Form>("\t");
descriptor.AddField(x => x.Name)
descriptor.AddField(x => x.Width)
descriptor.AddField(x => x.Height)
var engine = descriptor.CreateEngine()
var records = engine.ReadFile("Source.txt")
//records will be a Form[]
engine.WriteFile("Destination.txt", records)
// For a dynamic types
var descriptor = new DelimitedRecordDescriptor("\t");
descriptor.AddField(x => x.Field1) // if posible or AddField("Field1")
descriptor.AddField(x => x.Field2)
var engine = descriptor.CreateEngine()
var records = engine.ReadFile("Source.txt")
//records will be a dynamic[] or ExpandoObject[]
engine.WriteFile("Destination.txt", records)
// Advance usage with fluent code
var descriptor = new DelimitedRecordDescriptor<Form>("\t");
descriptor.AddField(x => x.Name).Trim(TrimMode.Both).Align(AlignMode.Left)
descriptor.AddField(x => x.Size).ConvertToString(x => x.Width + "x" + x.Height)
.ConvertFromString(x => new Size(int.Parse(x.Split('x')[0]), int.Parse(x.Split('x')[1]))
descriptor.AddField(x => x.Icon).ConvertToString(x => Helper.IconToBase64(x) )
.ConvertFromString(x => Helper.IconFromBase64(x))
@regisbsb
Copy link

Maybe .net core support as well!? 👍

@MarcosMeli
Copy link
Author

@regisbsb of course, the new feature will avoid dynamic compilation, and other permission aware problems for a simple Linq Expressions perspective 🎯

@netniV
Copy link

netniV commented Aug 21, 2015

Using the field is a great idea. I make use of Linq.Expression to get the parameter info, thus the field name. Presumably though, you would add support for a naming attribute should the field name not match the header?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment