Parse the file differently in another version

EDIT: I did some research on this, but couldn't find a solution!

I am reading the configuration from an XML file. The configuration layout is configured for a specific version. This version is in the first line of XML, which is easy to extract.

What I would like to know is the best way to parse this file / get another version based method (so all 4 elements, major, small, strict and editor).

Right now I came up with this:

switch (version.Major) { case 0: switch (version.Minor) { case 0: switch (version.Build) { case 0: switch (version.Revision) { case 0: return VersionNone(doc); } break; } break; } break; } throw new NotImplementedException(); 

But I do not find it elegant (in general), and I feel that there is a better way to do this.

Who can help?

+5
source share
4 answers

For something like this, I will be tempted to build an action dictionary

Edit: updated to include document option in response to comment from OP

  Dictionary<string, Action<XmlDocument>> versionSpecificMethods = new Dictionary<string, Action<XmlDocument>>{ {"1.0.0.0", DoStuff1}, {"1.2.3.4", DoStuff2}, {"3.1.7.182", DoStuff3}}; private void RunMethodForVersion(string version, XmlDocument someXmlDoc) { var codeToRun = versionSpecificMethods[version]; codeToRun.Invoke(someXmlDoc); } private static void DoStuff1(XmlDocument doc) { // Insert user code here } 
+8
source

If you want to share the code between the version, you can try using inheritance:

 interface IConfig { string GetConnectionString(); string GetUserSettings(); int GetDelay(); } class Version_1 : IConfig { public virtual string GetConnectionString() { ... } public virtual string GetUserSettings { ... } public virtual int GetDelay(); } class Version_1_1 : Version_1 { // Changed UserSetttings and delay in xml public override GetUserSettings { ... } public override int GetDelay { ... } } class Version_1_1_4 : Version_1_1 { // Changed delay in xml public override int GetDelay { ... } } 

Now you need to create a reqired config instance, depending on the version of your file.

+3
source

You can try this, for example: (

 enum Version { Major = 0, Build = 1, Revision = 2 } public class MyApp { static Dictionary<Version, object> versionManager = new Dictionary<Version, object> { {Version.Major, new Major() }, {Version.Build, new Build() }, {Version.Revision, new Revision() } }; public static object Run( int v, XmlDocument xmlDoc ) { try { Version version = (Version)v; var classObj = versionManager[version]; return classObj.GetType().GetMethod("Parse").Invoke(classObj, new object[] { xmlDoc });//System.Reflection; } catch { throw new NotImplementedException(); } } } //Major = 0 class Major { public Major( ) { } public string Parse( XmlDocument xmlDoc ) { return "DONE"; } } //Build = 1 class Build { public Build( ) { } public string Parse( XmlDocument xmlDoc ) { return "DONE"; } } //Revision = 2 class Revision { public Revision( ) { } public string Parse( XmlDocument xmlDoc ) { return "DONE"; } } 

Use ==>

 MyApp.Run(1/*Version==>Build*/, new XmlDocument()); 
+2
source

Based on @ Andy-P answer , but looking for the β€œlatest” version instead of the exact version:

 using ParserFunction = System.Func<object,object>; public static class ParserFactory { private static System.Collections.Generic.SortedDictionary<Version, ParserFunction> Parsers = new System.Collections.Generic.SortedDictionary<Version, ParserFunction>(); public static void SetParser(Version version, ParserFunction parser) { if (Parsers.ContainsKey(version)) Parsers[version] = parser; else Parsers.Add(version, parser); } public static ParserFunction GetParser(Version version) { if (Parsers.Count == 0) return null; Version lastKey = null; foreach ( var key in Parsers.Keys) { if (version.CompareTo(key) < 0) { if ( lastKey == null ) lastKey = key; break; } lastKey = key; if (version.CompareTo(key) == 0) break; } return Parsers[lastKey]; } } 
+1
source

Source: https://habr.com/ru/post/1276005/


All Articles