c# - Standard way to instantiate objects from an xml config file -
question: how can instantiate model based on config file?
background: have model simulator application consisting of dozens of different types of objects, each composed of other objects (in complex compositional hierarchy). able instantiate model based on external configuration file. there "standard approach" sort of task?
so example, if had config.xml file this:
<site> <facility name="f1"> <tank name="t1"/> <pipe name="p1"/> </facility> <facility name="f2"> ... i want configurator can parse file , create site object composed of collection of facility objects composed of various tank & pipe equipment, etc. (eventually in reverse persist model.) model configuration change frequently; users collect many different versions of config file.
here options i've seen suggested, haven't used of these tools yet, , i'm hoping advice (if any) of these approaches might appropriate problem.
- role own xml config file, parse using xmltextreader, , instantiate model using reflection map strings types. (but re-inventing wheel?)
- use configurationmanager
system.configuration, storing settings in app.config. (but not sure how support "many different versions of config file" requirement.) - use xmlserializer. (but understand requires default constructors, rule out constructor-based dependency injection had hoped use.)
- use ioc container. (but have several inexperienced programmers interacting program; i'd rather not overwhelm them unnecessary complexity.)
(bottom line: hope "do simplest thing possibly work". unfamiliarity these tools prevents me determining 1 simplest.)
option 1:
@johnsaunders says specific way create xmltextreader deprecated, not xmltextreader per se. that's true. see here: http://msdn.microsoft.com/en-us/library/9khb6435(v=vs.110).aspx
while long list of cases 1 option, 1 not create such knot in code. alternatives:
- factory objects register themself key used in xml identify factory object
- using type name in xml , working reflection: http://msdn.microsoft.com/en-us/library/vstudio/f7ykdhsy(v=vs.100).aspx
option 2:
i can't see how saving configuration in app.config instead of other xml file relates or solves original problem.
option 3:
then don't scatter serialization code.
option 4:
how looking source code of not big di framework ideas how then?
hth
Comments
Post a Comment