如何跟踪业务对象的变化?
我得到了创建业务对象或实体来表示诸如人之类的东西的概念。 然后我可以使用 DTO 序列化 Person 并将其发送给客户端。 如果客户端更改了对象,它可以有一个 IsDirty 标志,因此当它被发送回服务器时我知道要更新它。
但是如果我有一个 Order 对象怎么办? 它具有主标题信息、客户、供应商、所需日期等。然后它具有 OrderItems,它是一个 List<< 。 OrderItem>,是要订购的项目。 我希望能够在我的 UI 上使用此业务对象。 因此,我有一些文本框连接到位置、供应商、所需日期等,还有一个网格连接到 OrderItems。 由于 OrderItems 是一个列表,我可以轻松地向其中添加和删除记录。 但我如何跟踪这一点,尤其是已删除的项目。 我不希望删除的项目在我的网格中可见,并且如果我使用 foreach,我不应该能够迭代它们,因为它们已被删除。 但我仍然需要追踪删除的事实。 我如何跟踪更改。 我想我需要使用一个工作单元? 但随后代码似乎变得相当复杂。 那么我想知道为什么不简单地使用 DataTables 并免费获得更改跟踪呢? 但后来我了解到业务对象是如何发展的。
我在简单的人员示例中找到了各种示例,但没有找到诸如订单之类的标题详细信息示例。
顺便说一句,为此使用 C# 3.5。
I get the concept of creating a business object or entity to represent something like a Person. I can then serialize the Person using a DTO and send it down to the client. If the client changes the object, it can have an IsDirty flag on there so when it gets sent back to the server I know to update it.
But what if I have an Order object? This has the main header informaton, customer, supplier, required date, etc. Then it has OrderItems which is a List< OrderItem>, being the items to be ordered. I want to be able to use this business object on my UI. So I have some textboxes hooked up to the location, supplier, required date, etc and a grid hooked up to OrderItems. Since OrderItems is a List I can easily add and delete records to it. But how do I track this, especially the deleted items. I don't want the deleted items to be visible in my grid and I shouldn't be able to iterate over them if I used foreach, because they have been deleted. But I still need to track the fact there was a deletion. How do I track the changes. I think I need to use a unit of work? But then the code seems to become quite complex. So then I wonder why not simply use DataTables and get the change tracking for free? But then I read how business objects are the way to go.
I’ve found various examples on simple Person examples, bnut not header-detail examples like Orders.
BTW using C# 3.5 for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
数据对象不跟踪更改。 更改跟踪发生在 DataContext 和您通过 DataContext 检索的对象上。 因此,为了跟踪更改,您需要执行以下操作:
现在,在代码隐藏中,您可以执行以下操作:
我不建议直接在代码隐藏中使用数据上下文,但这是开始使用 Linq To 的好方法SQL。 我建议将所有数据库交互放在外部项目中,并从 GUI 调用封装此行为的类。
如果您不熟悉 Linq To Sql,我建议您创建一个 Linq To Sql (dbml) 文件。
在解决方案资源管理器中右键单击您的项目,然后选择“添加新项目”。 选择 Linq To SQL 文件,然后它会让您连接到数据库并选择表。
然后,您可以查看生成的代码,并获得有关 Linq To Sql 如何工作以及可以用它做什么的一些好主意。
使用它作为使用 Linq to SQL 的指南,这将带您走得更远......
The data objects don't track changes. The change tracking occurs on the DataContext and objects that you've retrieved through the DataContext. So in order to track changes you need to do the following:
Now in your codebehind you can do something like:
I wouldn't recommend directly using the data context in the code behind, but this is a good way to get started with Linq To SQL. I would recommend putting all your database interactions in an external project and call from the GUI to the classes that encapsulate this behavior.
I would recommend creating a Linq To Sql (dbml) file if you're new to Linq To Sql.
Right click on your project in solution explorer, and select Add New Item. Select Linq To SQL file, and it will then let you connect to your database and select the tables.
You can then look at the generated code, and get some great ideas on how Linq To Sql works and what you can do with it.
Use that as a guideline on working with Linq to SQL and that will take you far...
您很清楚需要一个工作单元,但没有编写一个。 使用 NHibernate 或其他一些 ORM。 这就是它们的用途。 它们内置了工作单元。
对于大多数应用程序来说,业务对象确实是“必经之路”。 你正在潜入一个很深的领域,有很多东西要做。 看看DDD。
我还强烈建议不要在代码隐藏中使用类似的代码。 研究 MVP 模式。
我也会(虽然我费心去学习很多新的、非常关键的东西)研究 SOLID。
您可能想看看 JP Boodhoo 的 .net 课程,因为它涵盖了很多这些内容。
You're spot on about needing a unit of work, but don't write one. Use NHibernate or some other ORM. That is what they're made for. They have Unit of Works built in.
Business objects are indeed "the way to go" for most applications. You're diving into a deep area and there will be much learning to do. Look into DDD.
I'd also strongly advise against code like that in your code-behind. Look into the MVP pattern.
I'd also (while I was bothering to learn lots of new, highly critical things) look into SOLID.
You may want to check out JP Boodhoo's nothing but .net course as it covers a lot of these things.
首先,您可以使用解决这些问题的现有框架,例如 CSLA.NET。 该框架的作者已经解决了这些问题。 请访问 http://www.rockfordlhotka.net/cslanet/ 进行此操作。 即使您不使用完整的框架,这些概念仍然适用。
如果您想推出自己的,我过去所做的就是不使用 List 作为我的集合,而是使用从 BindingList 派生的自定义类型。 从 BindingList 继承允许您覆盖添加/删除项目的行为。 因此,您可以拥有另一个“删除”项目的内部集合。 每次在集合上调用重写的Remove方法时,请将项目放入“已删除”集合中,然后调用Remove方法的基本实现。 您可以对添加的项目或更改的项目执行相同的操作。
Firstly, you can use an existing framework that addresses these issues, like CSLA.NET. The author of this framework has tackled these very issues. Go to http://www.rockfordlhotka.net/cslanet/ for this. Even if you don't use the full framework, the concepts are still applicable.
If you wanted to roll your own, what I've done in the past was to instead of using List for my collections, I've used a custom type derived from BindingList. Inhereting from BindingList allows you to override the behaviour of add/remove item. So you can for example have another internal collection of "delteted" items. Every time the overriden Remove method is called on your collection, put the item into the "deleted" collection, and then call the base implementation of the Remove method. You can do the same for added items or changed items.