KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have not worked with Entity Framework or generics before and am having some difficulty reducing my code. I am parsing a text file to load 10 lookup tables with data that could possibly change nightly. The text files have a heading for the "type" followed by a list of key/value sets. I have this working perfectly, but I would like to refactor the code to clean it up and would like to use generic methods to accomplish this so I can reduce the duplicated code. I have gotten the parsing down to a generic method, but I have not been able to figure out how to get the entities added to the context in a generic way. I have to loop over a list of the entities for each type and add them to the context: void Main() { switch (line.ToUpper()) { case "AREA": { List<Area> areaList = this.GetItems<Area>(file); foreach (var element in areaList) { if (context.Area.Find(element.Id) == null) { context.Area.Add(element); } } break; } case "CATEGORY": { List<Category> categoryList = this.GetItems<Category>(file); foreach (var element in categoryList) { if (context.Category.Find(element.Id) == null) { context.Category.Add(element); } } break; } } } private List<T> GetItems<T>(StreamReader reader) where T : ILookupData, new() { string line; List<T> list = new List<T>(); while (reader.Peek() == ' ') { line = reader.ReadLine().TrimStart(); string[] tokens = line.Split(new string[] { " - " }, 2, StringSplitOptions.None); T item = new T(); item.Id = Convert.ToInt32(tokens[0]); item.Description = (string)tokens[1]; lis
Tags (comma-separated)
Save Edits
Cancel