在 ASP.NET 站点中正确使用基于 xml 文件的存储作为数据库替代方案
我正在构建一个小型应用程序,为了减少托管成本和依赖性,我计划将所有持久数据存储到 xml 文件而不是 Sql Server 数据库。
在这种情况下,网站受众仅限于朋友和家人 - 预计并发用户不会超过几个,因此网站不需要进行扩展。在所有事务中从磁盘上逐字打开和关闭 xml 文件是否可行?一个页面最多可能会显示来自几个 xml 文件的数据,有时用户会执行需要更新其中一个的操作。
例如,大致遵循用于获取和保存“事物”的存储库模式,一些方法可能会喜欢:
public IEnumerable<Thing> GetThings() {
XElement xml = XElement.Load(_xmlRepositoryPath);
var q = from s in xml.Descendants("Thing")
select new Thing {
//set properties...
};
return q;
}
public void SaveThing(Thing t) {
XElement xml = XElement.Load(_xmlRepositoryPath);
//update xml...
xml.Save(_xmlRepositoryPath);
}
这种方法有任何陷阱或问题吗?我宁愿避免添加额外的缓存或内存数据层带来的额外复杂性。额外加分:在用户负载或交易级别的什么情况下,您认为需要以不同的方式实施?
I'm building a small application and to reduce hosting costs and dependencies, I am planning to store all persistent data to xml files rather than a Sql Server database.
In this case, the site audience is limited to friends and family - no more than a few concurrent users are ever expected, so the site does not need to scale. Is it feasible to literally open and close an xml file from disk on all transactions? At most a page might display data from a couple xml files, and occasionally a user will perform an action requiring an update of one.
For example, roughly following the repository pattern for getting and saving "Things," some methods would like like:
public IEnumerable<Thing> GetThings() {
XElement xml = XElement.Load(_xmlRepositoryPath);
var q = from s in xml.Descendants("Thing")
select new Thing {
//set properties...
};
return q;
}
public void SaveThing(Thing t) {
XElement xml = XElement.Load(_xmlRepositoryPath);
//update xml...
xml.Save(_xmlRepositoryPath);
}
Any pitfalls or problems with this approach? I'd rather avoid additional complexity of adding an additional caching or in-memory data layer. Extra credit: at what point of user load or transaction levels do think this would need to be implemented differently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数据库将提供的主要内容(文件系统不会提供)是 [atomicity](http://en.wikipedia.org/wiki/Atomicity_(database_systems%29)。
一旦有多个人访问您的 xml 文件,您就需要实现 ReaderWriter 锁,以确保在您尝试更新文件时没有人在读取。这是一个不平凡的问题,但大多数数据库系统都可以解决这个问题。
如果您关心成本,那么有很多开源解决方案。
无论您决定采用哪种解决方案,请确保封装所有数据访问,以便更改它并不那么困难。
The main thing that a database will provide, which the file system wont, is [atomicity](http://en.wikipedia.org/wiki/Atomicity_(database_systems%29).
As soon as you have more than one person accessing your xml file, you need to implement a ReaderWriter lock to make sure that no one's reading whilst you're trying to update the file. It's a non-trivial problem, but one that's solved with most database systems.
If you're concerned with cost, then there are any number of opensource solutions.
What ever solution you decide on, make sure you encapsulate all the data access so that changing it isn't so hard.
就调用 Load 而言 - 您可以在每次点击时执行此操作,服务器甚至不会眨眼,我们有一些站点可以有效地执行此操作(加载 XML、使用 XSLT 和基于 URL 的参数呈现为 HTML、传递到浏览器) ,XSLT 的加载是显式的,或者是调用渲染时隐含的 XML),我们只是没有看到它们的问题,您需要 100 多个并发用户,这样在阅读数据。
在进行文件写入(保存)方面 - 不知道,但我不认为这是一个大问题,处理并发(无论如何都是一个问题)将是我比服务器负载更关心的事情,在您的使用级别,创造性地使用应用程序锁可能就足够了,对于任何严重的情况,使用 XML 作为数据库将变得具有挑战性。
顺便说一句,这是 ASP.NET 明显表现出色的一个领域 - 服务器端代码的性能 - 在一般情况下 - 非常优秀(可能太好了)。
In terms of calling Load - you can do it on every hit and the server won't even blink we have sites that are effectively doing pretty much that (load XML, render to HTML using XSLT and parameters based on the URL, deliver to browser, load of the XSLT is explicit or the XML implied by the call to render with transform) and we just don't see issues with them, you'd need concurrent users into the 100s for this to start to be an issue when reading the data.
In terms of doing the file write (save) - don't know but I'd not expect it to be a huge issue, dealing with concurrency (a problem regardless) would be something that would concern me far more than the server load, at your usage levels creative use of app lock might be sufficient, for anything serious this is where the use of XML as a database would become challenging.
As an aside this is an area where ASP.NET clearly rocks - the performance of the server side code - in the general case - is excellent (too good probably).