Is it better to read an xml file once and pass around the values, or read the xml multiple times where needed?

StackOverflow https://stackoverflow.com/questions/19188226

Pregunta

I have an Asp.Net MVC web application, with an xml configuration file that has about 40 values in it.

My web app is just one page, but it is split up into several partial views (each with their own models and controllers). Each section needs to use some, but not all, of the values from the config file, though there is some overlap. E.g., ViewA needs valueX, and ViewB needs valueX and valueY.

My question is this - is it better to read the .xml file once in the main HomeController, and pass around the values to all of the other views/controllers? I'm not sure how I would do this. I can't really put these values into a model since each section has its own model, and I don't think I want to use ViewBag to store 40 values.

Or is it better to have each Controller read the xml file on its own and only get the data it needs? This would cause the same xml file to be read up to 5 times just to load one screen, and then every time a partial view is updated.

Will either or these options have an impact on performance or memory?

¿Fue útil?

Solución

Will either or these options have an impact on performance or memory?

Yes. Reading a file multiple times is going to be much slower than reading it once. And holding onto the values will consume more memory than not holding them onto them.

But we're not talking about a lot of time or a lot of memory, not with just 40 values. Unless those values are megabytes each.

There are many ways to load XML data and cache it. You can load it once--the first time the application is accessed. Or you can load it for each request. In my application, I load the data once the first time the application is accessed. It's loaded into an object that is essentially a name/value collection. I pass an interface reference to every controller that needs it. But then I use StructureMap, which makes this kind of thing pretty easy.

Without knowing more about the structure of your application, it's hard to say which would be a better fit. You could have a lazy-loaded singleton object that your models call to get the data. The singleton object itself would load the data on first use. Check out Jon Skeet's Implementing the Singleton Pattern in C#.

Although maybe you're worrying needlessly. If the file is small, it's likely that the first read will get the data from the disk into a cache, and after that your calls to read the file are satisfied from the cache. You still have the cost of parsing the XML for each call, but reading the file is cheap because it's already in memory. If it's working fast enough for you now and you don't expect a huge change in the number of requests, your efforts are probably better spent optimizing something else.

Otros consejos

My current solution is instead of using an xml file, to use a global resource file (.resx), where I can directly access the values from anywhere. But if there is a good way to do this with xml, I am still interested in hearing it.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top