Xml存储库实施
我正在寻找一个简单的 Xml 存储库(GetAll、添加、更新、删除)示例。
每个人都说“使用存储库模式是个好主意,因为您可以交换数据存储位置...”现在我需要将数据存储在 xml 文件上,但不知道如何实现 XML 存储库。我用谷歌搜索遍了也没找到。
如果可能,请发送包含关系数据句柄的示例。就像您在 EF 上保存产品实体一样,所有产品相关实体也会被持久化。
I'm looking for a simple Xml Repository(GetAll, Add, Update, Delete) example.
Everyone says "It's a good idea to use the repository pattern because you can swap your data store location..." now I need to store my data on a xml file and dont know how to implement a XML repository. I've searched all over the google and cannot find it.
If possible, send an example containing relational data handle. Like when you save a product entity on EF and all the product dependent entities are also persisted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新 2020: 已经有很好的 nuget 包可以很好地处理这个问题,例如 SharpRepository.XmlRepository,它是许多存储库实现套件的一部分。
嗯,彼得的解决方案很好。
只是为了分享我的实现,我将再次回答我的问题,我希望这对某人有用。请评分和评论。
XML 存储库的基类
以及另外 2 个抽象类,一个用于独立实体,另一个用于子实体。为了避免每次都读取 Xml 文件,我制作了一种缓存控件
现在是子类型的实现
现在是现实世界的实现
现在是 XDocumentProvider。它的功能是抽象对xml文件的访问,并将XDocument作为数据上下文统一到所有存储库。 这可以命名为UnitOfWork吗?
然后,我可以为不同的实体创建多个存储库,在单个数据上下文中添加挂起的持久性操作。
我已经使用模拟对使用此存储库的应用程序进行了测试,并且运行良好。
在我的 IoC 配置中,我必须设置 XDocumentProvider 的默认值。如果有必要,我们可以通过构造函数传递 XDocumentProvider 而不是这个静态“默认”属性
您对我的实现有何看法?
谢谢
Update 2020: There is already nice nuget packages that handle this nicelly, such as SharpRepository.XmlRepository, which is part of a suite of many repository implementations.
Well, Petter solution is nice.
Just to share my implementation I will answer my question again, I hope that can be usefull to someone. Please, rate and comment.
And the base class for XML Repositories
And 2 more abstract classes, one to Independent entities and other to child entities. To avoid reading the Xml file every time, I've made a kind of cache control
Now the implementation for child types
Now, a real world implementation
And now, the XDocumentProvider. Its function is to abstract the access to the xml file and unify to all repositories what XDocument is the data context. This can be named UnitOfWork?
Then I can have many repositories for diferent entities adding pendent persistence actions in a single data context.
I have made tests for my application that uses this repository using mocks and worked well.
On my IoC configuration I have to set the Default for XDocumentProvider. If necessary, we can pass the XDocumentProvider throught constructor instead of this static "Default" property
What do you think about my implementation?
Thanks
我和一位同事正是实现了这样一个 XML 存储库,它被称为 XmlRepository :-)。
它是使用 linq to XML 在内部构建的,外部访问类似于使用 linq for nhibernate 的方式。它是通过 linq to object 完成的,由于简单的 XML 注释接口,客户端代码中的使用非常简单、容易且快速理解。
当前版本(程序集)没有内置对子类或 1:n 关系的支持,但是当前的开发源代码(您也可以在上面的站点上找到)内置了这两者。
它还没有完全准备好发布——它可能有一些小错误,但是如果你愿意的话,可以尝试一下,获取源代码并改进它。它是开源的。
任何评论、愿望、建设性批评以及开源(只读:仅源)项目的补丁都会让我和我的同事(Golo Roden)感到高兴,并使项目达到更好的状态。
示例应用程序可在此处< /a>(文本为德语)。
A colleague and I implemented exactly such an XML repository, and it's called XmlRepository :-).
It's built internally with linq to XML and the external access is similar to how you use linq for nhibernate. It's done with linq to objects, the usage in client code is very simple, easy and quickly understandable because of the simple XML-commented interface.
The current release (assembly) has no support built in for subclasses or 1:n relations, but the current development source code, which you can find also on the site above, has both of them built in.
It is not completely ready to release-- it may have some bit minor bugs, but try it out, take the source code and improve it, if you like. It's open source.
Any comments, wishes, constructive criticism, and patches for the open source (read: only source) project will make my colleague (Golo Roden) and I happy and bring the project to a better state.
An example application is available here (text is in German).