I have an Asp.Net MVC web application, with an xml configuration file that has about 40 values in it.
我有一个Asp。Net MVC web应用程序,带有一个包含大约40个值的xml配置文件。
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
.
我的web应用程序只是一个页面,但它被分成几个部分视图(每个视图都有自己的模型和控制器)。每个部分都需要使用配置文件中的一些值,但不是全部,尽管有些重叠。例如,ViewA需要valueX, ViewB需要valueX和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.
我的问题是——在主HomeController中读取.xml文件,并将值传递给所有其他视图/控制器,这样更好吗?我不知道该怎么做。我不能将这些值放入模型中,因为每个部分都有自己的模型,我不认为我想使用ViewBag存储40个值。
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.
还是让每个控制器自己读取xml文件,只获取它需要的数据?这将导致相同的xml文件被读5次,每次只加载一个屏幕,然后每次更新部分视图。
Will either or these options have an impact on performance or memory?
这些选项会对性能或内存产生影响吗?
2 个解决方案
#1
3
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.
但我们说的不是大量的时间或大量的记忆,不是只有40个值。除非这些值都是兆字节。
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.
加载和缓存XML数据的方法有很多。您可以加载它一次——第一次访问应用程序。或者可以为每个请求加载它。在我的应用程序中,我在第一次访问应用程序时加载数据。它被加载到实质上是一个名称/值集合的对象中。我将接口引用传递给每一个需要它的控制器。然后我使用了StructureMap,这让这类事情变得很简单。
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#.
如果不了解应用程序的结构,很难说哪种更适合。您可以使用模型调用的延迟加载的单例对象来获取数据。单例对象本身将在首次使用时加载数据。看看Jon Skeet在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.
也许你在不必要地担心。如果文件很小,第一次读取很可能会将数据从磁盘获取到缓存中,然后您对读取文件的调用就会从缓存中得到满足。您仍然需要为每个调用解析XML,但是读取文件很便宜,因为它已经在内存中了。如果它现在对您来说运行得足够快,并且您不期望在请求的数量上有巨大的变化,那么您的努力最好花在优化其他东西上。
#2
2
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.
我现在的解决方案不是使用xml文件,而是使用全局资源文件(.resx),在那里我可以直接访问任何地方的值。但是,如果有一种很好的方法可以使用xml来实现这一点,我还是很想听一听。
#1
3
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.
但我们说的不是大量的时间或大量的记忆,不是只有40个值。除非这些值都是兆字节。
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.
加载和缓存XML数据的方法有很多。您可以加载它一次——第一次访问应用程序。或者可以为每个请求加载它。在我的应用程序中,我在第一次访问应用程序时加载数据。它被加载到实质上是一个名称/值集合的对象中。我将接口引用传递给每一个需要它的控制器。然后我使用了StructureMap,这让这类事情变得很简单。
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#.
如果不了解应用程序的结构,很难说哪种更适合。您可以使用模型调用的延迟加载的单例对象来获取数据。单例对象本身将在首次使用时加载数据。看看Jon Skeet在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.
也许你在不必要地担心。如果文件很小,第一次读取很可能会将数据从磁盘获取到缓存中,然后您对读取文件的调用就会从缓存中得到满足。您仍然需要为每个调用解析XML,但是读取文件很便宜,因为它已经在内存中了。如果它现在对您来说运行得足够快,并且您不期望在请求的数量上有巨大的变化,那么您的努力最好花在优化其他东西上。
#2
2
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.
我现在的解决方案不是使用xml文件,而是使用全局资源文件(.resx),在那里我可以直接访问任何地方的值。但是,如果有一种很好的方法可以使用xml来实现这一点,我还是很想听一听。