Collection中的新对象

编程入门 行业动态 更新时间:2024-10-24 08:24:26
本文介绍了Collection中的新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个关于这个主题的类似线索,但我决定打破 进行单独的讨论。 问题: 确定对象IsNew的最佳方法是什么?到一个集合? 问题: 在我的应用程序中,我从数据库中加载了一组对象,然后 然后将该集合传递给用户。$ / $ Order.OrderRows //从DB返回OrderRows的集合。 如果用户执行类似的操作:Order.OrderRows.Add(newRow)what 确定添加了哪些行的最佳方法?我需要这个 ,我知道是否要保持与数据库的关系... 我的收藏是否需要在内部维护加入"并且 已删除对象,以便我可以请求更改来自 集合的列表如: Order.OrderRows.GetAdded(); Order.OrderRows.GetDeleted(); ====================================== ============ = 或者最好将集合中的所有对象包装成一些 类型的 ;物品包装物喜欢: CollectionItem item = new CollectionItem(); item.Value = OrderRow; item.Status = ItemStatus。删除; 因此内部集合总是在内部存储一个集合项目的物品(好的我需要更好的名字;-))。 所以强类型集合上的Add()方法如下: public void Add(OrderRow row) { List.Add(new CollectionItem(row,item.Status.Added); } 并获取收集看起来像: public OrderRow this [int index] { get { return(OrderRow)((CollectionItem)List [index])。Value; } } 包含方法和IndexOf有点奇怪但是因为那时候我需要手动循环收集这样的集合: public int IndexOf(OrderRow row) { for(int i = 0;我< List.Count; i ++) { if(List [i] .Value.Equals(row)) { 返回i; } } } 还有其他想法吗?或者我是不是错了?

I have a similiar thread going about this topic but I decided to break it off into a seperate discussion. Question: What is the best way to determine if an object "IsNew" to a collection? Problem: In my application I load a collection of objects from the database and then pass that collection to the user. Order.OrderRows // Returns a collection of OrderRows from DB. If a user does something like: Order.OrderRows.Add(newRow) what is the best way to determine which rows have been added? I need to this so that I know whether to persist the relation to the database... Does my collection need to internally maintain lists of "Added" and "Deleted" objects so that I can then request a "Changed" list from the collection like: Order.OrderRows.GetAdded(); Order.OrderRows.GetDeleted(); ================================================== = Or would it be better to wrap all objects in the collection in some sort of "Item Wrapper" like: CollectionItem item = new CollectionItem(); item.Value = OrderRow; item.Status = ItemStatus.Deleted; So that collection internally would always internally be storing a collection a CollectionItems (Ok I need better names ;-) ). So an Add() method on the strongly-typed collection would look like: public void Add(OrderRow row) { List.Add(new CollectionItem(row,item.Status.Added); } and to get at items in the collection would look like: public OrderRow this[int index] { get { return (OrderRow)((CollectionItem)List[index]).Value; } } Methods like Contains and IndexOf get kind of weird though because then I have to manually loop through the collection like: public int IndexOf(OrderRow row) { for(int i = 0; i < List.Count; i++) { if(List[i].Value.Equals(row)) { return i; } } } Any other ideas? Or am I going about this all wrong?

推荐答案

你最需要处理它。创建自己的自定义集合 具有必要的逻辑。 " aa7im" <乔** @ nautilusnet>在消息中写道 news:11 ********************** @ f14g2000cwb.googlegr oups ... Most likly you would need to handle that. Create your own custom collection that has the necessary logic. "aa7im" <jo**@nautilusnet> wrote in message news:11**********************@f14g2000cwb.googlegr oups... 我有一个关于这个主题的类似线索,但我决定将其分解为单独的讨论。 问题:确定的最佳方法是什么如果一个对象IsNew到一个集合? 问题:在我的应用程序中,我从数据库中加载了一组对象,然后将该集合传递给用户。 Order.OrderRows //从DB返回OrderRows的集合。 如果用户执行以下操作:Order.OrderRows.Add(newRow)确定哪些行的最佳方法是什么?已添加?我需要这样才能知道是否要保持与数据库的关系... 我的收藏是否需要在内部维护已添加的列表。和已删除对象,以便我可以请求更改来自集合的列表如下: Order.OrderRows.GetAdded(); Order.OrderRows.GetDeleted(); ==== ============================================== = 或者最好将所有对象包装在集合中的某些类型的Item Wrapper中。喜欢: CollectionItem item = new CollectionItem(); item.Value = OrderRow; item.Status = ItemStatus.Deleted; 这样内部集合总是在内部存储一个集合CollectionItems(好吧我需要更好的名字;-))。 因此强类型集合上的Add()方法如下所示: public void Add(OrderRow row) List.Add(new CollectionItem(row,item.Status.Added); } 和获取集合中的项目看起来像: public OrderRow this [int index] {获取返回(OrderRow)((CollectionItem)列表[index])。值; } } 像Contains和IndexOf这样的方法有点奇怪但是因为那时我必须手动循环遍历集合: public int IndexOf(OrderRow row) { for(int i = 0; i< List.Count; i ++) { if(List [i] .Value.Equals(row)) {返回i; } } } 还有其他想法吗?或者我是不是错了? I have a similiar thread going about this topic but I decided to break it off into a seperate discussion. Question: What is the best way to determine if an object "IsNew" to a collection? Problem: In my application I load a collection of objects from the database and then pass that collection to the user. Order.OrderRows // Returns a collection of OrderRows from DB. If a user does something like: Order.OrderRows.Add(newRow) what is the best way to determine which rows have been added? I need to this so that I know whether to persist the relation to the database... Does my collection need to internally maintain lists of "Added" and "Deleted" objects so that I can then request a "Changed" list from the collection like: Order.OrderRows.GetAdded(); Order.OrderRows.GetDeleted(); ================================================== = Or would it be better to wrap all objects in the collection in some sort of "Item Wrapper" like: CollectionItem item = new CollectionItem(); item.Value = OrderRow; item.Status = ItemStatus.Deleted; So that collection internally would always internally be storing a collection a CollectionItems (Ok I need better names ;-) ). So an Add() method on the strongly-typed collection would look like: public void Add(OrderRow row) { List.Add(new CollectionItem(row,item.Status.Added); } and to get at items in the collection would look like: public OrderRow this[int index] { get { return (OrderRow)((CollectionItem)List[index]).Value; } } Methods like Contains and IndexOf get kind of weird though because then I have to manually loop through the collection like: public int IndexOf(OrderRow row) { for(int i = 0; i < List.Count; i++) { if(List[i].Value.Equals(row)) { return i; } } } Any other ideas? Or am I going about this all wrong?

问题:什么是确定对象IsNew的最佳方法是收藏? 取决于你的意思,如果按实际情况检查,你需要做的就是将所有元素与一个元素进行比较你将会插入,你会发现两个变量是否引用相同的 实例。 如果你想要的话值,然后你可以重载==运算符,或者你 可以在集合的方法中进行比较。 问题:在我的应用程序中我从数据库中加载一组对象,然后将该集合传递给用户。 Order.OrderRows //从DB返回OrderRows的集合。 如果用户执行以下操作:Order.OrderRows.Add(newRow)确定哪些行已添加的最佳方法是什么?我需要这样才能知道是否要保持与数据库的关系... 如果OrderRow有一个主键(不是订单号的外键) )你 可以确定哪些行没有持久保存到DB中。只需使用默认的 值,例如-1 我的收藏是否需要在内部维护已添加列表和已删除对象,以便我可以请求更改来自集合的列表如: Question: What is the best way to determine if an object "IsNew" to a collection? Depend of what you mean with that, if you check by actual instances all you have to do is compare all the elements of the collection with the one you will insert, with this you will find if two variables reference to the same instance. If you want mean value, then you could overload the == operator , or you could do the comparision in a method of the collection. Problem: In my application I load a collection of objects from the database and then pass that collection to the user. Order.OrderRows // Returns a collection of OrderRows from DB. If a user does something like: Order.OrderRows.Add(newRow) what is the best way to determine which rows have been added? I need to this so that I know whether to persist the relation to the database... If OrderRow has a primary key ( not the foreign key to the order number) you could determine which rows are not persisted to the DB,. just use a default value , like -1 Does my collection need to internally maintain lists of "Added" and "Deleted" objects so that I can then request a "Changed" list from the collection like:

IMO最好立即坚持更改,只要一行就是 删除你从数据库中删除它,和添加的行类似。 这样做会消除你现在遇到的问题。 干杯, - Ignacio Machin, ignacio.machin at dot.state.fl.us 佛罗里达州交通局

IMO it''s better to persist the changes right away, as soon as a row is delete you delete it from the DB, and a similar thing to the added rows. doing so will remove the problem you have now. Cheers, -- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation

遗憾的是我无法立即将更改保留到数据库中,因为所有内容都必须在交易中执行一个 工作单位。这允许用户取消动作... 我不清楚我对IsNew的需求是什么 集合的一部分。我不需要知道对象是否是IsNew。在 意义上它从来没有被持久化到数据库中(这很简单,因为你提到了),但是它是新的。从我加载它到收集。对于 示例,假设您从数据库加载了一个现有对象,并且想要将它与多对多关系中的另一个对象相关联。 嗯...让我想一个例子...... 好​​......让我们说在Order系统中有一个预定义的列表 评论。可以为每个订单分配多个评论,例如30 日退货政策,感谢您与我们一起购物,查看我们的销售额 传单即将到来下个月出来....这些早先的评论将是 添加如下: 评论firstComment = new Comment(1);加载来自DB的评论,ID为 1 评论secondComment = new评论(5);加载ID为DB的评论 of 5 评论thirdComment = new Comment(7);加载来自DB的评论,ID为 7 Order.Comments.Add(firstComment); Order.Comments.Add (secondComment); Order.Comments.Add(thirdComment); 现在在Order对象的Save()方法中我需要循环 通过Order.Comments集合查找已添加到订单中的所有评论 对象,以便我可以创建 关系 ; DB中的记录。使用现有订单 Order.Comments集合已经填充,修改后的集合 可以有一系列已经存在 关系","需要删除关系的评论"和 "需要相关添加的评论" 有关如何处理的任何想法这个?谢谢! Unfortuanely I can not persist changes immediately to the DB becuase everything must be performed within a transaction as one "unit of work". This allows users to Cancel the action... I wasn''t clear on what I need for the "IsNew" portion of the collection. I do not need to know whether the object "IsNew" in the sense that it has never been persisted to the DB (that is simple as you mention) but that it is "New" to the collection since I loaded it. For example lets say you loaded an existing object from the database and want to relate it to another object in a many-to-many relationship. Hmm... Let me think of an example.... Ok... Lets say that in the Order system there is a list of predefined "Comments". Multiple comments can be assigned per order such as "30 Day return policy", "Thank you for shopping with us", "Check our sales flyer coming out next month".... These prexisting comments would be added like: Comment firstComment = new Comment(1); Loads comment from DB with ID of 1 Comment secondComment = new Comment(5); Loads comment from DB with ID of 5 Comment thirdComment = new Comment(7); Loads comment from DB with ID of 7 Order.Comments.Add(firstComment); Order.Comments.Add(secondComment); Order.Comments.Add(thirdComment); Now in the Save() method of the Order object I will need to loop through the Order.Comments collection and find all the "Comment" objects that have been added to the order so that I can create the "relation" records in the DB. Working on existing Order the Order.Comments collection would already be populated and the collection after modifications could have a series of "Already Existing Relations", "Comments that need to have their relations removed", and "Comments that need a related added". Any ideas on how to deal with this? THANKS!

更多推荐

Collection中的新对象

本文发布于:2023-10-29 10:16:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539432.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   Collection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!